src/thread/windows/SDL_syssem.c
changeset 5062 e8916fe9cfc8
parent 4475 883a2f2ff43f
child 5086 c2539ff054c8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/thread/windows/SDL_syssem.c	Thu Jan 20 18:04:05 2011 -0800
     1.3 @@ -0,0 +1,174 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2010 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +/* Semaphore functions using the Win32 API */
    1.28 +
    1.29 +#define WIN32_LEAN_AND_MEAN
    1.30 +#include <windows.h>
    1.31 +
    1.32 +#include "SDL_thread.h"
    1.33 +#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
    1.34 +#include "win_ce_semaphore.h"
    1.35 +#endif
    1.36 +
    1.37 +
    1.38 +struct SDL_semaphore
    1.39 +{
    1.40 +#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
    1.41 +    SYNCHHANDLE id;
    1.42 +#else
    1.43 +    HANDLE id;
    1.44 +#endif
    1.45 +    LONG volatile count;
    1.46 +};
    1.47 +
    1.48 +
    1.49 +/* Create a semaphore */
    1.50 +SDL_sem *
    1.51 +SDL_CreateSemaphore(Uint32 initial_value)
    1.52 +{
    1.53 +    SDL_sem *sem;
    1.54 +
    1.55 +    /* Allocate sem memory */
    1.56 +    sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
    1.57 +    if (sem) {
    1.58 +        /* Create the semaphore, with max value 32K */
    1.59 +#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
    1.60 +        sem->id = CreateSemaphoreCE(NULL, initial_value, 32 * 1024, NULL);
    1.61 +#else
    1.62 +        sem->id = CreateSemaphore(NULL, initial_value, 32 * 1024, NULL);
    1.63 +#endif
    1.64 +        sem->count = initial_value;
    1.65 +        if (!sem->id) {
    1.66 +            SDL_SetError("Couldn't create semaphore");
    1.67 +            SDL_free(sem);
    1.68 +            sem = NULL;
    1.69 +        }
    1.70 +    } else {
    1.71 +        SDL_OutOfMemory();
    1.72 +    }
    1.73 +    return (sem);
    1.74 +}
    1.75 +
    1.76 +/* Free the semaphore */
    1.77 +void
    1.78 +SDL_DestroySemaphore(SDL_sem * sem)
    1.79 +{
    1.80 +    if (sem) {
    1.81 +        if (sem->id) {
    1.82 +#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
    1.83 +            CloseSynchHandle(sem->id);
    1.84 +#else
    1.85 +            CloseHandle(sem->id);
    1.86 +#endif
    1.87 +            sem->id = 0;
    1.88 +        }
    1.89 +        SDL_free(sem);
    1.90 +    }
    1.91 +}
    1.92 +
    1.93 +int
    1.94 +SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
    1.95 +{
    1.96 +    int retval;
    1.97 +    DWORD dwMilliseconds;
    1.98 +
    1.99 +    if (!sem) {
   1.100 +        SDL_SetError("Passed a NULL sem");
   1.101 +        return -1;
   1.102 +    }
   1.103 +
   1.104 +    if (timeout == SDL_MUTEX_MAXWAIT) {
   1.105 +        dwMilliseconds = INFINITE;
   1.106 +    } else {
   1.107 +        dwMilliseconds = (DWORD) timeout;
   1.108 +    }
   1.109 +#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
   1.110 +    switch (WaitForSemaphoreCE(sem->id, dwMilliseconds)) {
   1.111 +#else
   1.112 +    switch (WaitForSingleObject(sem->id, dwMilliseconds)) {
   1.113 +#endif
   1.114 +    case WAIT_OBJECT_0:
   1.115 +        InterlockedDecrement(&sem->count);
   1.116 +        retval = 0;
   1.117 +        break;
   1.118 +    case WAIT_TIMEOUT:
   1.119 +        retval = SDL_MUTEX_TIMEDOUT;
   1.120 +        break;
   1.121 +    default:
   1.122 +        SDL_SetError("WaitForSingleObject() failed");
   1.123 +        retval = -1;
   1.124 +        break;
   1.125 +    }
   1.126 +    return retval;
   1.127 +}
   1.128 +
   1.129 +int
   1.130 +SDL_SemTryWait(SDL_sem * sem)
   1.131 +{
   1.132 +    return SDL_SemWaitTimeout(sem, 0);
   1.133 +}
   1.134 +
   1.135 +int
   1.136 +SDL_SemWait(SDL_sem * sem)
   1.137 +{
   1.138 +    return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
   1.139 +}
   1.140 +
   1.141 +/* Returns the current count of the semaphore */
   1.142 +Uint32
   1.143 +SDL_SemValue(SDL_sem * sem)
   1.144 +{
   1.145 +    if (!sem) {
   1.146 +        SDL_SetError("Passed a NULL sem");
   1.147 +        return 0;
   1.148 +    }
   1.149 +    return (Uint32)sem->count;
   1.150 +}
   1.151 +
   1.152 +int
   1.153 +SDL_SemPost(SDL_sem * sem)
   1.154 +{
   1.155 +    if (!sem) {
   1.156 +        SDL_SetError("Passed a NULL sem");
   1.157 +        return -1;
   1.158 +    }
   1.159 +    /* Increase the counter in the first place, because
   1.160 +     * after a successful release the semaphore may
   1.161 +     * immediately get destroyed by another thread which
   1.162 +     * is waiting for this semaphore.
   1.163 +     */
   1.164 +    InterlockedIncrement(&sem->count);
   1.165 +#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
   1.166 +    if (ReleaseSemaphoreCE(sem->id, 1, NULL) == FALSE) {
   1.167 +#else
   1.168 +    if (ReleaseSemaphore(sem->id, 1, NULL) == FALSE) {
   1.169 +#endif
   1.170 +        InterlockedDecrement(&sem->count);      /* restore */
   1.171 +        SDL_SetError("ReleaseSemaphore() failed");
   1.172 +        return -1;
   1.173 +    }
   1.174 +    return 0;
   1.175 +}
   1.176 +
   1.177 +/* vi: set ts=4 sw=4 expandtab: */