2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
33 #include "SDL_timer.h"
34 #include "../SDL_timer_c.h"
36 /* The clock_gettime provides monotonous time, so we should use it if
37 it's available. The clock_gettime function is behind ifdef
39 Tommi Kyntola (tommi.kyntola@ray.fi) 27/09/2005
41 #if HAVE_NANOSLEEP || HAVE_CLOCK_GETTIME
49 #if SDL_THREADS_DISABLED
53 /* The first ticks value of the application */
54 #ifdef HAVE_CLOCK_GETTIME
55 static struct timespec start;
57 static struct timeval start;
58 #endif /* HAVE_CLOCK_GETTIME */
61 void SDL_StartTicks(void)
63 /* Set first ticks value */
64 #if HAVE_CLOCK_GETTIME
65 clock_gettime(CLOCK_MONOTONIC,&start);
67 gettimeofday(&start, NULL);
71 Uint32 SDL_GetTicks (void)
73 #if HAVE_CLOCK_GETTIME
76 clock_gettime(CLOCK_MONOTONIC,&now);
77 ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_nsec-start.tv_nsec)/1000000;
82 gettimeofday(&now, NULL);
83 ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000;
88 void SDL_Delay (Uint32 ms)
93 tv.tv_usec = (ms%1000)*1000;
99 struct timespec elapsed, tv;
102 Uint32 then, now, elapsed;
105 /* Set the timeout interval */
107 elapsed.tv_sec = ms/1000;
108 elapsed.tv_nsec = (ms%1000)*1000000;
110 then = SDL_GetTicks();
116 tv.tv_sec = elapsed.tv_sec;
117 tv.tv_nsec = elapsed.tv_nsec;
118 was_error = nanosleep(&tv, &elapsed);
120 /* Calculate the time interval left (in case of interrupt) */
121 now = SDL_GetTicks();
122 elapsed = (now-then);
124 if ( elapsed >= ms ) {
129 tv.tv_usec = (ms%1000)*1000;
131 was_error = select(0, NULL, NULL, NULL, &tv);
132 #endif /* HAVE_NANOSLEEP */
133 } while ( was_error && (errno == EINTR) );
134 #endif /* SDL_THREAD_PTH */
139 static void HandleAlarm(int sig)
143 if ( SDL_alarm_callback ) {
144 ms = (*SDL_alarm_callback)(SDL_alarm_interval);
145 if ( ms != SDL_alarm_interval ) {
146 SDL_SetTimer(ms, SDL_alarm_callback);
151 int SDL_SYS_TimerInit(void)
153 struct sigaction action;
155 /* Set the alarm handler (Linux specific) */
156 SDL_memset(&action, 0, sizeof(action));
157 action.sa_handler = HandleAlarm;
158 action.sa_flags = SA_RESTART;
159 sigemptyset(&action.sa_mask);
160 sigaction(SIGALRM, &action, NULL);
164 void SDL_SYS_TimerQuit(void)
166 SDL_SetTimer(0, NULL);
169 int SDL_SYS_StartTimer(void)
171 struct itimerval timer;
173 timer.it_value.tv_sec = (SDL_alarm_interval/1000);
174 timer.it_value.tv_usec = (SDL_alarm_interval%1000)*1000;
175 timer.it_interval.tv_sec = (SDL_alarm_interval/1000);
176 timer.it_interval.tv_usec = (SDL_alarm_interval%1000)*1000;
177 setitimer(ITIMER_REAL, &timer, NULL);
181 void SDL_SYS_StopTimer(void)
183 struct itimerval timer;
185 SDL_memset(&timer, 0, (sizeof timer));
186 setitimer(ITIMER_REAL, &timer, NULL);
189 #else /* USE_ITIMER */
191 #include "SDL_thread.h"
193 /* Data to handle a single periodic alarm */
194 static int timer_alive = 0;
195 static SDL_Thread *timer = NULL;
197 static int RunTimer(void *unused)
199 while ( timer_alive ) {
200 if ( SDL_timer_running ) {
201 SDL_ThreadedTimerCheck();
208 /* This is only called if the event thread is not running */
209 int SDL_SYS_TimerInit(void)
212 timer = SDL_CreateThread(RunTimer, NULL);
215 return(SDL_SetTimerThreaded(1));
218 void SDL_SYS_TimerQuit(void)
222 SDL_WaitThread(timer, NULL);
227 int SDL_SYS_StartTimer(void)
229 SDL_SetError("Internal logic error: Linux uses threaded timer");
233 void SDL_SYS_StopTimer(void)
238 #endif /* USE_ITIMER */
240 #endif /* SDL_TIMER_UNIX */