src/thread/pthread/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 13 Apr 2006 14:27:58 +0000
changeset 1630 eba962f9d603
parent 1627 aee7ea396f59
child 1662 782fd950bd46
child 1851 536b0704b7d8
permissions -rw-r--r--
Fixed compile error
     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 	sem_t sem;
    39 };
    40 
    41 /* Create a semaphore, initialized with value */
    42 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
    43 {
    44 	SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
    45 	if ( sem ) {
    46 		if ( sem_init(&sem->sem, 0, initial_value) < 0 ) {
    47 			SDL_SetError("sem_init() failed");
    48 			SDL_free(sem);
    49 			sem = NULL;
    50 		}
    51 	} else {
    52 		SDL_OutOfMemory();
    53 	}
    54 	return sem;
    55 }
    56 
    57 void SDL_DestroySemaphore(SDL_sem *sem)
    58 {
    59 	if ( sem ) {
    60 		sem_destroy(&sem->sem);
    61 		SDL_free(sem);
    62 	}
    63 }
    64 
    65 int SDL_SemTryWait(SDL_sem *sem)
    66 {
    67 	int retval;
    68 
    69 	if ( ! sem ) {
    70 		SDL_SetError("Passed a NULL semaphore");
    71 		return -1;
    72 	}
    73 	retval = SDL_MUTEX_TIMEDOUT;
    74 	if ( sem_trywait(&sem->sem) == 0 ) {
    75 		retval = 0;
    76 	}
    77 	return retval;
    78 }
    79 
    80 int SDL_SemWait(SDL_sem *sem)
    81 {
    82 	int retval;
    83 
    84 	if ( ! sem ) {
    85 		SDL_SetError("Passed a NULL semaphore");
    86 		return -1;
    87 	}
    88 
    89 	retval = sem_wait(&sem->sem);
    90 	if ( retval < 0 ) {
    91 		SDL_SetError("sem_wait() failed");
    92 	}
    93 	return retval;
    94 }
    95 
    96 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
    97 {
    98 	int retval;
    99 
   100 	if ( ! sem ) {
   101 		SDL_SetError("Passed a NULL semaphore");
   102 		return -1;
   103 	}
   104 
   105 	/* Try the easy cases first */
   106 	if ( timeout == 0 ) {
   107 		return SDL_SemTryWait(sem);
   108 	}
   109 	if ( timeout == SDL_MUTEX_MAXWAIT ) {
   110 		return SDL_SemWait(sem);
   111 	}
   112 
   113 	/* Ack!  We have to busy wait... */
   114 	/* FIXME: Use sem_timedwait()? */
   115 	timeout += SDL_GetTicks();
   116 	do {
   117 		retval = SDL_SemTryWait(sem);
   118 		if ( retval == 0 ) {
   119 			break;
   120 		}
   121 		SDL_Delay(1);
   122 	} while ( SDL_GetTicks() < timeout );
   123 
   124 	return retval;
   125 }
   126 
   127 Uint32 SDL_SemValue(SDL_sem *sem)
   128 {
   129 	int ret = 0;
   130 	if ( sem ) {
   131 		sem_getvalue(&sem->sem, &ret);
   132 		if ( ret < 0 ) {
   133 			ret = 0;
   134 		}
   135 	}
   136 	return (Uint32)ret;
   137 }
   138 
   139 int SDL_SemPost(SDL_sem *sem)
   140 {
   141 	int retval;
   142 
   143 	if ( ! sem ) {
   144 		SDL_SetError("Passed a NULL semaphore");
   145 		return -1;
   146 	}
   147 
   148 	retval = sem_post(&sem->sem);
   149 	if ( retval < 0 ) {
   150 		SDL_SetError("sem_post() failed");
   151 	}
   152 	return retval;
   153 }
   154 
   155 #endif /* __MACOSX__ */