Fixed bug #179
authorSam Lantinga <slouken@libsdl.org>
Thu, 13 Apr 2006 14:04:13 +0000
changeset 1627aee7ea396f59
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
     1.1 --- a/src/thread/generic/SDL_syssem.c	Thu Apr 13 13:38:40 2006 +0000
     1.2 +++ b/src/thread/generic/SDL_syssem.c	Thu Apr 13 14:04:13 2006 +0000
     1.3 @@ -87,7 +87,7 @@
     1.4  	sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
     1.5  	if ( ! sem ) {
     1.6  		SDL_OutOfMemory();
     1.7 -		return(0);
     1.8 +		return NULL;
     1.9  	}
    1.10  	sem->count = initial_value;
    1.11  	sem->waiters_count = 0;
    1.12 @@ -96,10 +96,10 @@
    1.13  	sem->count_nonzero = SDL_CreateCond();
    1.14  	if ( ! sem->count_lock || ! sem->count_nonzero ) {
    1.15  		SDL_DestroySemaphore(sem);
    1.16 -		return(0);
    1.17 +		return NULL;
    1.18  	}
    1.19  
    1.20 -	return(sem);
    1.21 +	return sem;
    1.22  }
    1.23  
    1.24  /* WARNING:
    1.25 @@ -114,9 +114,11 @@
    1.26  			SDL_Delay(10);
    1.27  		}
    1.28  		SDL_DestroyCond(sem->count_nonzero);
    1.29 -		SDL_mutexP(sem->count_lock);
    1.30 -		SDL_mutexV(sem->count_lock);
    1.31 -		SDL_DestroyMutex(sem->count_lock);
    1.32 +		if ( sem->count_lock ) {
    1.33 +			SDL_mutexP(sem->count_lock);
    1.34 +			SDL_mutexV(sem->count_lock);
    1.35 +			SDL_DestroyMutex(sem->count_lock);
    1.36 +		}
    1.37  		SDL_free(sem);
    1.38  	}
    1.39  }
     2.1 --- a/src/thread/pthread/SDL_syssem.c	Thu Apr 13 13:38:40 2006 +0000
     2.2 +++ b/src/thread/pthread/SDL_syssem.c	Thu Apr 13 14:04:13 2006 +0000
     2.3 @@ -30,15 +30,12 @@
     2.4  /* Wrapper around POSIX 1003.1b semaphores */
     2.5  
     2.6  #ifdef __MACOSX__
     2.7 -#define USE_NAMED_SEMAPHORES 1
     2.8 -#include <unistd.h>
     2.9 -#endif
    2.10 +/* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
    2.11 +#include "../generic/SDL_syssem.c"
    2.12 +#else
    2.13  
    2.14  struct SDL_semaphore {
    2.15  	sem_t *sem;
    2.16 -#if !USE_NAMED_SEMAPHORES
    2.17 -	sem_t sem_data;
    2.18 -#endif
    2.19  };
    2.20  
    2.21  /* Create a semaphore, initialized with value */
    2.22 @@ -46,20 +43,6 @@
    2.23  {
    2.24  	SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
    2.25  	if ( sem ) {
    2.26 -#if USE_NAMED_SEMAPHORES
    2.27 -		static int semnum = 0;
    2.28 -		char name[32];
    2.29 -
    2.30 -		SDL_snprintf(name, SDL_arraysize(name), "/SDL_sem-%d-%4.4d", getpid(), semnum++);
    2.31 -		sem->sem = sem_open(name, O_CREAT, 0600, initial_value);
    2.32 -		if ( sem->sem == (sem_t *)SEM_FAILED ) {
    2.33 -			SDL_SetError("sem_open(%s) failed", name);
    2.34 -			SDL_free(sem);
    2.35 -			sem = NULL;
    2.36 -		} else {
    2.37 -			sem_unlink(name);
    2.38 -		}
    2.39 -#else
    2.40  		if ( sem_init(&sem->sem_data, 0, initial_value) < 0 ) {
    2.41  			SDL_SetError("sem_init() failed");
    2.42  			SDL_free(sem);
    2.43 @@ -67,7 +50,6 @@
    2.44  		} else {
    2.45  			sem->sem = &sem->sem_data;
    2.46  		}
    2.47 -#endif /* USE_NAMED_SEMAPHORES */
    2.48  	} else {
    2.49  		SDL_OutOfMemory();
    2.50  	}
    2.51 @@ -77,11 +59,7 @@
    2.52  void SDL_DestroySemaphore(SDL_sem *sem)
    2.53  {
    2.54  	if ( sem ) {
    2.55 -#if USE_NAMED_SEMAPHORES
    2.56 -		sem_close(sem->sem);
    2.57 -#else
    2.58  		sem_destroy(sem->sem);
    2.59 -#endif
    2.60  		SDL_free(sem);
    2.61  	}
    2.62  }
    2.63 @@ -135,6 +113,7 @@
    2.64  	}
    2.65  
    2.66  	/* Ack!  We have to busy wait... */
    2.67 +	/* FIXME: Use sem_timedwait()? */
    2.68  	timeout += SDL_GetTicks();
    2.69  	do {
    2.70  		retval = SDL_SemTryWait(sem);
    2.71 @@ -174,3 +153,5 @@
    2.72  	}
    2.73  	return retval;
    2.74  }
    2.75 +
    2.76 +#endif /* __MACOSX__ */