src/thread/windows/SDL_syssem.c
author Sam Lantinga
Fri, 27 Jan 2017 06:05:50 -0800
changeset 10856 486aa38c6a88
parent 10737 3406a0f8b041
child 11794 c40f03dda342
permissions -rw-r--r--
Added Thrustmaster Wheel FFB entry to the list of wheels
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 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
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@0
    22
slouken@6044
    23
#if SDL_THREAD_WINDOWS
slouken@6044
    24
slouken@0
    25
/* Semaphore functions using the Win32 API */
slouken@0
    26
slouken@5090
    27
#include "../../core/windows/SDL_windows.h"
slouken@0
    28
slouken@0
    29
#include "SDL_thread.h"
slouken@0
    30
slouken@1895
    31
struct SDL_semaphore
slouken@1895
    32
{
slouken@1895
    33
    HANDLE id;
slouken@5086
    34
    LONG count;
slouken@0
    35
};
slouken@0
    36
slouken@0
    37
slouken@0
    38
/* Create a semaphore */
slouken@1895
    39
SDL_sem *
slouken@1895
    40
SDL_CreateSemaphore(Uint32 initial_value)
slouken@0
    41
{
slouken@1895
    42
    SDL_sem *sem;
slouken@0
    43
slouken@1895
    44
    /* Allocate sem memory */
slouken@1895
    45
    sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
slouken@1895
    46
    if (sem) {
slouken@1895
    47
        /* Create the semaphore, with max value 32K */
dludwig@9930
    48
#if __WINRT__
dludwig@9930
    49
        sem->id = CreateSemaphoreEx(NULL, initial_value, 32 * 1024, NULL, 0, SEMAPHORE_ALL_ACCESS);
dludwig@9930
    50
#else
slouken@1895
    51
        sem->id = CreateSemaphore(NULL, initial_value, 32 * 1024, NULL);
dludwig@9930
    52
#endif
slouken@1895
    53
        sem->count = initial_value;
slouken@1895
    54
        if (!sem->id) {
slouken@1895
    55
            SDL_SetError("Couldn't create semaphore");
slouken@1895
    56
            SDL_free(sem);
slouken@1895
    57
            sem = NULL;
slouken@1895
    58
        }
slouken@1895
    59
    } else {
slouken@1895
    60
        SDL_OutOfMemory();
slouken@1895
    61
    }
slouken@1895
    62
    return (sem);
slouken@0
    63
}
slouken@0
    64
slouken@0
    65
/* Free the semaphore */
slouken@1895
    66
void
slouken@1895
    67
SDL_DestroySemaphore(SDL_sem * sem)
slouken@0
    68
{
slouken@1895
    69
    if (sem) {
slouken@1895
    70
        if (sem->id) {
slouken@1895
    71
            CloseHandle(sem->id);
slouken@1895
    72
            sem->id = 0;
slouken@1895
    73
        }
slouken@1895
    74
        SDL_free(sem);
slouken@1895
    75
    }
slouken@0
    76
}
slouken@0
    77
slouken@1895
    78
int
slouken@1895
    79
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
slouken@0
    80
{
slouken@1895
    81
    int retval;
slouken@1895
    82
    DWORD dwMilliseconds;
slouken@0
    83
slouken@1895
    84
    if (!sem) {
icculus@7037
    85
        return SDL_SetError("Passed a NULL sem");
slouken@1895
    86
    }
slouken@0
    87
slouken@1895
    88
    if (timeout == SDL_MUTEX_MAXWAIT) {
slouken@1895
    89
        dwMilliseconds = INFINITE;
slouken@1895
    90
    } else {
slouken@1895
    91
        dwMilliseconds = (DWORD) timeout;
slouken@1895
    92
    }
dludwig@9930
    93
#if __WINRT__
dludwig@9930
    94
    switch (WaitForSingleObjectEx(sem->id, dwMilliseconds, FALSE)) {
dludwig@9930
    95
#else
slouken@1895
    96
    switch (WaitForSingleObject(sem->id, dwMilliseconds)) {
dludwig@9930
    97
#endif
slouken@1895
    98
    case WAIT_OBJECT_0:
slouken@3069
    99
        InterlockedDecrement(&sem->count);
slouken@1895
   100
        retval = 0;
slouken@1895
   101
        break;
slouken@1895
   102
    case WAIT_TIMEOUT:
slouken@1895
   103
        retval = SDL_MUTEX_TIMEDOUT;
slouken@1895
   104
        break;
slouken@1895
   105
    default:
icculus@7037
   106
        retval = SDL_SetError("WaitForSingleObject() failed");
slouken@1895
   107
        break;
slouken@1895
   108
    }
slouken@1895
   109
    return retval;
slouken@0
   110
}
slouken@0
   111
slouken@1895
   112
int
slouken@1895
   113
SDL_SemTryWait(SDL_sem * sem)
slouken@0
   114
{
slouken@1895
   115
    return SDL_SemWaitTimeout(sem, 0);
slouken@0
   116
}
slouken@0
   117
slouken@1895
   118
int
slouken@1895
   119
SDL_SemWait(SDL_sem * sem)
slouken@0
   120
{
slouken@1895
   121
    return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
slouken@0
   122
}
slouken@0
   123
slouken@0
   124
/* Returns the current count of the semaphore */
slouken@1895
   125
Uint32
slouken@1895
   126
SDL_SemValue(SDL_sem * sem)
slouken@0
   127
{
slouken@1895
   128
    if (!sem) {
slouken@1895
   129
        SDL_SetError("Passed a NULL sem");
slouken@1895
   130
        return 0;
slouken@1895
   131
    }
slouken@4475
   132
    return (Uint32)sem->count;
slouken@0
   133
}
slouken@0
   134
slouken@1895
   135
int
slouken@1895
   136
SDL_SemPost(SDL_sem * sem)
slouken@0
   137
{
slouken@1895
   138
    if (!sem) {
icculus@7037
   139
        return SDL_SetError("Passed a NULL sem");
slouken@1895
   140
    }
slouken@1895
   141
    /* Increase the counter in the first place, because
slouken@1895
   142
     * after a successful release the semaphore may
slouken@1895
   143
     * immediately get destroyed by another thread which
slouken@1895
   144
     * is waiting for this semaphore.
slouken@1895
   145
     */
slouken@3069
   146
    InterlockedIncrement(&sem->count);
slouken@1895
   147
    if (ReleaseSemaphore(sem->id, 1, NULL) == FALSE) {
slouken@3071
   148
        InterlockedDecrement(&sem->count);      /* restore */
icculus@7037
   149
        return SDL_SetError("ReleaseSemaphore() failed");
slouken@1895
   150
    }
slouken@1895
   151
    return 0;
slouken@0
   152
}
slouken@1895
   153
slouken@6044
   154
#endif /* SDL_THREAD_WINDOWS */
slouken@6044
   155
slouken@1895
   156
/* vi: set ts=4 sw=4 expandtab: */