src/thread/nds/SDL_syscond.c
branchgsoc2008_nds
changeset 2671 c3e7c0698cbb
parent 2670 6e4669f4db49
     1.1 --- a/src/thread/nds/SDL_syscond.c	Tue Jun 10 06:57:57 2008 +0000
     1.2 +++ b/src/thread/nds/SDL_syscond.c	Thu Jun 12 02:38:49 2008 +0000
     1.3 @@ -22,7 +22,7 @@
     1.4  
     1.5  #ifdef SAVE_RCSID
     1.6  static char rcsid =
     1.7 - "@(#) $Id: SDL_syscond.c,v 1.2 2001/04/26 16:50:18 hercules Exp $";
     1.8 +    "@(#) $Id: SDL_syscond.c,v 1.2 2001/04/26 16:50:18 hercules Exp $";
     1.9  #endif
    1.10  
    1.11  /* An implementation of condition variables using semaphores and mutexes */
    1.12 @@ -39,107 +39,111 @@
    1.13  
    1.14  struct SDL_cond
    1.15  {
    1.16 -	SDL_mutex *lock;
    1.17 -	int waiting;
    1.18 -	int signals;
    1.19 -	SDL_sem *wait_sem;
    1.20 -	SDL_sem *wait_done;
    1.21 +    SDL_mutex *lock;
    1.22 +    int waiting;
    1.23 +    int signals;
    1.24 +    SDL_sem *wait_sem;
    1.25 +    SDL_sem *wait_done;
    1.26  };
    1.27  
    1.28  /* Create a condition variable */
    1.29 -SDL_cond * SDL_CreateCond(void)
    1.30 +SDL_cond *
    1.31 +SDL_CreateCond(void)
    1.32  {
    1.33 -	SDL_cond *cond;
    1.34 +    SDL_cond *cond;
    1.35  
    1.36 -	cond = (SDL_cond *) malloc(sizeof(SDL_cond));
    1.37 -	if ( cond ) {
    1.38 -		cond->lock = SDL_CreateMutex();
    1.39 -		cond->wait_sem = SDL_CreateSemaphore(0);
    1.40 -		cond->wait_done = SDL_CreateSemaphore(0);
    1.41 -		cond->waiting = cond->signals = 0;
    1.42 -		if ( ! cond->lock || ! cond->wait_sem || ! cond->wait_done ) {
    1.43 -			SDL_DestroyCond(cond);
    1.44 -			cond = NULL;
    1.45 -		}
    1.46 -	} else {
    1.47 -		SDL_OutOfMemory();
    1.48 -	}
    1.49 -	return(cond);
    1.50 +    cond = (SDL_cond *) malloc(sizeof(SDL_cond));
    1.51 +    if (cond) {
    1.52 +        cond->lock = SDL_CreateMutex();
    1.53 +        cond->wait_sem = SDL_CreateSemaphore(0);
    1.54 +        cond->wait_done = SDL_CreateSemaphore(0);
    1.55 +        cond->waiting = cond->signals = 0;
    1.56 +        if (!cond->lock || !cond->wait_sem || !cond->wait_done) {
    1.57 +            SDL_DestroyCond(cond);
    1.58 +            cond = NULL;
    1.59 +        }
    1.60 +    } else {
    1.61 +        SDL_OutOfMemory();
    1.62 +    }
    1.63 +    return (cond);
    1.64  }
    1.65  
    1.66  /* Destroy a condition variable */
    1.67 -void SDL_DestroyCond(SDL_cond *cond)
    1.68 +void
    1.69 +SDL_DestroyCond(SDL_cond * cond)
    1.70  {
    1.71 -	if ( cond ) {
    1.72 -		if ( cond->wait_sem ) {
    1.73 -			SDL_DestroySemaphore(cond->wait_sem);
    1.74 -		}
    1.75 -		if ( cond->wait_done ) {
    1.76 -			SDL_DestroySemaphore(cond->wait_done);
    1.77 -		}
    1.78 -		if ( cond->lock ) {
    1.79 -			SDL_DestroyMutex(cond->lock);
    1.80 -		}
    1.81 -		free(cond);
    1.82 -	}
    1.83 +    if (cond) {
    1.84 +        if (cond->wait_sem) {
    1.85 +            SDL_DestroySemaphore(cond->wait_sem);
    1.86 +        }
    1.87 +        if (cond->wait_done) {
    1.88 +            SDL_DestroySemaphore(cond->wait_done);
    1.89 +        }
    1.90 +        if (cond->lock) {
    1.91 +            SDL_DestroyMutex(cond->lock);
    1.92 +        }
    1.93 +        free(cond);
    1.94 +    }
    1.95  }
    1.96  
    1.97  /* Restart one of the threads that are waiting on the condition variable */
    1.98 -int SDL_CondSignal(SDL_cond *cond)
    1.99 +int
   1.100 +SDL_CondSignal(SDL_cond * cond)
   1.101  {
   1.102 -	if ( ! cond ) {
   1.103 -		SDL_SetError("Passed a NULL condition variable");
   1.104 -		return -1;
   1.105 -	}
   1.106 +    if (!cond) {
   1.107 +        SDL_SetError("Passed a NULL condition variable");
   1.108 +        return -1;
   1.109 +    }
   1.110  
   1.111 -	/* If there are waiting threads not already signalled, then
   1.112 -	   signal the condition and wait for the thread to respond.
   1.113 -	*/
   1.114 -	SDL_LockMutex(cond->lock);
   1.115 -	if ( cond->waiting > cond->signals ) {
   1.116 -		++cond->signals;
   1.117 -		SDL_SemPost(cond->wait_sem);
   1.118 -		SDL_UnlockMutex(cond->lock);
   1.119 -		SDL_SemWait(cond->wait_done);
   1.120 -	} else {
   1.121 -		SDL_UnlockMutex(cond->lock);
   1.122 -	}
   1.123 +    /* If there are waiting threads not already signalled, then
   1.124 +       signal the condition and wait for the thread to respond.
   1.125 +     */
   1.126 +    SDL_LockMutex(cond->lock);
   1.127 +    if (cond->waiting > cond->signals) {
   1.128 +        ++cond->signals;
   1.129 +        SDL_SemPost(cond->wait_sem);
   1.130 +        SDL_UnlockMutex(cond->lock);
   1.131 +        SDL_SemWait(cond->wait_done);
   1.132 +    } else {
   1.133 +        SDL_UnlockMutex(cond->lock);
   1.134 +    }
   1.135  
   1.136 -	return 0;
   1.137 +    return 0;
   1.138  }
   1.139  
   1.140  /* Restart all threads that are waiting on the condition variable */
   1.141 -int SDL_CondBroadcast(SDL_cond *cond)
   1.142 +int
   1.143 +SDL_CondBroadcast(SDL_cond * cond)
   1.144  {
   1.145 -	if ( ! cond ) {
   1.146 -		SDL_SetError("Passed a NULL condition variable");
   1.147 -		return -1;
   1.148 -	}
   1.149 +    if (!cond) {
   1.150 +        SDL_SetError("Passed a NULL condition variable");
   1.151 +        return -1;
   1.152 +    }
   1.153  
   1.154 -	/* If there are waiting threads not already signalled, then
   1.155 -	   signal the condition and wait for the thread to respond.
   1.156 -	*/
   1.157 -	SDL_LockMutex(cond->lock);
   1.158 -	if ( cond->waiting > cond->signals ) {
   1.159 -		int i, num_waiting;
   1.160 +    /* If there are waiting threads not already signalled, then
   1.161 +       signal the condition and wait for the thread to respond.
   1.162 +     */
   1.163 +    SDL_LockMutex(cond->lock);
   1.164 +    if (cond->waiting > cond->signals) {
   1.165 +        int i, num_waiting;
   1.166  
   1.167 -		num_waiting = (cond->waiting - cond->signals);
   1.168 -		cond->signals = cond->waiting;
   1.169 -		for ( i=0; i<num_waiting; ++i ) {
   1.170 -			SDL_SemPost(cond->wait_sem);
   1.171 -		}
   1.172 -		/* Now all released threads are blocked here, waiting for us.
   1.173 -		   Collect them all (and win fabulous prizes!) :-)
   1.174 -		 */
   1.175 -		SDL_UnlockMutex(cond->lock);
   1.176 -		for ( i=0; i<num_waiting; ++i ) {
   1.177 -			SDL_SemWait(cond->wait_done);
   1.178 -		}
   1.179 -	} else {
   1.180 -		SDL_UnlockMutex(cond->lock);
   1.181 -	}
   1.182 +        num_waiting = (cond->waiting - cond->signals);
   1.183 +        cond->signals = cond->waiting;
   1.184 +        for (i = 0; i < num_waiting; ++i) {
   1.185 +            SDL_SemPost(cond->wait_sem);
   1.186 +        }
   1.187 +        /* Now all released threads are blocked here, waiting for us.
   1.188 +           Collect them all (and win fabulous prizes!) :-)
   1.189 +         */
   1.190 +        SDL_UnlockMutex(cond->lock);
   1.191 +        for (i = 0; i < num_waiting; ++i) {
   1.192 +            SDL_SemWait(cond->wait_done);
   1.193 +        }
   1.194 +    } else {
   1.195 +        SDL_UnlockMutex(cond->lock);
   1.196 +    }
   1.197  
   1.198 -	return 0;
   1.199 +    return 0;
   1.200  }
   1.201  
   1.202  /* Wait on the condition variable for at most 'ms' milliseconds.
   1.203 @@ -162,62 +166,64 @@
   1.204  	...
   1.205  	SDL_UnlockMutex(lock);
   1.206   */
   1.207 -int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
   1.208 +int
   1.209 +SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
   1.210  {
   1.211 -	int retval;
   1.212 +    int retval;
   1.213  
   1.214 -	if ( ! cond ) {
   1.215 -		SDL_SetError("Passed a NULL condition variable");
   1.216 -		return -1;
   1.217 -	}
   1.218 +    if (!cond) {
   1.219 +        SDL_SetError("Passed a NULL condition variable");
   1.220 +        return -1;
   1.221 +    }
   1.222  
   1.223 -	/* Obtain the protection mutex, and increment the number of waiters.
   1.224 -	   This allows the signal mechanism to only perform a signal if there
   1.225 -	   are waiting threads.
   1.226 -	 */
   1.227 -	SDL_LockMutex(cond->lock);
   1.228 -	++cond->waiting;
   1.229 -	SDL_UnlockMutex(cond->lock);
   1.230 +    /* Obtain the protection mutex, and increment the number of waiters.
   1.231 +       This allows the signal mechanism to only perform a signal if there
   1.232 +       are waiting threads.
   1.233 +     */
   1.234 +    SDL_LockMutex(cond->lock);
   1.235 +    ++cond->waiting;
   1.236 +    SDL_UnlockMutex(cond->lock);
   1.237  
   1.238 -	/* Unlock the mutex, as is required by condition variable semantics */
   1.239 -	SDL_UnlockMutex(mutex);
   1.240 +    /* Unlock the mutex, as is required by condition variable semantics */
   1.241 +    SDL_UnlockMutex(mutex);
   1.242  
   1.243 -	/* Wait for a signal */
   1.244 -	if ( ms == SDL_MUTEX_MAXWAIT ) {
   1.245 -		retval = SDL_SemWait(cond->wait_sem);
   1.246 -	} else {
   1.247 -		retval = SDL_SemWaitTimeout(cond->wait_sem, ms);
   1.248 -	}
   1.249 +    /* Wait for a signal */
   1.250 +    if (ms == SDL_MUTEX_MAXWAIT) {
   1.251 +        retval = SDL_SemWait(cond->wait_sem);
   1.252 +    } else {
   1.253 +        retval = SDL_SemWaitTimeout(cond->wait_sem, ms);
   1.254 +    }
   1.255  
   1.256 -	/* Let the signaler know we have completed the wait, otherwise
   1.257 -           the signaler can race ahead and get the condition semaphore
   1.258 -           if we are stopped between the mutex unlock and semaphore wait,
   1.259 -           giving a deadlock.  See the following URL for details:
   1.260 -        http://www-classic.be.com/aboutbe/benewsletter/volume_III/Issue40.html
   1.261 -	*/
   1.262 -	SDL_LockMutex(cond->lock);
   1.263 -	if ( cond->signals > 0 ) {
   1.264 -		/* If we timed out, we need to eat a condition signal */
   1.265 -		if ( retval > 0 ) {
   1.266 -			SDL_SemWait(cond->wait_sem);
   1.267 -		}
   1.268 -		/* We always notify the signal thread that we are done */
   1.269 -		SDL_SemPost(cond->wait_done);
   1.270 +    /* Let the signaler know we have completed the wait, otherwise
   1.271 +       the signaler can race ahead and get the condition semaphore
   1.272 +       if we are stopped between the mutex unlock and semaphore wait,
   1.273 +       giving a deadlock.  See the following URL for details:
   1.274 +       http://www-classic.be.com/aboutbe/benewsletter/volume_III/Issue40.html
   1.275 +     */
   1.276 +    SDL_LockMutex(cond->lock);
   1.277 +    if (cond->signals > 0) {
   1.278 +        /* If we timed out, we need to eat a condition signal */
   1.279 +        if (retval > 0) {
   1.280 +            SDL_SemWait(cond->wait_sem);
   1.281 +        }
   1.282 +        /* We always notify the signal thread that we are done */
   1.283 +        SDL_SemPost(cond->wait_done);
   1.284  
   1.285 -		/* Signal handshake complete */
   1.286 -		--cond->signals;
   1.287 -	}
   1.288 -	--cond->waiting;
   1.289 -	SDL_UnlockMutex(cond->lock);
   1.290 +        /* Signal handshake complete */
   1.291 +        --cond->signals;
   1.292 +    }
   1.293 +    --cond->waiting;
   1.294 +    SDL_UnlockMutex(cond->lock);
   1.295  
   1.296 -	/* Lock the mutex, as is required by condition variable semantics */
   1.297 -	SDL_LockMutex(mutex);
   1.298 +    /* Lock the mutex, as is required by condition variable semantics */
   1.299 +    SDL_LockMutex(mutex);
   1.300  
   1.301 -	return retval;
   1.302 +    return retval;
   1.303  }
   1.304  
   1.305  /* Wait on the condition variable forever */
   1.306 -int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
   1.307 +int
   1.308 +SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
   1.309  {
   1.310 -	return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
   1.311 +    return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
   1.312  }