src/thread/pthread/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 20 Feb 2012 23:51:53 -0500
changeset 6298 9110462bfc57
parent 6219 8e94af244f2c
child 6885 700f1b25f77f
permissions -rwxr-xr-x
Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out deraj 2012-02-19 19:01:08 PST Fix to treat ETIMEDOUT as a time out instead of an error (and update the test)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     2
  Simple DirectMedia Layer
6138
4c64952a58fb Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 5981
diff changeset
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     7
  arising from the use of this software.
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    11
  freely, subject to the following restrictions:
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1361
diff changeset
    21
#include "SDL_config.h"
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
5106
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
    23
#include <errno.h>
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
#include <pthread.h>
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
#include <semaphore.h>
5981
75caa8a7d559 Fixed a whole slew of compiler warnings that -Wall exposed.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    26
#include <sys/time.h>
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
#include "SDL_thread.h"
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
#include "SDL_timer.h"
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
/* Wrapper around POSIX 1003.1b semaphores */
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2735
diff changeset
    33
#if defined(__MACOSX__) || defined(__IPHONEOS__)
1627
aee7ea396f59 Fixed bug #179
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
    34
/* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
aee7ea396f59 Fixed bug #179
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
    35
#include "../generic/SDL_syssem.c"
aee7ea396f59 Fixed bug #179
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
    36
#else
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    38
struct SDL_semaphore
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    39
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    40
    sem_t sem;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
};
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
/* Create a semaphore, initialized with value */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    44
SDL_sem *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    45
SDL_CreateSemaphore(Uint32 initial_value)
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    47
    SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    48
    if (sem) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    49
        if (sem_init(&sem->sem, 0, initial_value) < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    50
            SDL_SetError("sem_init() failed");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    51
            SDL_free(sem);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    52
            sem = NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    53
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    54
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    55
        SDL_OutOfMemory();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    56
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    57
    return sem;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    60
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    61
SDL_DestroySemaphore(SDL_sem * sem)
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    63
    if (sem) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    64
        sem_destroy(&sem->sem);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    65
        SDL_free(sem);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    66
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    69
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    70
SDL_SemTryWait(SDL_sem * sem)
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    72
    int retval;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    74
    if (!sem) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    75
        SDL_SetError("Passed a NULL semaphore");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    76
        return -1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    77
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    78
    retval = SDL_MUTEX_TIMEDOUT;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    79
    if (sem_trywait(&sem->sem) == 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    80
        retval = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    81
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    82
    return retval;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    85
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    86
SDL_SemWait(SDL_sem * sem)
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    88
    int retval;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    89
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    90
    if (!sem) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    91
        SDL_SetError("Passed a NULL semaphore");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    92
        return -1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    93
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    94
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    95
    retval = sem_wait(&sem->sem);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    96
    if (retval < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    97
        SDL_SetError("sem_wait() failed");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    98
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
    99
    return retval;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   100
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   101
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   102
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   103
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   104
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   105
    int retval;
6219
8e94af244f2c Check for sem_timedwait(), which isn't available on some systems (including OpenBSD
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   106
#ifdef HAVE_SEM_TIMEDWAIT
5106
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   107
    struct timeval now;
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   108
    struct timespec ts_timeout;
6219
8e94af244f2c Check for sem_timedwait(), which isn't available on some systems (including OpenBSD
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   109
#else
8e94af244f2c Check for sem_timedwait(), which isn't available on some systems (including OpenBSD
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   110
    Uint32 end;
8e94af244f2c Check for sem_timedwait(), which isn't available on some systems (including OpenBSD
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   111
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   113
    if (!sem) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   114
        SDL_SetError("Passed a NULL semaphore");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   115
        return -1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   116
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   118
    /* Try the easy cases first */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   119
    if (timeout == 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   120
        return SDL_SemTryWait(sem);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   121
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   122
    if (timeout == SDL_MUTEX_MAXWAIT) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   123
        return SDL_SemWait(sem);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   124
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   125
6219
8e94af244f2c Check for sem_timedwait(), which isn't available on some systems (including OpenBSD
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   126
#ifdef HAVE_SEM_TIMEDWAIT
5106
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   127
    /* Setup the timeout. sem_timedwait doesn't wait for
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   128
    * a lapse of time, but until we reach a certain time.
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   129
    * This time is now plus the timeout.
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   130
    */
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   131
    gettimeofday(&now, NULL);
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   132
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   133
    /* Add our timeout to current time */
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   134
    now.tv_usec += (timeout % 1000) * 1000;
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   135
    now.tv_sec += timeout / 1000;
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   136
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   137
    /* Wrap the second if needed */
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   138
    if ( now.tv_usec >= 1000000 ) {
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   139
        now.tv_usec -= 1000000;
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   140
        now.tv_sec ++;
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   141
    }
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   142
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   143
    /* Convert to timespec */
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   144
    ts_timeout.tv_sec = now.tv_sec;
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   145
    ts_timeout.tv_nsec = now.tv_usec * 1000;
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   146
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   147
    /* Wait. */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   148
    do {
5106
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   149
        retval = sem_timedwait(&sem->sem, &ts_timeout);
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   150
    } while (retval < 0 && errno == EINTR);
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   151
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   152
    if (retval < 0) {
6298
9110462bfc57 Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out
Sam Lantinga <slouken@libsdl.org>
parents: 6219
diff changeset
   153
        if (errno == ETIMEDOUT) {
9110462bfc57 Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out
Sam Lantinga <slouken@libsdl.org>
parents: 6219
diff changeset
   154
            retval = SDL_MUTEX_TIMEDOUT;
9110462bfc57 Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out
Sam Lantinga <slouken@libsdl.org>
parents: 6219
diff changeset
   155
        } else {
9110462bfc57 Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out
Sam Lantinga <slouken@libsdl.org>
parents: 6219
diff changeset
   156
            SDL_SetError(strerror(errno));
9110462bfc57 Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out
Sam Lantinga <slouken@libsdl.org>
parents: 6219
diff changeset
   157
        }
5106
d547877e355e Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   158
    }
6219
8e94af244f2c Check for sem_timedwait(), which isn't available on some systems (including OpenBSD
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   159
#else
8e94af244f2c Check for sem_timedwait(), which isn't available on some systems (including OpenBSD
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   160
    end = SDL_GetTicks() + timeout;
8e94af244f2c Check for sem_timedwait(), which isn't available on some systems (including OpenBSD
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   161
    while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) {
8e94af244f2c Check for sem_timedwait(), which isn't available on some systems (including OpenBSD
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   162
        if ((SDL_GetTicks() - end) >= 0) {
8e94af244f2c Check for sem_timedwait(), which isn't available on some systems (including OpenBSD
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   163
            break;
8e94af244f2c Check for sem_timedwait(), which isn't available on some systems (including OpenBSD
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   164
        }
8e94af244f2c Check for sem_timedwait(), which isn't available on some systems (including OpenBSD
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   165
        SDL_Delay(0);
8e94af244f2c Check for sem_timedwait(), which isn't available on some systems (including OpenBSD
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   166
    }
8e94af244f2c Check for sem_timedwait(), which isn't available on some systems (including OpenBSD
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   167
#endif /* HAVE_SEM_TIMEDWAIT */
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   168
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   169
    return retval;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   170
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   171
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   172
Uint32
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   173
SDL_SemValue(SDL_sem * sem)
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   174
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   175
    int ret = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   176
    if (sem) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   177
        sem_getvalue(&sem->sem, &ret);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   178
        if (ret < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   179
            ret = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   180
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   181
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   182
    return (Uint32) ret;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   183
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   185
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   186
SDL_SemPost(SDL_sem * sem)
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   187
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   188
    int retval;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   189
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   190
    if (!sem) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   191
        SDL_SetError("Passed a NULL semaphore");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   192
        return -1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   193
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   194
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   195
    retval = sem_post(&sem->sem);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   196
    if (retval < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   197
        SDL_SetError("sem_post() failed");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   198
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   199
    return retval;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   200
}
1627
aee7ea396f59 Fixed bug #179
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
   201
aee7ea396f59 Fixed bug #179
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
   202
#endif /* __MACOSX__ */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1851
diff changeset
   203
/* vi: set ts=4 sw=4 expandtab: */