src/thread/pthread/SDL_syssem.c
author Philipp Wiesemann
Thu, 30 May 2013 12:23:36 +0200
changeset 7242 96f4133dfafa
parent 7037 3fedf1f25b94
child 7722 ea5565d856a8
permissions -rw-r--r--
Added deactivated loading of SDL2_net to Java file.
slouken@1361
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@1361
     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@1361
     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@1361
    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@1361
    20
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@1361
    22
slouken@5106
    23
#include <errno.h>
slouken@1361
    24
#include <pthread.h>
slouken@1361
    25
#include <semaphore.h>
icculus@5981
    26
#include <sys/time.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) {
icculus@7037
    75
        return SDL_SetError("Passed a NULL semaphore");
slouken@1895
    76
    }
slouken@1895
    77
    retval = SDL_MUTEX_TIMEDOUT;
slouken@1895
    78
    if (sem_trywait(&sem->sem) == 0) {
slouken@1895
    79
        retval = 0;
slouken@1895
    80
    }
slouken@1895
    81
    return retval;
slouken@1361
    82
}
slouken@1361
    83
slouken@1895
    84
int
slouken@1895
    85
SDL_SemWait(SDL_sem * sem)
slouken@1361
    86
{
slouken@1895
    87
    int retval;
slouken@1361
    88
slouken@1895
    89
    if (!sem) {
icculus@7037
    90
        return SDL_SetError("Passed a NULL semaphore");
slouken@1895
    91
    }
slouken@1361
    92
slouken@1895
    93
    retval = sem_wait(&sem->sem);
slouken@1895
    94
    if (retval < 0) {
icculus@7037
    95
        retval = SDL_SetError("sem_wait() failed");
slouken@1895
    96
    }
slouken@1895
    97
    return retval;
slouken@1361
    98
}
slouken@1361
    99
slouken@1895
   100
int
slouken@1895
   101
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
slouken@1361
   102
{
slouken@1895
   103
    int retval;
slouken@6219
   104
#ifdef HAVE_SEM_TIMEDWAIT
slouken@5106
   105
    struct timeval now;
slouken@5106
   106
    struct timespec ts_timeout;
slouken@6219
   107
#else
slouken@6219
   108
    Uint32 end;
slouken@6219
   109
#endif
slouken@1361
   110
slouken@1895
   111
    if (!sem) {
icculus@7037
   112
        return SDL_SetError("Passed a NULL semaphore");
slouken@1895
   113
    }
slouken@1361
   114
slouken@1895
   115
    /* Try the easy cases first */
slouken@1895
   116
    if (timeout == 0) {
slouken@1895
   117
        return SDL_SemTryWait(sem);
slouken@1895
   118
    }
slouken@1895
   119
    if (timeout == SDL_MUTEX_MAXWAIT) {
slouken@1895
   120
        return SDL_SemWait(sem);
slouken@1895
   121
    }
slouken@1361
   122
slouken@6219
   123
#ifdef HAVE_SEM_TIMEDWAIT
slouken@5106
   124
    /* Setup the timeout. sem_timedwait doesn't wait for
slouken@5106
   125
    * a lapse of time, but until we reach a certain time.
slouken@5106
   126
    * This time is now plus the timeout.
slouken@5106
   127
    */
slouken@5106
   128
    gettimeofday(&now, NULL);
slouken@5106
   129
slouken@5106
   130
    /* Add our timeout to current time */
slouken@5106
   131
    now.tv_usec += (timeout % 1000) * 1000;
slouken@5106
   132
    now.tv_sec += timeout / 1000;
slouken@5106
   133
slouken@5106
   134
    /* Wrap the second if needed */
slouken@5106
   135
    if ( now.tv_usec >= 1000000 ) {
slouken@5106
   136
        now.tv_usec -= 1000000;
slouken@5106
   137
        now.tv_sec ++;
slouken@5106
   138
    }
slouken@5106
   139
slouken@5106
   140
    /* Convert to timespec */
slouken@5106
   141
    ts_timeout.tv_sec = now.tv_sec;
slouken@5106
   142
    ts_timeout.tv_nsec = now.tv_usec * 1000;
slouken@5106
   143
slouken@5106
   144
    /* Wait. */
slouken@1895
   145
    do {
slouken@5106
   146
        retval = sem_timedwait(&sem->sem, &ts_timeout);
slouken@5106
   147
    } while (retval < 0 && errno == EINTR);
slouken@5106
   148
slouken@5106
   149
    if (retval < 0) {
slouken@6298
   150
        if (errno == ETIMEDOUT) {
slouken@6298
   151
            retval = SDL_MUTEX_TIMEDOUT;
slouken@6298
   152
        } else {
slouken@6298
   153
            SDL_SetError(strerror(errno));
slouken@6298
   154
        }
slouken@5106
   155
    }
slouken@6219
   156
#else
slouken@6219
   157
    end = SDL_GetTicks() + timeout;
slouken@6219
   158
    while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) {
slouken@6219
   159
        if ((SDL_GetTicks() - end) >= 0) {
slouken@6219
   160
            break;
slouken@6219
   161
        }
slouken@6219
   162
        SDL_Delay(0);
slouken@6219
   163
    }
slouken@6219
   164
#endif /* HAVE_SEM_TIMEDWAIT */
slouken@1361
   165
slouken@1895
   166
    return retval;
slouken@1361
   167
}
slouken@1361
   168
slouken@1895
   169
Uint32
slouken@1895
   170
SDL_SemValue(SDL_sem * sem)
slouken@1361
   171
{
slouken@1895
   172
    int ret = 0;
slouken@1895
   173
    if (sem) {
slouken@1895
   174
        sem_getvalue(&sem->sem, &ret);
slouken@1895
   175
        if (ret < 0) {
slouken@1895
   176
            ret = 0;
slouken@1895
   177
        }
slouken@1895
   178
    }
slouken@1895
   179
    return (Uint32) ret;
slouken@1361
   180
}
slouken@1361
   181
slouken@1895
   182
int
slouken@1895
   183
SDL_SemPost(SDL_sem * sem)
slouken@1361
   184
{
slouken@1895
   185
    int retval;
slouken@1361
   186
slouken@1895
   187
    if (!sem) {
icculus@7037
   188
        return SDL_SetError("Passed a NULL semaphore");
slouken@1895
   189
    }
slouken@1361
   190
slouken@1895
   191
    retval = sem_post(&sem->sem);
slouken@1895
   192
    if (retval < 0) {
slouken@1895
   193
        SDL_SetError("sem_post() failed");
slouken@1895
   194
    }
slouken@1895
   195
    return retval;
slouken@1361
   196
}
slouken@1627
   197
slouken@1627
   198
#endif /* __MACOSX__ */
slouken@1895
   199
/* vi: set ts=4 sw=4 expandtab: */