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