diff src/thread/amigaos/SDL_syssem.c @ 21:75a95f82bc1f

Updated the Amiga OS port of SDL (thanks Gabriele)
author Sam Lantinga <slouken@lokigames.com>
date Thu, 10 May 2001 20:13:29 +0000
parents 74212992fb08
children e8157fcb3114
line wrap: on
line diff
--- a/src/thread/amigaos/SDL_syssem.c	Thu May 10 20:10:54 2001 +0000
+++ b/src/thread/amigaos/SDL_syssem.c	Thu May 10 20:13:29 2001 +0000
@@ -25,7 +25,7 @@
  "@(#) $Id$";
 #endif
 
-/* An native implementation of semaphores on AmigaOS */
+/* An implementation of semaphores using mutexes and condition variables */
 
 #include "SDL_error.h"
 #include "SDL_thread.h"
@@ -35,9 +35,13 @@
 struct SDL_semaphore
 {
 	struct SignalSemaphore Sem;
+	Uint32 count;
+	Uint32 waiters_count;
+	SDL_mutex *count_lock;
+	SDL_cond *count_nonzero;
 };
 
-#undef D(x)
+#undef D
 
 #define D(x)
 
@@ -46,20 +50,18 @@
 	SDL_sem *sem;
 
 	sem = (SDL_sem *)malloc(sizeof(*sem));
+
 	if ( ! sem ) {
 		SDL_OutOfMemory();
 		return(0);
 	}
-	memset(sem, 0, sizeof(*sem));
 
 	D(bug("Creating semaphore %lx...\n",sem));
 
+	memset(sem,0,sizeof(*sem));
+
 	InitSemaphore(&sem->Sem);
-#if 1 // Allow multiple obtainings of the semaphore
-        while ( initial_value-- ) {
-		ReleaseSemaphore(&sem->Sem);
-	}
-#endif
+	
 	return(sem);
 }
 
@@ -75,8 +77,6 @@
 
 int SDL_SemTryWait(SDL_sem *sem)
 {
-	int retval;
-
 	if ( ! sem ) {
 		SDL_SetError("Passed a NULL semaphore");
 		return -1;
@@ -84,17 +84,17 @@
 
 	D(bug("TryWait semaphore...%lx\n",sem));
 
-	retval = SDL_MUTEX_TIMEDOUT;
-	if ( AttemptSemaphore(&sem->Sem) ) {
-		retval = 0;
-	}
-	return retval;
+	ObtainSemaphore(&sem->Sem);
+//	ReleaseSemaphore(&sem->Sem);
+
+	return 1;
 }
 
 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
 {
 	int retval;
 
+
 	if ( ! sem ) {
 		SDL_SetError("Passed a NULL semaphore");
 		return -1;
@@ -102,16 +102,22 @@
 
 	D(bug("WaitTimeout (%ld) semaphore...%lx\n",timeout,sem));
 
-#if 1 // We need to keep trying the semaphore until the timeout expires
-	retval = SDL_MUTEX_TIMEDOUT;
-	then = SDL_GetTicks();
-	do {
-		if ( AttemptSemaphore(&sem->Sem) ) {
-			retval = 0;
-		}
-		now = SDL_GetTicks();
-	} while ( (retval == SDL_MUTEX_TIMEDOUT) && ((now-then) < timeout) );
-#else
+	/* A timeout of 0 is an easy case */
+	if ( timeout == 0 ) {
+		return SDL_SemTryWait(sem);
+	}
+/*
+	SDL_LockMutex(sem->count_lock);
+	++sem->waiters_count;
+	retval = 0;
+	while ( (sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT) ) {
+		retval = SDL_CondWaitTimeout(sem->count_nonzero,
+		                             sem->count_lock, timeout);
+	}
+	--sem->waiters_count;
+	--sem->count;
+	SDL_UnlockMutex(sem->count_lock);
+*/
 	if(!(retval=AttemptSemaphore(&sem->Sem)))
 	{
 		SDL_Delay(timeout);
@@ -123,22 +129,15 @@
 //		ReleaseSemaphore(&sem->Sem);
 		retval=1;
 	}
-#endif
+
 	return retval;
 }
 
 int SDL_SemWait(SDL_sem *sem)
 {
-	if ( ! sem ) {
-		SDL_SetError("Passed a NULL semaphore");
-		return -1;
-	}
-#if 1 // This should be an infinite wait - FIXME, what is the return value?
 	ObtainSemaphore(&sem->Sem);
-        return 0;
-#else
-	return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
-#endif
+	return 0;
+//	return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
 }
 
 Uint32 SDL_SemValue(SDL_sem *sem)
@@ -148,6 +147,7 @@
 	value = 0;
 	if ( sem ) {
 		value = sem->Sem.ss_NestCount;
+//		SDL_UnlockMutex(sem->count_lock);
 	}
 	return value;
 }
@@ -161,6 +161,14 @@
 	D(bug("SemPost semaphore...%lx\n",sem));
 
 	ReleaseSemaphore(&sem->Sem);
+#if 0
+	SDL_LockMutex(sem->count_lock);
+	if ( sem->waiters_count > 0 ) {
+		SDL_CondSignal(sem->count_nonzero);
+	}
+	++sem->count;
+	SDL_UnlockMutex(sem->count_lock);
+#endif
 	return 0;
 }