PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

/project/jni/sdl-1.3/src/thread/windows/SDL_syssem.c

https://github.com/aichunyu/FFPlayer
C | 176 lines | 125 code | 19 blank | 32 comment | 23 complexity | 559459122376f662091fb41f28e335a4 MD5 | raw file
  1/*
  2  Simple DirectMedia Layer
  3  Copyright (C) 1997-2012 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#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
 31#include "win_ce_semaphore.h"
 32#endif
 33
 34
 35struct SDL_semaphore
 36{
 37#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
 38    SYNCHHANDLE id;
 39#else
 40    HANDLE id;
 41#endif
 42    LONG count;
 43};
 44
 45
 46/* Create a semaphore */
 47SDL_sem *
 48SDL_CreateSemaphore(Uint32 initial_value)
 49{
 50    SDL_sem *sem;
 51
 52    /* Allocate sem memory */
 53    sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
 54    if (sem) {
 55        /* Create the semaphore, with max value 32K */
 56#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
 57        sem->id = CreateSemaphoreCE(NULL, initial_value, 32 * 1024, NULL);
 58#else
 59        sem->id = CreateSemaphore(NULL, initial_value, 32 * 1024, NULL);
 60#endif
 61        sem->count = initial_value;
 62        if (!sem->id) {
 63            SDL_SetError("Couldn't create semaphore");
 64            SDL_free(sem);
 65            sem = NULL;
 66        }
 67    } else {
 68        SDL_OutOfMemory();
 69    }
 70    return (sem);
 71}
 72
 73/* Free the semaphore */
 74void
 75SDL_DestroySemaphore(SDL_sem * sem)
 76{
 77    if (sem) {
 78        if (sem->id) {
 79#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
 80            CloseSynchHandle(sem->id);
 81#else
 82            CloseHandle(sem->id);
 83#endif
 84            sem->id = 0;
 85        }
 86        SDL_free(sem);
 87    }
 88}
 89
 90int
 91SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
 92{
 93    int retval;
 94    DWORD dwMilliseconds;
 95
 96    if (!sem) {
 97        SDL_SetError("Passed a NULL sem");
 98        return -1;
 99    }
100
101    if (timeout == SDL_MUTEX_MAXWAIT) {
102        dwMilliseconds = INFINITE;
103    } else {
104        dwMilliseconds = (DWORD) timeout;
105    }
106#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
107    switch (WaitForSemaphoreCE(sem->id, dwMilliseconds)) {
108#else
109    switch (WaitForSingleObject(sem->id, dwMilliseconds)) {
110#endif
111    case WAIT_OBJECT_0:
112        InterlockedDecrement(&sem->count);
113        retval = 0;
114        break;
115    case WAIT_TIMEOUT:
116        retval = SDL_MUTEX_TIMEDOUT;
117        break;
118    default:
119        SDL_SetError("WaitForSingleObject() failed");
120        retval = -1;
121        break;
122    }
123    return retval;
124}
125
126int
127SDL_SemTryWait(SDL_sem * sem)
128{
129    return SDL_SemWaitTimeout(sem, 0);
130}
131
132int
133SDL_SemWait(SDL_sem * sem)
134{
135    return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
136}
137
138/* Returns the current count of the semaphore */
139Uint32
140SDL_SemValue(SDL_sem * sem)
141{
142    if (!sem) {
143        SDL_SetError("Passed a NULL sem");
144        return 0;
145    }
146    return (Uint32)sem->count;
147}
148
149int
150SDL_SemPost(SDL_sem * sem)
151{
152    if (!sem) {
153        SDL_SetError("Passed a NULL sem");
154        return -1;
155    }
156    /* Increase the counter in the first place, because
157     * after a successful release the semaphore may
158     * immediately get destroyed by another thread which
159     * is waiting for this semaphore.
160     */
161    InterlockedIncrement(&sem->count);
162#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
163    if (ReleaseSemaphoreCE(sem->id, 1, NULL) == FALSE) {
164#else
165    if (ReleaseSemaphore(sem->id, 1, NULL) == FALSE) {
166#endif
167        InterlockedDecrement(&sem->count);      /* restore */
168        SDL_SetError("ReleaseSemaphore() failed");
169        return -1;
170    }
171    return 0;
172}
173
174#endif /* SDL_THREAD_WINDOWS */
175
176/* vi: set ts=4 sw=4 expandtab: */