src/thread/nds/SDL_syssem.c
branchgsoc2008_nds
changeset 2670 6e4669f4db49
child 2671 c3e7c0698cbb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/thread/nds/SDL_syssem.c	Tue Jun 10 06:57:57 2008 +0000
     1.3 @@ -0,0 +1,214 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@devolution.com
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id: SDL_syssem.c,v 1.2 2001/04/26 16:50:18 hercules Exp $";
    1.29 +#endif
    1.30 +
    1.31 +/* An implementation of semaphores using mutexes and condition variables */
    1.32 +
    1.33 +#include <stdlib.h>
    1.34 +
    1.35 +#include "SDL_error.h"
    1.36 +#include "SDL_timer.h"
    1.37 +#include "SDL_thread.h"
    1.38 +#include "SDL_systhread_c.h"
    1.39 +
    1.40 +
    1.41 +#ifdef DISABLE_THREADS
    1.42 +
    1.43 +SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
    1.44 +{
    1.45 +	SDL_SetError("SDL not configured with thread support");
    1.46 +	return (SDL_sem *)0;
    1.47 +}
    1.48 +
    1.49 +void SDL_DestroySemaphore(SDL_sem *sem)
    1.50 +{
    1.51 +	return;
    1.52 +}
    1.53 +
    1.54 +int SDL_SemTryWait(SDL_sem *sem)
    1.55 +{
    1.56 +	SDL_SetError("SDL not configured with thread support");
    1.57 +	return -1;
    1.58 +}
    1.59 +
    1.60 +int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
    1.61 +{
    1.62 +	SDL_SetError("SDL not configured with thread support");
    1.63 +	return -1;
    1.64 +}
    1.65 +
    1.66 +int SDL_SemWait(SDL_sem *sem)
    1.67 +{
    1.68 +	SDL_SetError("SDL not configured with thread support");
    1.69 +	return -1;
    1.70 +}
    1.71 +
    1.72 +Uint32 SDL_SemValue(SDL_sem *sem)
    1.73 +{
    1.74 +	return 0;
    1.75 +}
    1.76 +
    1.77 +int SDL_SemPost(SDL_sem *sem)
    1.78 +{
    1.79 +	SDL_SetError("SDL not configured with thread support");
    1.80 +	return -1;
    1.81 +}
    1.82 +
    1.83 +#else
    1.84 +
    1.85 +struct SDL_semaphore
    1.86 +{
    1.87 +	Uint32 count;
    1.88 +	Uint32 waiters_count;
    1.89 +	SDL_mutex *count_lock;
    1.90 +	SDL_cond *count_nonzero;
    1.91 +};
    1.92 +
    1.93 +SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
    1.94 +{
    1.95 +	SDL_sem *sem;
    1.96 +
    1.97 +	sem = (SDL_sem *)malloc(sizeof(*sem));
    1.98 +	if ( ! sem ) {
    1.99 +		SDL_OutOfMemory();
   1.100 +		return(0);
   1.101 +	}
   1.102 +	sem->count = initial_value;
   1.103 +	sem->waiters_count = 0;
   1.104 +
   1.105 +	sem->count_lock = SDL_CreateMutex();
   1.106 +	sem->count_nonzero = SDL_CreateCond();
   1.107 +	if ( ! sem->count_lock || ! sem->count_nonzero ) {
   1.108 +		SDL_DestroySemaphore(sem);
   1.109 +		return(0);
   1.110 +	}
   1.111 +
   1.112 +	return(sem);
   1.113 +}
   1.114 +
   1.115 +/* WARNING:
   1.116 +   You cannot call this function when another thread is using the semaphore.
   1.117 +*/
   1.118 +void SDL_DestroySemaphore(SDL_sem *sem)
   1.119 +{
   1.120 +	if ( sem ) {
   1.121 +		sem->count = 0xFFFFFFFF;
   1.122 +		while ( sem->waiters_count > 0) {
   1.123 +			SDL_CondSignal(sem->count_nonzero);
   1.124 +			SDL_Delay(10);
   1.125 +		}
   1.126 +		SDL_DestroyCond(sem->count_nonzero);
   1.127 +		SDL_mutexP(sem->count_lock);
   1.128 +		SDL_mutexV(sem->count_lock);
   1.129 +		SDL_DestroyMutex(sem->count_lock);
   1.130 +		free(sem);
   1.131 +	}
   1.132 +}
   1.133 +
   1.134 +int SDL_SemTryWait(SDL_sem *sem)
   1.135 +{
   1.136 +	int retval;
   1.137 +
   1.138 +	if ( ! sem ) {
   1.139 +		SDL_SetError("Passed a NULL semaphore");
   1.140 +		return -1;
   1.141 +	}
   1.142 +
   1.143 +	retval = SDL_MUTEX_TIMEDOUT;
   1.144 +	SDL_LockMutex(sem->count_lock);
   1.145 +	if ( sem->count > 0 ) {
   1.146 +		--sem->count;
   1.147 +		retval = 0;
   1.148 +	}
   1.149 +	SDL_UnlockMutex(sem->count_lock);
   1.150 +
   1.151 +	return retval;
   1.152 +}
   1.153 +
   1.154 +int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
   1.155 +{
   1.156 +	int retval;
   1.157 +
   1.158 +	if ( ! sem ) {
   1.159 +		SDL_SetError("Passed a NULL semaphore");
   1.160 +		return -1;
   1.161 +	}
   1.162 +
   1.163 +	/* A timeout of 0 is an easy case */
   1.164 +	if ( timeout == 0 ) {
   1.165 +		return SDL_SemTryWait(sem);
   1.166 +	}
   1.167 +
   1.168 +	SDL_LockMutex(sem->count_lock);
   1.169 +	++sem->waiters_count;
   1.170 +	retval = 0;
   1.171 +	while ( (sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT) ) {
   1.172 +		retval = SDL_CondWaitTimeout(sem->count_nonzero,
   1.173 +		                             sem->count_lock, timeout);
   1.174 +	}
   1.175 +	--sem->waiters_count;
   1.176 +	--sem->count;
   1.177 +	SDL_UnlockMutex(sem->count_lock);
   1.178 +
   1.179 +	return retval;
   1.180 +}
   1.181 +
   1.182 +int SDL_SemWait(SDL_sem *sem)
   1.183 +{
   1.184 +	return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
   1.185 +}
   1.186 +
   1.187 +Uint32 SDL_SemValue(SDL_sem *sem)
   1.188 +{
   1.189 +	Uint32 value;
   1.190 +	
   1.191 +	value = 0;
   1.192 +	if ( sem ) {
   1.193 +		SDL_LockMutex(sem->count_lock);
   1.194 +		value = sem->count;
   1.195 +		SDL_UnlockMutex(sem->count_lock);
   1.196 +	}
   1.197 +	return value;
   1.198 +}
   1.199 +
   1.200 +int SDL_SemPost(SDL_sem *sem)
   1.201 +{
   1.202 +	if ( ! sem ) {
   1.203 +		SDL_SetError("Passed a NULL semaphore");
   1.204 +		return -1;
   1.205 +	}
   1.206 +
   1.207 +	SDL_LockMutex(sem->count_lock);
   1.208 +	if ( sem->waiters_count > 0 ) {
   1.209 +		SDL_CondSignal(sem->count_nonzero);
   1.210 +	}
   1.211 +	++sem->count;
   1.212 +	SDL_UnlockMutex(sem->count_lock);
   1.213 +
   1.214 +	return 0;
   1.215 +}
   1.216 +
   1.217 +#endif /* DISABLE_THREADS */