src/thread/pthread/SDL_syssem.c
changeset 1627 aee7ea396f59
parent 1487 dc6b59e925a2
child 1630 eba962f9d603
equal deleted inserted replaced
1626:a80e1e0880b8 1627:aee7ea396f59
    28 #include "SDL_timer.h"
    28 #include "SDL_timer.h"
    29 
    29 
    30 /* Wrapper around POSIX 1003.1b semaphores */
    30 /* Wrapper around POSIX 1003.1b semaphores */
    31 
    31 
    32 #ifdef __MACOSX__
    32 #ifdef __MACOSX__
    33 #define USE_NAMED_SEMAPHORES 1
    33 /* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
    34 #include <unistd.h>
    34 #include "../generic/SDL_syssem.c"
    35 #endif
    35 #else
    36 
    36 
    37 struct SDL_semaphore {
    37 struct SDL_semaphore {
    38 	sem_t *sem;
    38 	sem_t *sem;
    39 #if !USE_NAMED_SEMAPHORES
       
    40 	sem_t sem_data;
       
    41 #endif
       
    42 };
    39 };
    43 
    40 
    44 /* Create a semaphore, initialized with value */
    41 /* Create a semaphore, initialized with value */
    45 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
    42 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
    46 {
    43 {
    47 	SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
    44 	SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
    48 	if ( sem ) {
    45 	if ( sem ) {
    49 #if USE_NAMED_SEMAPHORES
       
    50 		static int semnum = 0;
       
    51 		char name[32];
       
    52 
       
    53 		SDL_snprintf(name, SDL_arraysize(name), "/SDL_sem-%d-%4.4d", getpid(), semnum++);
       
    54 		sem->sem = sem_open(name, O_CREAT, 0600, initial_value);
       
    55 		if ( sem->sem == (sem_t *)SEM_FAILED ) {
       
    56 			SDL_SetError("sem_open(%s) failed", name);
       
    57 			SDL_free(sem);
       
    58 			sem = NULL;
       
    59 		} else {
       
    60 			sem_unlink(name);
       
    61 		}
       
    62 #else
       
    63 		if ( sem_init(&sem->sem_data, 0, initial_value) < 0 ) {
    46 		if ( sem_init(&sem->sem_data, 0, initial_value) < 0 ) {
    64 			SDL_SetError("sem_init() failed");
    47 			SDL_SetError("sem_init() failed");
    65 			SDL_free(sem);
    48 			SDL_free(sem);
    66 			sem = NULL;
    49 			sem = NULL;
    67 		} else {
    50 		} else {
    68 			sem->sem = &sem->sem_data;
    51 			sem->sem = &sem->sem_data;
    69 		}
    52 		}
    70 #endif /* USE_NAMED_SEMAPHORES */
       
    71 	} else {
    53 	} else {
    72 		SDL_OutOfMemory();
    54 		SDL_OutOfMemory();
    73 	}
    55 	}
    74 	return sem;
    56 	return sem;
    75 }
    57 }
    76 
    58 
    77 void SDL_DestroySemaphore(SDL_sem *sem)
    59 void SDL_DestroySemaphore(SDL_sem *sem)
    78 {
    60 {
    79 	if ( sem ) {
    61 	if ( sem ) {
    80 #if USE_NAMED_SEMAPHORES
       
    81 		sem_close(sem->sem);
       
    82 #else
       
    83 		sem_destroy(sem->sem);
    62 		sem_destroy(sem->sem);
    84 #endif
       
    85 		SDL_free(sem);
    63 		SDL_free(sem);
    86 	}
    64 	}
    87 }
    65 }
    88 
    66 
    89 int SDL_SemTryWait(SDL_sem *sem)
    67 int SDL_SemTryWait(SDL_sem *sem)
   133 	if ( timeout == SDL_MUTEX_MAXWAIT ) {
   111 	if ( timeout == SDL_MUTEX_MAXWAIT ) {
   134 		return SDL_SemWait(sem);
   112 		return SDL_SemWait(sem);
   135 	}
   113 	}
   136 
   114 
   137 	/* Ack!  We have to busy wait... */
   115 	/* Ack!  We have to busy wait... */
       
   116 	/* FIXME: Use sem_timedwait()? */
   138 	timeout += SDL_GetTicks();
   117 	timeout += SDL_GetTicks();
   139 	do {
   118 	do {
   140 		retval = SDL_SemTryWait(sem);
   119 		retval = SDL_SemTryWait(sem);
   141 		if ( retval == 0 ) {
   120 		if ( retval == 0 ) {
   142 			break;
   121 			break;
   172 	if ( retval < 0 ) {
   151 	if ( retval < 0 ) {
   173 		SDL_SetError("sem_post() failed");
   152 		SDL_SetError("sem_post() failed");
   174 	}
   153 	}
   175 	return retval;
   154 	return retval;
   176 }
   155 }
       
   156 
       
   157 #endif /* __MACOSX__ */