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.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include <pthread.h>
    25 #include <semaphore.h>
    26 
    27 #include "SDL_thread.h"
    28 #include "SDL_timer.h"
    29 
    30 /* Wrapper around POSIX 1003.1b semaphores */
    31 
    32 #ifdef __MACOSX__
    33 /* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
    34 #include "../generic/SDL_syssem.c"
    35 #else
    36 
    37 struct SDL_semaphore
    38 {
    39     sem_t sem;
    40 };
    41 
    42 /* Create a semaphore, initialized with value */
    43 SDL_sem *
    44 SDL_CreateSemaphore (Uint32 initial_value)
    45 {
    46     SDL_sem *sem = (SDL_sem *) SDL_malloc (sizeof (SDL_sem));
    47     if (sem) {
    48         if (sem_init (&sem->sem, 0, initial_value) < 0) {
    49             SDL_SetError ("sem_init() failed");
    50             SDL_free (sem);
    51             sem = NULL;
    52         }
    53     } else {
    54         SDL_OutOfMemory ();
    55     }
    56     return sem;
    57 }
    58 
    59 void
    60 SDL_DestroySemaphore (SDL_sem * sem)
    61 {
    62     if (sem) {
    63         sem_destroy (&sem->sem);
    64         SDL_free (sem);
    65     }
    66 }
    67 
    68 int
    69 SDL_SemTryWait (SDL_sem * sem)
    70 {
    71     int retval;
    72 
    73     if (!sem) {
    74         SDL_SetError ("Passed a NULL semaphore");
    75         return -1;
    76     }
    77     retval = SDL_MUTEX_TIMEDOUT;
    78     if (sem_trywait (&sem->sem) == 0) {
    79         retval = 0;
    80     }
    81     return retval;
    82 }
    83 
    84 int
    85 SDL_SemWait (SDL_sem * sem)
    86 {
    87     int retval;
    88 
    89     if (!sem) {
    90         SDL_SetError ("Passed a NULL semaphore");
    91         return -1;
    92     }
    93 
    94     retval = sem_wait (&sem->sem);
    95     if (retval < 0) {
    96         SDL_SetError ("sem_wait() failed");
    97     }
    98     return retval;
    99 }
   100 
   101 int
   102 SDL_SemWaitTimeout (SDL_sem * sem, Uint32 timeout)
   103 {
   104     int retval;
   105 
   106     if (!sem) {
   107         SDL_SetError ("Passed a NULL semaphore");
   108         return -1;
   109     }
   110 
   111     /* Try the easy cases first */
   112     if (timeout == 0) {
   113         return SDL_SemTryWait (sem);
   114     }
   115     if (timeout == SDL_MUTEX_MAXWAIT) {
   116         return SDL_SemWait (sem);
   117     }
   118 
   119     /* Ack!  We have to busy wait... */
   120     /* FIXME: Use sem_timedwait()? */
   121     timeout += SDL_GetTicks ();
   122     do {
   123         retval = SDL_SemTryWait (sem);
   124         if (retval == 0) {
   125             break;
   126         }
   127         SDL_Delay (1);
   128     }
   129     while (SDL_GetTicks () < timeout);
   130 
   131     return retval;
   132 }
   133 
   134 Uint32
   135 SDL_SemValue (SDL_sem * sem)
   136 {
   137     int ret = 0;
   138     if (sem) {
   139         sem_getvalue (&sem->sem, &ret);
   140         if (ret < 0) {
   141             ret = 0;
   142         }
   143     }
   144     return (Uint32) ret;
   145 }
   146 
   147 int
   148 SDL_SemPost (SDL_sem * sem)
   149 {
   150     int retval;
   151 
   152     if (!sem) {
   153         SDL_SetError ("Passed a NULL semaphore");
   154         return -1;
   155     }
   156 
   157     retval = sem_post (&sem->sem);
   158     if (retval < 0) {
   159         SDL_SetError ("sem_post() failed");
   160     }
   161     return retval;
   162 }
   163 
   164 #endif /* __MACOSX__ */
   165 /* vi: set ts=4 sw=4 expandtab: */