src/thread/windows/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5262 b530ef003506
child 6044 35448a5ea044
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@0
    22
slouken@0
    23
/* Semaphore functions using the Win32 API */
slouken@0
    24
slouken@5090
    25
#include "../../core/windows/SDL_windows.h"
slouken@0
    26
slouken@0
    27
#include "SDL_thread.h"
slouken@36
    28
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@36
    29
#include "win_ce_semaphore.h"
slouken@36
    30
#endif
slouken@0
    31
slouken@0
    32
slouken@1895
    33
struct SDL_semaphore
slouken@1895
    34
{
slouken@36
    35
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@1895
    36
    SYNCHHANDLE id;
slouken@36
    37
#else
slouken@1895
    38
    HANDLE id;
slouken@36
    39
#endif
slouken@5086
    40
    LONG count;
slouken@0
    41
};
slouken@0
    42
slouken@0
    43
slouken@0
    44
/* Create a semaphore */
slouken@1895
    45
SDL_sem *
slouken@1895
    46
SDL_CreateSemaphore(Uint32 initial_value)
slouken@0
    47
{
slouken@1895
    48
    SDL_sem *sem;
slouken@0
    49
slouken@1895
    50
    /* Allocate sem memory */
slouken@1895
    51
    sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
slouken@1895
    52
    if (sem) {
slouken@1895
    53
        /* Create the semaphore, with max value 32K */
slouken@36
    54
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@1895
    55
        sem->id = CreateSemaphoreCE(NULL, initial_value, 32 * 1024, NULL);
slouken@36
    56
#else
slouken@1895
    57
        sem->id = CreateSemaphore(NULL, initial_value, 32 * 1024, NULL);
slouken@36
    58
#endif
slouken@1895
    59
        sem->count = initial_value;
slouken@1895
    60
        if (!sem->id) {
slouken@1895
    61
            SDL_SetError("Couldn't create semaphore");
slouken@1895
    62
            SDL_free(sem);
slouken@1895
    63
            sem = NULL;
slouken@1895
    64
        }
slouken@1895
    65
    } else {
slouken@1895
    66
        SDL_OutOfMemory();
slouken@1895
    67
    }
slouken@1895
    68
    return (sem);
slouken@0
    69
}
slouken@0
    70
slouken@0
    71
/* Free the semaphore */
slouken@1895
    72
void
slouken@1895
    73
SDL_DestroySemaphore(SDL_sem * sem)
slouken@0
    74
{
slouken@1895
    75
    if (sem) {
slouken@1895
    76
        if (sem->id) {
slouken@36
    77
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@1895
    78
            CloseSynchHandle(sem->id);
slouken@36
    79
#else
slouken@1895
    80
            CloseHandle(sem->id);
slouken@36
    81
#endif
slouken@1895
    82
            sem->id = 0;
slouken@1895
    83
        }
slouken@1895
    84
        SDL_free(sem);
slouken@1895
    85
    }
slouken@0
    86
}
slouken@0
    87
slouken@1895
    88
int
slouken@1895
    89
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
slouken@0
    90
{
slouken@1895
    91
    int retval;
slouken@1895
    92
    DWORD dwMilliseconds;
slouken@0
    93
slouken@1895
    94
    if (!sem) {
slouken@1895
    95
        SDL_SetError("Passed a NULL sem");
slouken@1895
    96
        return -1;
slouken@1895
    97
    }
slouken@0
    98
slouken@1895
    99
    if (timeout == SDL_MUTEX_MAXWAIT) {
slouken@1895
   100
        dwMilliseconds = INFINITE;
slouken@1895
   101
    } else {
slouken@1895
   102
        dwMilliseconds = (DWORD) timeout;
slouken@1895
   103
    }
slouken@36
   104
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@1895
   105
    switch (WaitForSemaphoreCE(sem->id, dwMilliseconds)) {
slouken@36
   106
#else
slouken@1895
   107
    switch (WaitForSingleObject(sem->id, dwMilliseconds)) {
slouken@36
   108
#endif
slouken@1895
   109
    case WAIT_OBJECT_0:
slouken@3069
   110
        InterlockedDecrement(&sem->count);
slouken@1895
   111
        retval = 0;
slouken@1895
   112
        break;
slouken@1895
   113
    case WAIT_TIMEOUT:
slouken@1895
   114
        retval = SDL_MUTEX_TIMEDOUT;
slouken@1895
   115
        break;
slouken@1895
   116
    default:
slouken@1895
   117
        SDL_SetError("WaitForSingleObject() failed");
slouken@1895
   118
        retval = -1;
slouken@1895
   119
        break;
slouken@1895
   120
    }
slouken@1895
   121
    return retval;
slouken@0
   122
}
slouken@0
   123
slouken@1895
   124
int
slouken@1895
   125
SDL_SemTryWait(SDL_sem * sem)
slouken@0
   126
{
slouken@1895
   127
    return SDL_SemWaitTimeout(sem, 0);
slouken@0
   128
}
slouken@0
   129
slouken@1895
   130
int
slouken@1895
   131
SDL_SemWait(SDL_sem * sem)
slouken@0
   132
{
slouken@1895
   133
    return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
slouken@0
   134
}
slouken@0
   135
slouken@0
   136
/* Returns the current count of the semaphore */
slouken@1895
   137
Uint32
slouken@1895
   138
SDL_SemValue(SDL_sem * sem)
slouken@0
   139
{
slouken@1895
   140
    if (!sem) {
slouken@1895
   141
        SDL_SetError("Passed a NULL sem");
slouken@1895
   142
        return 0;
slouken@1895
   143
    }
slouken@4475
   144
    return (Uint32)sem->count;
slouken@0
   145
}
slouken@0
   146
slouken@1895
   147
int
slouken@1895
   148
SDL_SemPost(SDL_sem * sem)
slouken@0
   149
{
slouken@1895
   150
    if (!sem) {
slouken@1895
   151
        SDL_SetError("Passed a NULL sem");
slouken@1895
   152
        return -1;
slouken@1895
   153
    }
slouken@1895
   154
    /* Increase the counter in the first place, because
slouken@1895
   155
     * after a successful release the semaphore may
slouken@1895
   156
     * immediately get destroyed by another thread which
slouken@1895
   157
     * is waiting for this semaphore.
slouken@1895
   158
     */
slouken@3069
   159
    InterlockedIncrement(&sem->count);
slouken@36
   160
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@1895
   161
    if (ReleaseSemaphoreCE(sem->id, 1, NULL) == FALSE) {
slouken@36
   162
#else
slouken@1895
   163
    if (ReleaseSemaphore(sem->id, 1, NULL) == FALSE) {
slouken@36
   164
#endif
slouken@3071
   165
        InterlockedDecrement(&sem->count);      /* restore */
slouken@1895
   166
        SDL_SetError("ReleaseSemaphore() failed");
slouken@1895
   167
        return -1;
slouken@1895
   168
    }
slouken@1895
   169
    return 0;
slouken@0
   170
}
slouken@1895
   171
slouken@1895
   172
/* vi: set ts=4 sw=4 expandtab: */