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#include <sys/time.h>
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <errno.h>
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <pthread.h>
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_thread.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_sysmutex_c.h"
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct SDL_cond
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pthread_cond_t cond;
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Create a condition variable */
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_cond * SDL_CreateCond(void)
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_cond *cond;
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( cond ) {
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( pthread_cond_init(&cond->cond, NULL) < 0 ) {
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("pthread_cond_init() failed");
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(cond);
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			cond = NULL;
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(cond);
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Destroy a condition variable */
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_DestroyCond(SDL_cond *cond)
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( cond ) {
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pthread_cond_destroy(&cond->cond);
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(cond);
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Restart one of the threads that are waiting on the condition variable */
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_CondSignal(SDL_cond *cond)
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! cond ) {
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Passed a NULL condition variable");
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = 0;
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( pthread_cond_signal(&cond->cond) != 0 ) {
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("pthread_cond_signal() failed");
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = -1;
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retval;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Restart all threads that are waiting on the condition variable */
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_CondBroadcast(SDL_cond *cond)
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! cond ) {
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Passed a NULL condition variable");
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = 0;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( pthread_cond_broadcast(&cond->cond) != 0 ) {
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("pthread_cond_broadcast() failed");
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = -1;
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retval;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct timeval delta;
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct timespec abstime;
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! cond ) {
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Passed a NULL condition variable");
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gettimeofday(&delta, NULL);
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	abstime.tv_sec = delta.tv_sec + (ms/1000);
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	abstime.tv_nsec = (delta.tv_usec + (ms%1000) * 1000) * 1000;
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( abstime.tv_nsec > 1000000000 ) {
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          abstime.tv_sec += 1;
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          abstime.tv_nsec -= 1000000000;
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  tryagain:
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (retval) {
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case EINTR:
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		goto tryagain;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case ETIMEDOUT:
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = SDL_MUTEX_TIMEDOUT;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case 0:
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    default:
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("pthread_cond_timedwait() failed");
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = -1;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retval;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Wait on the condition variable, unlocking the provided mutex.
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   The mutex must be locked before entering this function!
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! cond ) {
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Passed a NULL condition variable");
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = 0;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( pthread_cond_wait(&cond->cond, &mutex->id) != 0 ) {
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("pthread_cond_wait() failed");
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = -1;
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retval;
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
156