Fixed bug #179
authorSam Lantinga <slouken@libsdl.org>
Thu, 13 Apr 2006 14:04:13 +0000
changeset 1627 aee7ea396f59
parent 1626 a80e1e0880b8
child 1628 10b4827452a8
Fixed bug #179 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.
src/thread/generic/SDL_syssem.c
src/thread/pthread/SDL_syssem.c
--- a/src/thread/generic/SDL_syssem.c	Thu Apr 13 13:38:40 2006 +0000
+++ b/src/thread/generic/SDL_syssem.c	Thu Apr 13 14:04:13 2006 +0000
@@ -87,7 +87,7 @@
 	sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
 	if ( ! sem ) {
 		SDL_OutOfMemory();
-		return(0);
+		return NULL;
 	}
 	sem->count = initial_value;
 	sem->waiters_count = 0;
@@ -96,10 +96,10 @@
 	sem->count_nonzero = SDL_CreateCond();
 	if ( ! sem->count_lock || ! sem->count_nonzero ) {
 		SDL_DestroySemaphore(sem);
-		return(0);
+		return NULL;
 	}
 
-	return(sem);
+	return sem;
 }
 
 /* WARNING:
@@ -114,9 +114,11 @@
 			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);
 	}
 }
--- a/src/thread/pthread/SDL_syssem.c	Thu Apr 13 13:38:40 2006 +0000
+++ b/src/thread/pthread/SDL_syssem.c	Thu Apr 13 14:04:13 2006 +0000
@@ -30,15 +30,12 @@
 /* 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 */
@@ -46,20 +43,6 @@
 {
 	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);
@@ -67,7 +50,6 @@
 		} else {
 			sem->sem = &sem->sem_data;
 		}
-#endif /* USE_NAMED_SEMAPHORES */
 	} else {
 		SDL_OutOfMemory();
 	}
@@ -77,11 +59,7 @@
 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);
 	}
 }
@@ -135,6 +113,7 @@
 	}
 
 	/* Ack!  We have to busy wait... */
+	/* FIXME: Use sem_timedwait()? */
 	timeout += SDL_GetTicks();
 	do {
 		retval = SDL_SemTryWait(sem);
@@ -174,3 +153,5 @@
 	}
 	return retval;
 }
+
+#endif /* __MACOSX__ */