src/thread/riscos/SDL_syscond.c
changeset 955 d74fbf56f2f6
child 1312 c9b51268668f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/thread/riscos/SDL_syscond.c	Fri Sep 17 13:20:10 2004 +0000
     1.3 @@ -0,0 +1,167 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2004 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@libsdl.org
    1.24 +*/
    1.25 +
    1.26 +/* RISC OS implementations uses pthreads based on linux code */
    1.27 +
    1.28 +#ifdef SAVE_RCSID
    1.29 +static char rcsid =
    1.30 + "@(#) $Id$";
    1.31 +#endif
    1.32 +
    1.33 +#ifdef DISABLE_THREADS
    1.34 +#include "../generic/SDL_syscond.c"
    1.35 +#else
    1.36 +#include <sys/time.h>
    1.37 +#include <unistd.h>
    1.38 +#include <errno.h>
    1.39 +#include <stdlib.h>
    1.40 +#include <pthread.h>
    1.41 +
    1.42 +#include "SDL_error.h"
    1.43 +#include "SDL_thread.h"
    1.44 +#include "SDL_sysmutex_c.h"
    1.45 +
    1.46 +struct SDL_cond
    1.47 +{
    1.48 +	pthread_cond_t cond;
    1.49 +};
    1.50 +
    1.51 +/* Create a condition variable */
    1.52 +SDL_cond * SDL_CreateCond(void)
    1.53 +{
    1.54 +	SDL_cond *cond;
    1.55 +
    1.56 +	cond = (SDL_cond *) malloc(sizeof(SDL_cond));
    1.57 +	if ( cond ) {
    1.58 +		if ( pthread_cond_init(&cond->cond, NULL) < 0 ) {
    1.59 +			SDL_SetError("pthread_cond_init() failed");
    1.60 +			free(cond);
    1.61 +			cond = NULL;
    1.62 +		}
    1.63 +	}
    1.64 +	return(cond);
    1.65 +}
    1.66 +
    1.67 +/* Destroy a condition variable */
    1.68 +void SDL_DestroyCond(SDL_cond *cond)
    1.69 +{
    1.70 +	if ( cond ) {
    1.71 +		pthread_cond_destroy(&cond->cond);
    1.72 +		free(cond);
    1.73 +	}
    1.74 +}
    1.75 +
    1.76 +/* Restart one of the threads that are waiting on the condition variable */
    1.77 +int SDL_CondSignal(SDL_cond *cond)
    1.78 +{
    1.79 +	int retval;
    1.80 +
    1.81 +	if ( ! cond ) {
    1.82 +		SDL_SetError("Passed a NULL condition variable");
    1.83 +		return -1;
    1.84 +	}
    1.85 +
    1.86 +	retval = 0;
    1.87 +	if ( pthread_cond_signal(&cond->cond) != 0 ) {
    1.88 +		SDL_SetError("pthread_cond_signal() failed");
    1.89 +		retval = -1;
    1.90 +	}
    1.91 +	return retval;
    1.92 +}
    1.93 +
    1.94 +/* Restart all threads that are waiting on the condition variable */
    1.95 +int SDL_CondBroadcast(SDL_cond *cond)
    1.96 +{
    1.97 +	int retval;
    1.98 +
    1.99 +	if ( ! cond ) {
   1.100 +		SDL_SetError("Passed a NULL condition variable");
   1.101 +		return -1;
   1.102 +	}
   1.103 +
   1.104 +	retval = 0;
   1.105 +	if ( pthread_cond_broadcast(&cond->cond) != 0 ) {
   1.106 +		SDL_SetError("pthread_cond_broadcast() failed");
   1.107 +		retval = -1;
   1.108 +	}
   1.109 +	return retval;
   1.110 +}
   1.111 +
   1.112 +int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
   1.113 +{
   1.114 +	int retval;
   1.115 +	struct timeval delta;
   1.116 +	struct timespec abstime;
   1.117 +
   1.118 +	if ( ! cond ) {
   1.119 +		SDL_SetError("Passed a NULL condition variable");
   1.120 +		return -1;
   1.121 +	}
   1.122 +
   1.123 +	gettimeofday(&delta, NULL);
   1.124 +
   1.125 +	abstime.tv_sec = delta.tv_sec + (ms/1000);
   1.126 +	abstime.tv_nsec = (delta.tv_usec + (ms%1000) * 1000) * 1000;
   1.127 +        if ( abstime.tv_nsec > 1000000000 ) {
   1.128 +          abstime.tv_sec += 1;
   1.129 +          abstime.tv_nsec -= 1000000000;
   1.130 +        }
   1.131 +
   1.132 +  tryagain:
   1.133 +	retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
   1.134 +	switch (retval) {
   1.135 +	    case EINTR:
   1.136 +		goto tryagain;
   1.137 +		break;
   1.138 +	    case ETIMEDOUT:
   1.139 +		retval = SDL_MUTEX_TIMEDOUT;
   1.140 +		break;
   1.141 +	    case 0:
   1.142 +		break;
   1.143 +	    default:
   1.144 +		SDL_SetError("pthread_cond_timedwait() failed");
   1.145 +		retval = -1;
   1.146 +		break;
   1.147 +	}
   1.148 +	return retval;
   1.149 +}
   1.150 +
   1.151 +/* Wait on the condition variable, unlocking the provided mutex.
   1.152 +   The mutex must be locked before entering this function!
   1.153 + */
   1.154 +int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
   1.155 +{
   1.156 +	int retval;
   1.157 +
   1.158 +	if ( ! cond ) {
   1.159 +		SDL_SetError("Passed a NULL condition variable");
   1.160 +		return -1;
   1.161 +	}
   1.162 +
   1.163 +	retval = 0;
   1.164 +	if ( pthread_cond_wait(&cond->cond, &mutex->id) != 0 ) {
   1.165 +		SDL_SetError("pthread_cond_wait() failed");
   1.166 +		retval = -1;
   1.167 +	}
   1.168 +	return retval;
   1.169 +}
   1.170 +#endif