src/thread/windows/SDL_syssem.c
author Sam Lantinga
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5262 b530ef003506
child 6044 35448a5ea044
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 /* Semaphore functions using the Win32 API */
    24 
    25 #include "../../core/windows/SDL_windows.h"
    26 
    27 #include "SDL_thread.h"
    28 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
    29 #include "win_ce_semaphore.h"
    30 #endif
    31 
    32 
    33 struct SDL_semaphore
    34 {
    35 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
    36     SYNCHHANDLE id;
    37 #else
    38     HANDLE id;
    39 #endif
    40     LONG count;
    41 };
    42 
    43 
    44 /* Create a semaphore */
    45 SDL_sem *
    46 SDL_CreateSemaphore(Uint32 initial_value)
    47 {
    48     SDL_sem *sem;
    49 
    50     /* Allocate sem memory */
    51     sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
    52     if (sem) {
    53         /* Create the semaphore, with max value 32K */
    54 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
    55         sem->id = CreateSemaphoreCE(NULL, initial_value, 32 * 1024, NULL);
    56 #else
    57         sem->id = CreateSemaphore(NULL, initial_value, 32 * 1024, NULL);
    58 #endif
    59         sem->count = initial_value;
    60         if (!sem->id) {
    61             SDL_SetError("Couldn't create semaphore");
    62             SDL_free(sem);
    63             sem = NULL;
    64         }
    65     } else {
    66         SDL_OutOfMemory();
    67     }
    68     return (sem);
    69 }
    70 
    71 /* Free the semaphore */
    72 void
    73 SDL_DestroySemaphore(SDL_sem * sem)
    74 {
    75     if (sem) {
    76         if (sem->id) {
    77 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
    78             CloseSynchHandle(sem->id);
    79 #else
    80             CloseHandle(sem->id);
    81 #endif
    82             sem->id = 0;
    83         }
    84         SDL_free(sem);
    85     }
    86 }
    87 
    88 int
    89 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
    90 {
    91     int retval;
    92     DWORD dwMilliseconds;
    93 
    94     if (!sem) {
    95         SDL_SetError("Passed a NULL sem");
    96         return -1;
    97     }
    98 
    99     if (timeout == SDL_MUTEX_MAXWAIT) {
   100         dwMilliseconds = INFINITE;
   101     } else {
   102         dwMilliseconds = (DWORD) timeout;
   103     }
   104 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
   105     switch (WaitForSemaphoreCE(sem->id, dwMilliseconds)) {
   106 #else
   107     switch (WaitForSingleObject(sem->id, dwMilliseconds)) {
   108 #endif
   109     case WAIT_OBJECT_0:
   110         InterlockedDecrement(&sem->count);
   111         retval = 0;
   112         break;
   113     case WAIT_TIMEOUT:
   114         retval = SDL_MUTEX_TIMEDOUT;
   115         break;
   116     default:
   117         SDL_SetError("WaitForSingleObject() failed");
   118         retval = -1;
   119         break;
   120     }
   121     return retval;
   122 }
   123 
   124 int
   125 SDL_SemTryWait(SDL_sem * sem)
   126 {
   127     return SDL_SemWaitTimeout(sem, 0);
   128 }
   129 
   130 int
   131 SDL_SemWait(SDL_sem * sem)
   132 {
   133     return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
   134 }
   135 
   136 /* Returns the current count of the semaphore */
   137 Uint32
   138 SDL_SemValue(SDL_sem * sem)
   139 {
   140     if (!sem) {
   141         SDL_SetError("Passed a NULL sem");
   142         return 0;
   143     }
   144     return (Uint32)sem->count;
   145 }
   146 
   147 int
   148 SDL_SemPost(SDL_sem * sem)
   149 {
   150     if (!sem) {
   151         SDL_SetError("Passed a NULL sem");
   152         return -1;
   153     }
   154     /* Increase the counter in the first place, because
   155      * after a successful release the semaphore may
   156      * immediately get destroyed by another thread which
   157      * is waiting for this semaphore.
   158      */
   159     InterlockedIncrement(&sem->count);
   160 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
   161     if (ReleaseSemaphoreCE(sem->id, 1, NULL) == FALSE) {
   162 #else
   163     if (ReleaseSemaphore(sem->id, 1, NULL) == FALSE) {
   164 #endif
   165         InterlockedDecrement(&sem->count);      /* restore */
   166         SDL_SetError("ReleaseSemaphore() failed");
   167         return -1;
   168     }
   169     return 0;
   170 }
   171 
   172 /* vi: set ts=4 sw=4 expandtab: */