19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* RISC OS implementations uses pthreads based on linux code */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_THREADS_DISABLED
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../generic/SDL_syscond.c"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/time.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <pthread.h>
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_thread.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_sysmutex_c.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct SDL_cond
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pthread_cond_t cond;
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Create a condition variable */
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_cond * SDL_CreateCond(void)
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_cond *cond;
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( cond ) {
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( pthread_cond_init(&cond->cond, NULL) < 0 ) {
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("pthread_cond_init() failed");
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(cond);
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			cond = NULL;
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(cond);
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Destroy a condition variable */
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_DestroyCond(SDL_cond *cond)
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( cond ) {
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pthread_cond_destroy(&cond->cond);
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(cond);
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Restart one of the threads that are waiting on the condition variable */
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_CondSignal(SDL_cond *cond)
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! cond ) {
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Passed a NULL condition variable");
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = 0;
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( pthread_cond_signal(&cond->cond) != 0 ) {
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("pthread_cond_signal() failed");
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = -1;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retval;
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Restart all threads that are waiting on the condition variable */
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_CondBroadcast(SDL_cond *cond)
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! cond ) {
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Passed a NULL condition variable");
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = 0;
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( pthread_cond_broadcast(&cond->cond) != 0 ) {
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("pthread_cond_broadcast() failed");
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = -1;
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retval;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct timeval delta;
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct timespec abstime;
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! cond ) {
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Passed a NULL condition variable");
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gettimeofday(&delta, NULL);
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	abstime.tv_sec = delta.tv_sec + (ms/1000);
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	abstime.tv_nsec = (delta.tv_usec + (ms%1000) * 1000) * 1000;
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( abstime.tv_nsec > 1000000000 ) {
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          abstime.tv_sec += 1;
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          abstime.tv_nsec -= 1000000000;
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  tryagain:
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (retval) {
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case EINTR:
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		goto tryagain;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case ETIMEDOUT:
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = SDL_MUTEX_TIMEDOUT;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case 0:
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    default:
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("pthread_cond_timedwait() failed");
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = -1;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retval;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Wait on the condition variable, unlocking the provided mutex.
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   The mutex must be locked before entering this function!
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! cond ) {
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Passed a NULL condition variable");
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = 0;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( pthread_cond_wait(&cond->cond, &mutex->id) != 0 ) {
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("pthread_cond_wait() failed");
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = -1;
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retval;
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
161