src/thread/windows/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 15 Feb 2013 08:47:44 -0800
changeset 6885 700f1b25f77f
parent 6430 48d519500f7e
child 7037 3fedf1f25b94
permissions -rw-r--r--
Happy New Year!
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 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 #if SDL_THREAD_WINDOWS
    24 
    25 /* Semaphore functions using the Win32 API */
    26 
    27 #include "../../core/windows/SDL_windows.h"
    28 
    29 #include "SDL_thread.h"
    30 
    31 struct SDL_semaphore
    32 {
    33     HANDLE id;
    34     LONG count;
    35 };
    36 
    37 
    38 /* Create a semaphore */
    39 SDL_sem *
    40 SDL_CreateSemaphore(Uint32 initial_value)
    41 {
    42     SDL_sem *sem;
    43 
    44     /* Allocate sem memory */
    45     sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
    46     if (sem) {
    47         /* Create the semaphore, with max value 32K */
    48         sem->id = CreateSemaphore(NULL, initial_value, 32 * 1024, NULL);
    49         sem->count = initial_value;
    50         if (!sem->id) {
    51             SDL_SetError("Couldn't create semaphore");
    52             SDL_free(sem);
    53             sem = NULL;
    54         }
    55     } else {
    56         SDL_OutOfMemory();
    57     }
    58     return (sem);
    59 }
    60 
    61 /* Free the semaphore */
    62 void
    63 SDL_DestroySemaphore(SDL_sem * sem)
    64 {
    65     if (sem) {
    66         if (sem->id) {
    67             CloseHandle(sem->id);
    68             sem->id = 0;
    69         }
    70         SDL_free(sem);
    71     }
    72 }
    73 
    74 int
    75 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
    76 {
    77     int retval;
    78     DWORD dwMilliseconds;
    79 
    80     if (!sem) {
    81         SDL_SetError("Passed a NULL sem");
    82         return -1;
    83     }
    84 
    85     if (timeout == SDL_MUTEX_MAXWAIT) {
    86         dwMilliseconds = INFINITE;
    87     } else {
    88         dwMilliseconds = (DWORD) timeout;
    89     }
    90     switch (WaitForSingleObject(sem->id, dwMilliseconds)) {
    91     case WAIT_OBJECT_0:
    92         InterlockedDecrement(&sem->count);
    93         retval = 0;
    94         break;
    95     case WAIT_TIMEOUT:
    96         retval = SDL_MUTEX_TIMEDOUT;
    97         break;
    98     default:
    99         SDL_SetError("WaitForSingleObject() failed");
   100         retval = -1;
   101         break;
   102     }
   103     return retval;
   104 }
   105 
   106 int
   107 SDL_SemTryWait(SDL_sem * sem)
   108 {
   109     return SDL_SemWaitTimeout(sem, 0);
   110 }
   111 
   112 int
   113 SDL_SemWait(SDL_sem * sem)
   114 {
   115     return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
   116 }
   117 
   118 /* Returns the current count of the semaphore */
   119 Uint32
   120 SDL_SemValue(SDL_sem * sem)
   121 {
   122     if (!sem) {
   123         SDL_SetError("Passed a NULL sem");
   124         return 0;
   125     }
   126     return (Uint32)sem->count;
   127 }
   128 
   129 int
   130 SDL_SemPost(SDL_sem * sem)
   131 {
   132     if (!sem) {
   133         SDL_SetError("Passed a NULL sem");
   134         return -1;
   135     }
   136     /* Increase the counter in the first place, because
   137      * after a successful release the semaphore may
   138      * immediately get destroyed by another thread which
   139      * is waiting for this semaphore.
   140      */
   141     InterlockedIncrement(&sem->count);
   142     if (ReleaseSemaphore(sem->id, 1, NULL) == FALSE) {
   143         InterlockedDecrement(&sem->count);      /* restore */
   144         SDL_SetError("ReleaseSemaphore() failed");
   145         return -1;
   146     }
   147     return 0;
   148 }
   149 
   150 #endif /* SDL_THREAD_WINDOWS */
   151 
   152 /* vi: set ts=4 sw=4 expandtab: */