src/thread/pthread/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1630 eba962f9d603
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
slouken@1361
     1
/*
slouken@1361
     2
    SDL - Simple DirectMedia Layer
slouken@1361
     3
    Copyright (C) 1997-2006 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@1361
    24
#include <pthread.h>
slouken@1361
    25
#include <semaphore.h>
slouken@1361
    26
slouken@1361
    27
#include "SDL_thread.h"
slouken@1361
    28
#include "SDL_timer.h"
slouken@1361
    29
slouken@1361
    30
/* Wrapper around POSIX 1003.1b semaphores */
slouken@1361
    31
slouken@1402
    32
#ifdef __MACOSX__
slouken@1627
    33
/* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
slouken@1627
    34
#include "../generic/SDL_syssem.c"
slouken@1627
    35
#else
slouken@1361
    36
slouken@1662
    37
struct SDL_semaphore
slouken@1662
    38
{
slouken@1662
    39
    sem_t sem;
slouken@1361
    40
};
slouken@1361
    41
slouken@1361
    42
/* Create a semaphore, initialized with value */
slouken@1662
    43
SDL_sem *
slouken@1662
    44
SDL_CreateSemaphore (Uint32 initial_value)
slouken@1361
    45
{
slouken@1662
    46
    SDL_sem *sem = (SDL_sem *) SDL_malloc (sizeof (SDL_sem));
slouken@1662
    47
    if (sem) {
slouken@1662
    48
        if (sem_init (&sem->sem, 0, initial_value) < 0) {
slouken@1662
    49
            SDL_SetError ("sem_init() failed");
slouken@1662
    50
            SDL_free (sem);
slouken@1662
    51
            sem = NULL;
slouken@1662
    52
        }
slouken@1662
    53
    } else {
slouken@1662
    54
        SDL_OutOfMemory ();
slouken@1662
    55
    }
slouken@1662
    56
    return sem;
slouken@1361
    57
}
slouken@1361
    58
slouken@1662
    59
void
slouken@1662
    60
SDL_DestroySemaphore (SDL_sem * sem)
slouken@1361
    61
{
slouken@1662
    62
    if (sem) {
slouken@1662
    63
        sem_destroy (&sem->sem);
slouken@1662
    64
        SDL_free (sem);
slouken@1662
    65
    }
slouken@1361
    66
}
slouken@1361
    67
slouken@1662
    68
int
slouken@1662
    69
SDL_SemTryWait (SDL_sem * sem)
slouken@1361
    70
{
slouken@1662
    71
    int retval;
slouken@1361
    72
slouken@1662
    73
    if (!sem) {
slouken@1662
    74
        SDL_SetError ("Passed a NULL semaphore");
slouken@1662
    75
        return -1;
slouken@1662
    76
    }
slouken@1662
    77
    retval = SDL_MUTEX_TIMEDOUT;
slouken@1662
    78
    if (sem_trywait (&sem->sem) == 0) {
slouken@1662
    79
        retval = 0;
slouken@1662
    80
    }
slouken@1662
    81
    return retval;
slouken@1361
    82
}
slouken@1361
    83
slouken@1662
    84
int
slouken@1662
    85
SDL_SemWait (SDL_sem * sem)
slouken@1361
    86
{
slouken@1662
    87
    int retval;
slouken@1361
    88
slouken@1662
    89
    if (!sem) {
slouken@1662
    90
        SDL_SetError ("Passed a NULL semaphore");
slouken@1662
    91
        return -1;
slouken@1662
    92
    }
slouken@1361
    93
slouken@1662
    94
    retval = sem_wait (&sem->sem);
slouken@1662
    95
    if (retval < 0) {
slouken@1662
    96
        SDL_SetError ("sem_wait() failed");
slouken@1662
    97
    }
slouken@1662
    98
    return retval;
slouken@1361
    99
}
slouken@1361
   100
slouken@1662
   101
int
slouken@1662
   102
SDL_SemWaitTimeout (SDL_sem * sem, Uint32 timeout)
slouken@1361
   103
{
slouken@1662
   104
    int retval;
slouken@1361
   105
slouken@1662
   106
    if (!sem) {
slouken@1662
   107
        SDL_SetError ("Passed a NULL semaphore");
slouken@1662
   108
        return -1;
slouken@1662
   109
    }
slouken@1361
   110
slouken@1662
   111
    /* Try the easy cases first */
slouken@1662
   112
    if (timeout == 0) {
slouken@1662
   113
        return SDL_SemTryWait (sem);
slouken@1662
   114
    }
slouken@1662
   115
    if (timeout == SDL_MUTEX_MAXWAIT) {
slouken@1662
   116
        return SDL_SemWait (sem);
slouken@1662
   117
    }
slouken@1361
   118
slouken@1662
   119
    /* Ack!  We have to busy wait... */
slouken@1662
   120
    /* FIXME: Use sem_timedwait()? */
slouken@1662
   121
    timeout += SDL_GetTicks ();
slouken@1662
   122
    do {
slouken@1662
   123
        retval = SDL_SemTryWait (sem);
slouken@1662
   124
        if (retval == 0) {
slouken@1662
   125
            break;
slouken@1662
   126
        }
slouken@1662
   127
        SDL_Delay (1);
slouken@1662
   128
    }
slouken@1662
   129
    while (SDL_GetTicks () < timeout);
slouken@1361
   130
slouken@1662
   131
    return retval;
slouken@1361
   132
}
slouken@1361
   133
slouken@1662
   134
Uint32
slouken@1662
   135
SDL_SemValue (SDL_sem * sem)
slouken@1361
   136
{
slouken@1662
   137
    int ret = 0;
slouken@1662
   138
    if (sem) {
slouken@1662
   139
        sem_getvalue (&sem->sem, &ret);
slouken@1662
   140
        if (ret < 0) {
slouken@1662
   141
            ret = 0;
slouken@1662
   142
        }
slouken@1662
   143
    }
slouken@1662
   144
    return (Uint32) ret;
slouken@1361
   145
}
slouken@1361
   146
slouken@1662
   147
int
slouken@1662
   148
SDL_SemPost (SDL_sem * sem)
slouken@1361
   149
{
slouken@1662
   150
    int retval;
slouken@1361
   151
slouken@1662
   152
    if (!sem) {
slouken@1662
   153
        SDL_SetError ("Passed a NULL semaphore");
slouken@1662
   154
        return -1;
slouken@1662
   155
    }
slouken@1361
   156
slouken@1662
   157
    retval = sem_post (&sem->sem);
slouken@1662
   158
    if (retval < 0) {
slouken@1662
   159
        SDL_SetError ("sem_post() failed");
slouken@1662
   160
    }
slouken@1662
   161
    return retval;
slouken@1361
   162
}
slouken@1627
   163
slouken@1627
   164
#endif /* __MACOSX__ */
slouken@1662
   165
/* vi: set ts=4 sw=4 expandtab: */