src/thread/generic/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Feb 2014 00:53:27 -0800
changeset 8149 681eb46b8ac4
parent 8093 b43765095a6f
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Fixed bug 2374 - Update copyright for 2014...

Is it that time already??
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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@0
    23
/* An implementation of semaphores using mutexes and condition variables */
slouken@0
    24
slouken@0
    25
#include "SDL_timer.h"
slouken@0
    26
#include "SDL_thread.h"
slouken@0
    27
#include "SDL_systhread_c.h"
slouken@0
    28
slouken@0
    29
slouken@1361
    30
#if SDL_THREADS_DISABLED
slouken@0
    31
slouken@1895
    32
SDL_sem *
slouken@1895
    33
SDL_CreateSemaphore(Uint32 initial_value)
slouken@0
    34
{
slouken@7307
    35
    SDL_SetError("SDL not built with thread support");
slouken@1895
    36
    return (SDL_sem *) 0;
slouken@0
    37
}
slouken@0
    38
slouken@1895
    39
void
slouken@1895
    40
SDL_DestroySemaphore(SDL_sem * sem)
slouken@0
    41
{
slouken@0
    42
}
slouken@0
    43
slouken@1895
    44
int
slouken@1895
    45
SDL_SemTryWait(SDL_sem * sem)
slouken@0
    46
{
slouken@7307
    47
    return SDL_SetError("SDL not built with thread support");
slouken@0
    48
}
slouken@0
    49
slouken@1895
    50
int
slouken@1895
    51
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
slouken@0
    52
{
slouken@7307
    53
    return SDL_SetError("SDL not built with thread support");
slouken@0
    54
}
slouken@0
    55
slouken@1895
    56
int
slouken@1895
    57
SDL_SemWait(SDL_sem * sem)
slouken@0
    58
{
slouken@7307
    59
    return SDL_SetError("SDL not built with thread support");
slouken@0
    60
}
slouken@0
    61
slouken@1895
    62
Uint32
slouken@1895
    63
SDL_SemValue(SDL_sem * sem)
slouken@0
    64
{
slouken@1895
    65
    return 0;
slouken@0
    66
}
slouken@0
    67
slouken@1895
    68
int
slouken@1895
    69
SDL_SemPost(SDL_sem * sem)
slouken@0
    70
{
slouken@7307
    71
    return SDL_SetError("SDL not built with thread support");
slouken@0
    72
}
slouken@0
    73
slouken@0
    74
#else
slouken@0
    75
slouken@0
    76
struct SDL_semaphore
slouken@0
    77
{
slouken@1895
    78
    Uint32 count;
slouken@1895
    79
    Uint32 waiters_count;
slouken@1895
    80
    SDL_mutex *count_lock;
slouken@1895
    81
    SDL_cond *count_nonzero;
slouken@0
    82
};
slouken@0
    83
slouken@1895
    84
SDL_sem *
slouken@1895
    85
SDL_CreateSemaphore(Uint32 initial_value)
slouken@0
    86
{
slouken@1895
    87
    SDL_sem *sem;
slouken@0
    88
slouken@1895
    89
    sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
slouken@1895
    90
    if (!sem) {
slouken@1895
    91
        SDL_OutOfMemory();
slouken@1895
    92
        return NULL;
slouken@1895
    93
    }
slouken@1895
    94
    sem->count = initial_value;
slouken@1895
    95
    sem->waiters_count = 0;
slouken@0
    96
slouken@1895
    97
    sem->count_lock = SDL_CreateMutex();
slouken@1895
    98
    sem->count_nonzero = SDL_CreateCond();
slouken@1895
    99
    if (!sem->count_lock || !sem->count_nonzero) {
slouken@1895
   100
        SDL_DestroySemaphore(sem);
slouken@1895
   101
        return NULL;
slouken@1895
   102
    }
slouken@0
   103
slouken@1895
   104
    return sem;
slouken@0
   105
}
slouken@0
   106
slouken@0
   107
/* WARNING:
slouken@0
   108
   You cannot call this function when another thread is using the semaphore.
slouken@0
   109
*/
slouken@1895
   110
void
slouken@1895
   111
SDL_DestroySemaphore(SDL_sem * sem)
slouken@0
   112
{
slouken@1895
   113
    if (sem) {
slouken@1895
   114
        sem->count = 0xFFFFFFFF;
slouken@1895
   115
        while (sem->waiters_count > 0) {
slouken@1895
   116
            SDL_CondSignal(sem->count_nonzero);
slouken@1895
   117
            SDL_Delay(10);
slouken@1895
   118
        }
slouken@1895
   119
        SDL_DestroyCond(sem->count_nonzero);
slouken@1895
   120
        if (sem->count_lock) {
slouken@6977
   121
            SDL_LockMutex(sem->count_lock);
slouken@6977
   122
            SDL_UnlockMutex(sem->count_lock);
slouken@1895
   123
            SDL_DestroyMutex(sem->count_lock);
slouken@1895
   124
        }
slouken@1895
   125
        SDL_free(sem);
slouken@1895
   126
    }
slouken@0
   127
}
slouken@0
   128
slouken@1895
   129
int
slouken@1895
   130
SDL_SemTryWait(SDL_sem * sem)
slouken@0
   131
{
slouken@1895
   132
    int retval;
slouken@0
   133
slouken@1895
   134
    if (!sem) {
icculus@7037
   135
        return SDL_SetError("Passed a NULL semaphore");
slouken@1895
   136
    }
slouken@0
   137
slouken@1895
   138
    retval = SDL_MUTEX_TIMEDOUT;
slouken@1895
   139
    SDL_LockMutex(sem->count_lock);
slouken@1895
   140
    if (sem->count > 0) {
slouken@1895
   141
        --sem->count;
slouken@1895
   142
        retval = 0;
slouken@1895
   143
    }
slouken@1895
   144
    SDL_UnlockMutex(sem->count_lock);
slouken@0
   145
slouken@1895
   146
    return retval;
slouken@0
   147
}
slouken@0
   148
slouken@1895
   149
int
slouken@1895
   150
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
slouken@0
   151
{
slouken@1895
   152
    int retval;
slouken@0
   153
slouken@1895
   154
    if (!sem) {
icculus@7037
   155
        return SDL_SetError("Passed a NULL semaphore");
slouken@1895
   156
    }
slouken@0
   157
slouken@1895
   158
    /* A timeout of 0 is an easy case */
slouken@1895
   159
    if (timeout == 0) {
slouken@1895
   160
        return SDL_SemTryWait(sem);
slouken@1895
   161
    }
slouken@0
   162
slouken@1895
   163
    SDL_LockMutex(sem->count_lock);
slouken@1895
   164
    ++sem->waiters_count;
slouken@1895
   165
    retval = 0;
slouken@1895
   166
    while ((sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT)) {
slouken@1895
   167
        retval = SDL_CondWaitTimeout(sem->count_nonzero,
slouken@1895
   168
                                     sem->count_lock, timeout);
slouken@1895
   169
    }
slouken@1895
   170
    --sem->waiters_count;
slouken@3287
   171
    if (retval == 0) {
slouken@3287
   172
        --sem->count;
slouken@3287
   173
    }
slouken@1895
   174
    SDL_UnlockMutex(sem->count_lock);
slouken@0
   175
slouken@1895
   176
    return retval;
slouken@0
   177
}
slouken@0
   178
slouken@1895
   179
int
slouken@1895
   180
SDL_SemWait(SDL_sem * sem)
slouken@0
   181
{
slouken@1895
   182
    return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
slouken@0
   183
}
slouken@0
   184
slouken@1895
   185
Uint32
slouken@1895
   186
SDL_SemValue(SDL_sem * sem)
slouken@0
   187
{
slouken@1895
   188
    Uint32 value;
slouken@1895
   189
slouken@1895
   190
    value = 0;
slouken@1895
   191
    if (sem) {
slouken@1895
   192
        SDL_LockMutex(sem->count_lock);
slouken@1895
   193
        value = sem->count;
slouken@1895
   194
        SDL_UnlockMutex(sem->count_lock);
slouken@1895
   195
    }
slouken@1895
   196
    return value;
slouken@0
   197
}
slouken@0
   198
slouken@1895
   199
int
slouken@1895
   200
SDL_SemPost(SDL_sem * sem)
slouken@0
   201
{
slouken@1895
   202
    if (!sem) {
icculus@7037
   203
        return SDL_SetError("Passed a NULL semaphore");
slouken@1895
   204
    }
slouken@0
   205
slouken@1895
   206
    SDL_LockMutex(sem->count_lock);
slouken@1895
   207
    if (sem->waiters_count > 0) {
slouken@1895
   208
        SDL_CondSignal(sem->count_nonzero);
slouken@1895
   209
    }
slouken@1895
   210
    ++sem->count;
slouken@1895
   211
    SDL_UnlockMutex(sem->count_lock);
slouken@0
   212
slouken@1895
   213
    return 0;
slouken@0
   214
}
slouken@0
   215
slouken@1361
   216
#endif /* SDL_THREADS_DISABLED */
slouken@1895
   217
/* vi: set ts=4 sw=4 expandtab: */