src/thread/pthread/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Feb 2011 22:37:15 -0800
changeset 5262 b530ef003506
parent 5106 d547877e355e
child 5535 96594ac5fd1a
permissions -rw-r--r--
Happy 2011! :)
slouken@1361
     1
/*
slouken@1361
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 Sam Lantinga
slouken@1361
     4
slouken@1361
     5
    This library is free software; you can redistribute it and/or
slouken@1361
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1361
     7
    License as published by the Free Software Foundation; either
slouken@1361
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1361
     9
slouken@1361
    10
    This library is distributed in the hope that it will be useful,
slouken@1361
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1361
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1361
    13
    Lesser General Public License for more details.
slouken@1361
    14
slouken@1361
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1361
    16
    License along with this library; if not, write to the Free Software
slouken@1361
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1361
    18
slouken@1361
    19
    Sam Lantinga
slouken@1361
    20
    slouken@libsdl.org
slouken@1361
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@1361
    23
slouken@5106
    24
#include <errno.h>
slouken@1361
    25
#include <pthread.h>
slouken@1361
    26
#include <semaphore.h>
slouken@1361
    27
slouken@1361
    28
#include "SDL_thread.h"
slouken@1361
    29
#include "SDL_timer.h"
slouken@1361
    30
slouken@1361
    31
/* Wrapper around POSIX 1003.1b semaphores */
slouken@1361
    32
slouken@2765
    33
#if defined(__MACOSX__) || defined(__IPHONEOS__)
slouken@1627
    34
/* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
slouken@1627
    35
#include "../generic/SDL_syssem.c"
slouken@1627
    36
#else
slouken@1361
    37
slouken@1895
    38
struct SDL_semaphore
slouken@1895
    39
{
slouken@1895
    40
    sem_t sem;
slouken@1361
    41
};
slouken@1361
    42
slouken@1361
    43
/* Create a semaphore, initialized with value */
slouken@1895
    44
SDL_sem *
slouken@1895
    45
SDL_CreateSemaphore(Uint32 initial_value)
slouken@1361
    46
{
slouken@1895
    47
    SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
slouken@1895
    48
    if (sem) {
slouken@1895
    49
        if (sem_init(&sem->sem, 0, initial_value) < 0) {
slouken@1895
    50
            SDL_SetError("sem_init() failed");
slouken@1895
    51
            SDL_free(sem);
slouken@1895
    52
            sem = NULL;
slouken@1895
    53
        }
slouken@1895
    54
    } else {
slouken@1895
    55
        SDL_OutOfMemory();
slouken@1895
    56
    }
slouken@1895
    57
    return sem;
slouken@1361
    58
}
slouken@1361
    59
slouken@1895
    60
void
slouken@1895
    61
SDL_DestroySemaphore(SDL_sem * sem)
slouken@1361
    62
{
slouken@1895
    63
    if (sem) {
slouken@1895
    64
        sem_destroy(&sem->sem);
slouken@1895
    65
        SDL_free(sem);
slouken@1895
    66
    }
slouken@1361
    67
}
slouken@1361
    68
slouken@1895
    69
int
slouken@1895
    70
SDL_SemTryWait(SDL_sem * sem)
slouken@1361
    71
{
slouken@1895
    72
    int retval;
slouken@1361
    73
slouken@1895
    74
    if (!sem) {
slouken@1895
    75
        SDL_SetError("Passed a NULL semaphore");
slouken@1895
    76
        return -1;
slouken@1895
    77
    }
slouken@1895
    78
    retval = SDL_MUTEX_TIMEDOUT;
slouken@1895
    79
    if (sem_trywait(&sem->sem) == 0) {
slouken@1895
    80
        retval = 0;
slouken@1895
    81
    }
slouken@1895
    82
    return retval;
slouken@1361
    83
}
slouken@1361
    84
slouken@1895
    85
int
slouken@1895
    86
SDL_SemWait(SDL_sem * sem)
slouken@1361
    87
{
slouken@1895
    88
    int retval;
slouken@1361
    89
slouken@1895
    90
    if (!sem) {
slouken@1895
    91
        SDL_SetError("Passed a NULL semaphore");
slouken@1895
    92
        return -1;
slouken@1895
    93
    }
slouken@1361
    94
slouken@1895
    95
    retval = sem_wait(&sem->sem);
slouken@1895
    96
    if (retval < 0) {
slouken@1895
    97
        SDL_SetError("sem_wait() failed");
slouken@1895
    98
    }
slouken@1895
    99
    return retval;
slouken@1361
   100
}
slouken@1361
   101
slouken@1895
   102
int
slouken@1895
   103
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
slouken@1361
   104
{
slouken@1895
   105
    int retval;
slouken@5106
   106
    struct timeval now;
slouken@5106
   107
    struct timespec ts_timeout;
slouken@1361
   108
slouken@1895
   109
    if (!sem) {
slouken@1895
   110
        SDL_SetError("Passed a NULL semaphore");
slouken@1895
   111
        return -1;
slouken@1895
   112
    }
slouken@1361
   113
slouken@1895
   114
    /* Try the easy cases first */
slouken@1895
   115
    if (timeout == 0) {
slouken@1895
   116
        return SDL_SemTryWait(sem);
slouken@1895
   117
    }
slouken@1895
   118
    if (timeout == SDL_MUTEX_MAXWAIT) {
slouken@1895
   119
        return SDL_SemWait(sem);
slouken@1895
   120
    }
slouken@1361
   121
slouken@5106
   122
    /* Setup the timeout. sem_timedwait doesn't wait for
slouken@5106
   123
    * a lapse of time, but until we reach a certain time.
slouken@5106
   124
    * This time is now plus the timeout.
slouken@5106
   125
    */
slouken@5106
   126
    gettimeofday(&now, NULL);
slouken@5106
   127
slouken@5106
   128
    /* Add our timeout to current time */
slouken@5106
   129
    now.tv_usec += (timeout % 1000) * 1000;
slouken@5106
   130
    now.tv_sec += timeout / 1000;
slouken@5106
   131
slouken@5106
   132
    /* Wrap the second if needed */
slouken@5106
   133
    if ( now.tv_usec >= 1000000 ) {
slouken@5106
   134
        now.tv_usec -= 1000000;
slouken@5106
   135
        now.tv_sec ++;
slouken@5106
   136
    }
slouken@5106
   137
slouken@5106
   138
    /* Convert to timespec */
slouken@5106
   139
    ts_timeout.tv_sec = now.tv_sec;
slouken@5106
   140
    ts_timeout.tv_nsec = now.tv_usec * 1000;
slouken@5106
   141
slouken@5106
   142
    /* Wait. */
slouken@1895
   143
    do {
slouken@5106
   144
        retval = sem_timedwait(&sem->sem, &ts_timeout);
slouken@5106
   145
    } while (retval < 0 && errno == EINTR);
slouken@5106
   146
slouken@5106
   147
    if (retval < 0) {
slouken@5106
   148
        SDL_SetError("sem_timedwait() failed");
slouken@5106
   149
    }
slouken@1361
   150
slouken@1895
   151
    return retval;
slouken@1361
   152
}
slouken@1361
   153
slouken@1895
   154
Uint32
slouken@1895
   155
SDL_SemValue(SDL_sem * sem)
slouken@1361
   156
{
slouken@1895
   157
    int ret = 0;
slouken@1895
   158
    if (sem) {
slouken@1895
   159
        sem_getvalue(&sem->sem, &ret);
slouken@1895
   160
        if (ret < 0) {
slouken@1895
   161
            ret = 0;
slouken@1895
   162
        }
slouken@1895
   163
    }
slouken@1895
   164
    return (Uint32) ret;
slouken@1361
   165
}
slouken@1361
   166
slouken@1895
   167
int
slouken@1895
   168
SDL_SemPost(SDL_sem * sem)
slouken@1361
   169
{
slouken@1895
   170
    int retval;
slouken@1361
   171
slouken@1895
   172
    if (!sem) {
slouken@1895
   173
        SDL_SetError("Passed a NULL semaphore");
slouken@1895
   174
        return -1;
slouken@1895
   175
    }
slouken@1361
   176
slouken@1895
   177
    retval = sem_post(&sem->sem);
slouken@1895
   178
    if (retval < 0) {
slouken@1895
   179
        SDL_SetError("sem_post() failed");
slouken@1895
   180
    }
slouken@1895
   181
    return retval;
slouken@1361
   182
}
slouken@1627
   183
slouken@1627
   184
#endif /* __MACOSX__ */
slouken@1895
   185
/* vi: set ts=4 sw=4 expandtab: */