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 <pthread.h>
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <semaphore.h>
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <errno.h>
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/time.h>
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_thread.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h"
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Wrapper around POSIX 1003.1b semaphores */
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __MACOSX__
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../generic/SDL_syssem.c"
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct SDL_semaphore {
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sem_t sem;
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Create a semaphore, initialized with value */
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( sem ) {
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( sem_init(&sem->sem, 0, initial_value) < 0 ) {
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("sem_init() failed");
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(sem);
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			sem = NULL;
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return sem;
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_DestroySemaphore(SDL_sem *sem)
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( sem ) {
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sem_destroy(&sem->sem);
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(sem);
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SemTryWait(SDL_sem *sem)
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! sem ) {
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Passed a NULL semaphore");
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = SDL_MUTEX_TIMEDOUT;
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( sem_trywait(&sem->sem) == 0 ) {
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = 0;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retval;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SemWait(SDL_sem *sem)
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! sem ) {
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Passed a NULL semaphore");
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while ( ((retval = sem_wait(&sem->sem)) == -1) && (errno == EINTR) ) {}
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( retval < 0 ) {
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("sem_wait() failed");
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retval;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_SEM_TIMEDWAIT
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct timeval now;
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct timespec ts_timeout;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 end;
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! sem ) {
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Passed a NULL semaphore");
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Try the easy cases first */
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( timeout == 0 ) {
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return SDL_SemTryWait(sem);
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( timeout == SDL_MUTEX_MAXWAIT ) {
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return SDL_SemWait(sem);
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_SEM_TIMEDWAIT
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Setup the timeout. sem_timedwait doesn't wait for
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * a lapse of time, but until we reach a certain time.
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * This time is now plus the timeout.
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gettimeofday(&now, NULL);
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Add our timeout to current time */
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	now.tv_usec += (timeout % 1000) * 1000;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	now.tv_sec += timeout / 1000;
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Wrap the second if needed */
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( now.tv_usec >= 1000000 ) {
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		now.tv_usec -= 1000000;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		now.tv_sec ++;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Convert to timespec */
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ts_timeout.tv_sec = now.tv_sec;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ts_timeout.tv_nsec = now.tv_usec * 1000;
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Wait. */
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	do
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = sem_timedwait(&sem->sem, &ts_timeout);
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while (retval == -1 && errno == EINTR);
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (retval == -1)
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError(strerror(errno));
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	end = SDL_GetTicks() + timeout;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) {
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((SDL_GetTicks() - end) >= 0) {
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Delay(0);
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* HAVE_SEM_TIMEDWAIT */
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retval;
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint32 SDL_SemValue(SDL_sem *sem)
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int ret = 0;
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( sem ) {
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sem_getvalue(&sem->sem, &ret);
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ret < 0 ) {
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			ret = 0;
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return (Uint32)ret;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SemPost(SDL_sem *sem)
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! sem ) {
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Passed a NULL semaphore");
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = sem_post(&sem->sem);
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( retval < 0 ) {
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("sem_post() failed");
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retval;
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* __MACOSX__ */
191