src/thread/windows/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 20 Jan 2011 18:04:05 -0800
changeset 5062 e8916fe9cfc8
parent 4475 src/thread/win32/SDL_syssem.c@883a2f2ff43f
child 5086 c2539ff054c8
permissions -rw-r--r--
Fixed bug #925

Changed "win32" to "windows"
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* Semaphore functions using the Win32 API */
slouken@0
    25
slouken@1433
    26
#define WIN32_LEAN_AND_MEAN
slouken@1433
    27
#include <windows.h>
slouken@0
    28
slouken@0
    29
#include "SDL_thread.h"
slouken@36
    30
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@36
    31
#include "win_ce_semaphore.h"
slouken@36
    32
#endif
slouken@0
    33
slouken@0
    34
slouken@1895
    35
struct SDL_semaphore
slouken@1895
    36
{
slouken@36
    37
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@1895
    38
    SYNCHHANDLE id;
slouken@36
    39
#else
slouken@1895
    40
    HANDLE id;
slouken@36
    41
#endif
slouken@4475
    42
    LONG volatile count;
slouken@0
    43
};
slouken@0
    44
slouken@0
    45
slouken@0
    46
/* Create a semaphore */
slouken@1895
    47
SDL_sem *
slouken@1895
    48
SDL_CreateSemaphore(Uint32 initial_value)
slouken@0
    49
{
slouken@1895
    50
    SDL_sem *sem;
slouken@0
    51
slouken@1895
    52
    /* Allocate sem memory */
slouken@1895
    53
    sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
slouken@1895
    54
    if (sem) {
slouken@1895
    55
        /* Create the semaphore, with max value 32K */
slouken@36
    56
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@1895
    57
        sem->id = CreateSemaphoreCE(NULL, initial_value, 32 * 1024, NULL);
slouken@36
    58
#else
slouken@1895
    59
        sem->id = CreateSemaphore(NULL, initial_value, 32 * 1024, NULL);
slouken@36
    60
#endif
slouken@1895
    61
        sem->count = initial_value;
slouken@1895
    62
        if (!sem->id) {
slouken@1895
    63
            SDL_SetError("Couldn't create semaphore");
slouken@1895
    64
            SDL_free(sem);
slouken@1895
    65
            sem = NULL;
slouken@1895
    66
        }
slouken@1895
    67
    } else {
slouken@1895
    68
        SDL_OutOfMemory();
slouken@1895
    69
    }
slouken@1895
    70
    return (sem);
slouken@0
    71
}
slouken@0
    72
slouken@0
    73
/* Free the semaphore */
slouken@1895
    74
void
slouken@1895
    75
SDL_DestroySemaphore(SDL_sem * sem)
slouken@0
    76
{
slouken@1895
    77
    if (sem) {
slouken@1895
    78
        if (sem->id) {
slouken@36
    79
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@1895
    80
            CloseSynchHandle(sem->id);
slouken@36
    81
#else
slouken@1895
    82
            CloseHandle(sem->id);
slouken@36
    83
#endif
slouken@1895
    84
            sem->id = 0;
slouken@1895
    85
        }
slouken@1895
    86
        SDL_free(sem);
slouken@1895
    87
    }
slouken@0
    88
}
slouken@0
    89
slouken@1895
    90
int
slouken@1895
    91
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
slouken@0
    92
{
slouken@1895
    93
    int retval;
slouken@1895
    94
    DWORD dwMilliseconds;
slouken@0
    95
slouken@1895
    96
    if (!sem) {
slouken@1895
    97
        SDL_SetError("Passed a NULL sem");
slouken@1895
    98
        return -1;
slouken@1895
    99
    }
slouken@0
   100
slouken@1895
   101
    if (timeout == SDL_MUTEX_MAXWAIT) {
slouken@1895
   102
        dwMilliseconds = INFINITE;
slouken@1895
   103
    } else {
slouken@1895
   104
        dwMilliseconds = (DWORD) timeout;
slouken@1895
   105
    }
slouken@36
   106
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@1895
   107
    switch (WaitForSemaphoreCE(sem->id, dwMilliseconds)) {
slouken@36
   108
#else
slouken@1895
   109
    switch (WaitForSingleObject(sem->id, dwMilliseconds)) {
slouken@36
   110
#endif
slouken@1895
   111
    case WAIT_OBJECT_0:
slouken@3069
   112
        InterlockedDecrement(&sem->count);
slouken@1895
   113
        retval = 0;
slouken@1895
   114
        break;
slouken@1895
   115
    case WAIT_TIMEOUT:
slouken@1895
   116
        retval = SDL_MUTEX_TIMEDOUT;
slouken@1895
   117
        break;
slouken@1895
   118
    default:
slouken@1895
   119
        SDL_SetError("WaitForSingleObject() failed");
slouken@1895
   120
        retval = -1;
slouken@1895
   121
        break;
slouken@1895
   122
    }
slouken@1895
   123
    return retval;
slouken@0
   124
}
slouken@0
   125
slouken@1895
   126
int
slouken@1895
   127
SDL_SemTryWait(SDL_sem * sem)
slouken@0
   128
{
slouken@1895
   129
    return SDL_SemWaitTimeout(sem, 0);
slouken@0
   130
}
slouken@0
   131
slouken@1895
   132
int
slouken@1895
   133
SDL_SemWait(SDL_sem * sem)
slouken@0
   134
{
slouken@1895
   135
    return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
slouken@0
   136
}
slouken@0
   137
slouken@0
   138
/* Returns the current count of the semaphore */
slouken@1895
   139
Uint32
slouken@1895
   140
SDL_SemValue(SDL_sem * sem)
slouken@0
   141
{
slouken@1895
   142
    if (!sem) {
slouken@1895
   143
        SDL_SetError("Passed a NULL sem");
slouken@1895
   144
        return 0;
slouken@1895
   145
    }
slouken@4475
   146
    return (Uint32)sem->count;
slouken@0
   147
}
slouken@0
   148
slouken@1895
   149
int
slouken@1895
   150
SDL_SemPost(SDL_sem * sem)
slouken@0
   151
{
slouken@1895
   152
    if (!sem) {
slouken@1895
   153
        SDL_SetError("Passed a NULL sem");
slouken@1895
   154
        return -1;
slouken@1895
   155
    }
slouken@1895
   156
    /* Increase the counter in the first place, because
slouken@1895
   157
     * after a successful release the semaphore may
slouken@1895
   158
     * immediately get destroyed by another thread which
slouken@1895
   159
     * is waiting for this semaphore.
slouken@1895
   160
     */
slouken@3069
   161
    InterlockedIncrement(&sem->count);
slouken@36
   162
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@1895
   163
    if (ReleaseSemaphoreCE(sem->id, 1, NULL) == FALSE) {
slouken@36
   164
#else
slouken@1895
   165
    if (ReleaseSemaphore(sem->id, 1, NULL) == FALSE) {
slouken@36
   166
#endif
slouken@3071
   167
        InterlockedDecrement(&sem->count);      /* restore */
slouken@1895
   168
        SDL_SetError("ReleaseSemaphore() failed");
slouken@1895
   169
        return -1;
slouken@1895
   170
    }
slouken@1895
   171
    return 0;
slouken@0
   172
}
slouken@1895
   173
slouken@1895
   174
/* vi: set ts=4 sw=4 expandtab: */