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#ifdef SDL_TIMER_UNIX
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <stdio.h>
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/time.h>
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <signal.h>
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <string.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <errno.h>
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_timer_c.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The clock_gettime provides monotonous time, so we should use it if
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   it's available. The clock_gettime function is behind ifdef
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   for __USE_POSIX199309
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   Tommi Kyntola (tommi.kyntola@ray.fi) 27/09/2005
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if HAVE_NANOSLEEP || HAVE_CLOCK_GETTIME
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <time.h>
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_THREAD_PTH
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <pth.h>
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_THREADS_DISABLED
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define USE_ITIMER
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The first ticks value of the application */
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_CLOCK_GETTIME
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic struct timespec start;
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic struct timeval start;
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* HAVE_CLOCK_GETTIME */
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_StartTicks(void)
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set first ticks value */
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if HAVE_CLOCK_GETTIME
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	clock_gettime(CLOCK_MONOTONIC,&start);
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gettimeofday(&start, NULL);
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint32 SDL_GetTicks (void)
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if HAVE_CLOCK_GETTIME
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 ticks;
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct timespec now;
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	clock_gettime(CLOCK_MONOTONIC,&now);
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_nsec-start.tv_nsec)/1000000;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(ticks);
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 ticks;
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct timeval now;
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gettimeofday(&now, NULL);
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(ticks);
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_Delay (Uint32 ms)
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_THREAD_PTH
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pth_time_t tv;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tv.tv_sec  =  ms/1000;
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tv.tv_usec = (ms%1000)*1000;
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pth_nap(tv);
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int was_error;
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if HAVE_NANOSLEEP
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct timespec elapsed, tv;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct timeval tv;
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 then, now, elapsed;
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the timeout interval */
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if HAVE_NANOSLEEP
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	elapsed.tv_sec = ms/1000;
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	elapsed.tv_nsec = (ms%1000)*1000000;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	then = SDL_GetTicks();
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	do {
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		errno = 0;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if HAVE_NANOSLEEP
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		tv.tv_sec = elapsed.tv_sec;
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		tv.tv_nsec = elapsed.tv_nsec;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		was_error = nanosleep(&tv, &elapsed);
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Calculate the time interval left (in case of interrupt) */
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		now = SDL_GetTicks();
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		elapsed = (now-then);
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		then = now;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( elapsed >= ms ) {
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ms -= elapsed;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		tv.tv_sec = ms/1000;
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		tv.tv_usec = (ms%1000)*1000;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		was_error = select(0, NULL, NULL, NULL, &tv);
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* HAVE_NANOSLEEP */
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} while ( was_error && (errno == EINTR) );
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_THREAD_PTH */
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_ITIMER
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void HandleAlarm(int sig)
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 ms;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_alarm_callback ) {
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ms = (*SDL_alarm_callback)(SDL_alarm_interval);
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ms != SDL_alarm_interval ) {
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetTimer(ms, SDL_alarm_callback);
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SYS_TimerInit(void)
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct sigaction action;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the alarm handler (Linux specific) */
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(&action, 0, sizeof(action));
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	action.sa_handler = HandleAlarm;
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	action.sa_flags = SA_RESTART;
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sigemptyset(&action.sa_mask);
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sigaction(SIGALRM, &action, NULL);
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_TimerQuit(void)
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetTimer(0, NULL);
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SYS_StartTimer(void)
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct itimerval timer;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	timer.it_value.tv_sec = (SDL_alarm_interval/1000);
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	timer.it_value.tv_usec = (SDL_alarm_interval%1000)*1000;
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	timer.it_interval.tv_sec = (SDL_alarm_interval/1000);
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	timer.it_interval.tv_usec = (SDL_alarm_interval%1000)*1000;
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	setitimer(ITIMER_REAL, &timer, NULL);
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_StopTimer(void)
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct itimerval timer;
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(&timer, 0, (sizeof timer));
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	setitimer(ITIMER_REAL, &timer, NULL);
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else /* USE_ITIMER */
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_thread.h"
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Data to handle a single periodic alarm */
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int timer_alive = 0;
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Thread *timer = NULL;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int RunTimer(void *unused)
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while ( timer_alive ) {
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_timer_running ) {
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_ThreadedTimerCheck();
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Delay(1);
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This is only called if the event thread is not running */
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SYS_TimerInit(void)
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	timer_alive = 1;
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	timer = SDL_CreateThread(RunTimer, NULL);
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( timer == NULL )
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_SetTimerThreaded(1));
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_TimerQuit(void)
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	timer_alive = 0;
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( timer ) {
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WaitThread(timer, NULL);
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		timer = NULL;
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SYS_StartTimer(void)
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetError("Internal logic error: Linux uses threaded timer");
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(-1);
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_StopTimer(void)
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* USE_ITIMER */
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_TIMER_UNIX */
241