Skip to content

Commit

Permalink
Fixed bug #179
Browse files Browse the repository at this point in the history
SDL_SemValue() always returns 0. That's because the underlying POSIX
sem_getvalue() is implemented as a noop in Mac OS X. Apart from that,
semaphores do work properly (at least according to test/testsem), so I'm not
sure if this is worth fixing at all.
  • Loading branch information
slouken committed Apr 13, 2006
1 parent eeaea16 commit 7f137d6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 31 deletions.
14 changes: 8 additions & 6 deletions src/thread/generic/SDL_syssem.c
Expand Up @@ -87,7 +87,7 @@ SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
if ( ! sem ) {
SDL_OutOfMemory();
return(0);
return NULL;
}
sem->count = initial_value;
sem->waiters_count = 0;
Expand All @@ -96,10 +96,10 @@ SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
sem->count_nonzero = SDL_CreateCond();
if ( ! sem->count_lock || ! sem->count_nonzero ) {
SDL_DestroySemaphore(sem);
return(0);
return NULL;
}

return(sem);
return sem;
}

/* WARNING:
Expand All @@ -114,9 +114,11 @@ void SDL_DestroySemaphore(SDL_sem *sem)
SDL_Delay(10);
}
SDL_DestroyCond(sem->count_nonzero);
SDL_mutexP(sem->count_lock);
SDL_mutexV(sem->count_lock);
SDL_DestroyMutex(sem->count_lock);
if ( sem->count_lock ) {
SDL_mutexP(sem->count_lock);
SDL_mutexV(sem->count_lock);
SDL_DestroyMutex(sem->count_lock);
}
SDL_free(sem);
}
}
Expand Down
31 changes: 6 additions & 25 deletions src/thread/pthread/SDL_syssem.c
Expand Up @@ -30,44 +30,26 @@
/* Wrapper around POSIX 1003.1b semaphores */

#ifdef __MACOSX__
#define USE_NAMED_SEMAPHORES 1
#include <unistd.h>
#endif
/* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
#include "../generic/SDL_syssem.c"
#else

struct SDL_semaphore {
sem_t *sem;
#if !USE_NAMED_SEMAPHORES
sem_t sem_data;
#endif
};

/* Create a semaphore, initialized with value */
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
{
SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
if ( sem ) {
#if USE_NAMED_SEMAPHORES
static int semnum = 0;
char name[32];

SDL_snprintf(name, SDL_arraysize(name), "/SDL_sem-%d-%4.4d", getpid(), semnum++);
sem->sem = sem_open(name, O_CREAT, 0600, initial_value);
if ( sem->sem == (sem_t *)SEM_FAILED ) {
SDL_SetError("sem_open(%s) failed", name);
SDL_free(sem);
sem = NULL;
} else {
sem_unlink(name);
}
#else
if ( sem_init(&sem->sem_data, 0, initial_value) < 0 ) {
SDL_SetError("sem_init() failed");
SDL_free(sem);
sem = NULL;
} else {
sem->sem = &sem->sem_data;
}
#endif /* USE_NAMED_SEMAPHORES */
} else {
SDL_OutOfMemory();
}
Expand All @@ -77,11 +59,7 @@ SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
void SDL_DestroySemaphore(SDL_sem *sem)
{
if ( sem ) {
#if USE_NAMED_SEMAPHORES
sem_close(sem->sem);
#else
sem_destroy(sem->sem);
#endif
SDL_free(sem);
}
}
Expand Down Expand Up @@ -135,6 +113,7 @@ int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
}

/* Ack! We have to busy wait... */
/* FIXME: Use sem_timedwait()? */
timeout += SDL_GetTicks();
do {
retval = SDL_SemTryWait(sem);
Expand Down Expand Up @@ -174,3 +153,5 @@ int SDL_SemPost(SDL_sem *sem)
}
return retval;
}

#endif /* __MACOSX__ */

0 comments on commit 7f137d6

Please sign in to comment.