comparison src/timer/unix/SDL_systimer.c @ 1361:19418e4422cb

New configure-based build system. Still work in progress, but much improved
author Sam Lantinga <slouken@libsdl.org>
date Thu, 16 Feb 2006 10:11:48 +0000
parents
children 1736c5e2173f
comparison
equal deleted inserted replaced
1360:70a9cfb4cf1b 1361:19418e4422cb
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2006 Sam Lantinga
4
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.
9
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.
14
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
18
19 Sam Lantinga
20 slouken@libsdl.org
21 */
22
23 #include <stdio.h>
24 #include <sys/time.h>
25 #include <signal.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29
30 #include "SDL_timer.h"
31 #include "../SDL_timer_c.h"
32
33 /* The clock_gettime provides monotonous time, so we should use it if
34 it's available. The clock_gettime function is behind ifdef
35 for __USE_POSIX199309
36 Tommi Kyntola (tommi.kyntola@ray.fi) 27/09/2005
37 */
38 #if HAVE_CLOCK_GETTIME
39 #include <time.h>
40 #endif
41
42 #if SDL_THREAD_PTH
43 #include <pth.h>
44 #elif _POSIX_THREAD_SYSCALL_SOFT
45 #include <pthread.h>
46 #endif
47
48 #if SDL_THREADS_DISABLED
49 #define USE_ITIMER
50 #endif
51
52 /* The first ticks value of the application */
53 #ifdef HAVE_CLOCK_GETTIME
54 static struct timespec start;
55 #else
56 static struct timeval start;
57 #endif /* HAVE_CLOCK_GETTIME */
58
59
60 void SDL_StartTicks(void)
61 {
62 /* Set first ticks value */
63 #if HAVE_CLOCK_GETTIME
64 clock_gettime(CLOCK_MONOTONIC,&start);
65 #else
66 gettimeofday(&start, NULL);
67 #endif
68 }
69
70 Uint32 SDL_GetTicks (void)
71 {
72 #if HAVE_CLOCK_GETTIME
73 Uint32 ticks;
74 struct timespec now;
75 clock_gettime(CLOCK_MONOTONIC,&now);
76 ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_nsec-start.tv_nsec)/1000000;
77 return(ticks);
78 #else
79 Uint32 ticks;
80 struct timeval now;
81 gettimeofday(&now, NULL);
82 ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000;
83 return(ticks);
84 #endif
85 }
86
87 void SDL_Delay (Uint32 ms)
88 {
89 #if SDL_THREAD_PTH
90 pth_time_t tv;
91 tv.tv_sec = ms/1000;
92 tv.tv_usec = (ms%1000)*1000;
93 pth_nap(tv);
94 #else
95 int was_error;
96
97 #if HAVE_NANOSLEEP
98 struct timespec elapsed, tv;
99 #else
100 struct timeval tv;
101 Uint32 then, now, elapsed;
102 #endif
103
104 /* Set the timeout interval */
105 #if HAVE_NANOSLEEP
106 elapsed.tv_sec = ms/1000;
107 elapsed.tv_nsec = (ms%1000)*1000000;
108 #else
109 then = SDL_GetTicks();
110 #endif
111 do {
112 errno = 0;
113
114 #if _POSIX_THREAD_SYSCALL_SOFT
115 pthread_yield_np();
116 #endif
117 #if HAVE_NANOSLEEP
118 tv.tv_sec = elapsed.tv_sec;
119 tv.tv_nsec = elapsed.tv_nsec;
120 was_error = nanosleep(&tv, &elapsed);
121 #else
122 /* Calculate the time interval left (in case of interrupt) */
123 now = SDL_GetTicks();
124 elapsed = (now-then);
125 then = now;
126 if ( elapsed >= ms ) {
127 break;
128 }
129 ms -= elapsed;
130 tv.tv_sec = ms/1000;
131 tv.tv_usec = (ms%1000)*1000;
132
133 was_error = select(0, NULL, NULL, NULL, &tv);
134 #endif /* HAVE_NANOSLEEP */
135 } while ( was_error && (errno == EINTR) );
136 #endif /* SDL_THREAD_PTH */
137 }
138
139 #ifdef USE_ITIMER
140
141 static void HandleAlarm(int sig)
142 {
143 Uint32 ms;
144
145 if ( SDL_alarm_callback ) {
146 ms = (*SDL_alarm_callback)(SDL_alarm_interval);
147 if ( ms != SDL_alarm_interval ) {
148 SDL_SetTimer(ms, SDL_alarm_callback);
149 }
150 }
151 }
152
153 int SDL_SYS_TimerInit(void)
154 {
155 struct sigaction action;
156
157 /* Set the alarm handler (Linux specific) */
158 SDL_memset(&action, 0, sizeof(action));
159 action.sa_handler = HandleAlarm;
160 action.sa_flags = SA_RESTART;
161 sigemptyset(&action.sa_mask);
162 sigaction(SIGALRM, &action, NULL);
163 return(0);
164 }
165
166 void SDL_SYS_TimerQuit(void)
167 {
168 SDL_SetTimer(0, NULL);
169 }
170
171 int SDL_SYS_StartTimer(void)
172 {
173 struct itimerval timer;
174
175 timer.it_value.tv_sec = (SDL_alarm_interval/1000);
176 timer.it_value.tv_usec = (SDL_alarm_interval%1000)*1000;
177 timer.it_interval.tv_sec = (SDL_alarm_interval/1000);
178 timer.it_interval.tv_usec = (SDL_alarm_interval%1000)*1000;
179 setitimer(ITIMER_REAL, &timer, NULL);
180 return(0);
181 }
182
183 void SDL_SYS_StopTimer(void)
184 {
185 struct itimerval timer;
186
187 SDL_memset(&timer, 0, (sizeof timer));
188 setitimer(ITIMER_REAL, &timer, NULL);
189 }
190
191 #else /* USE_ITIMER */
192
193 #include "SDL_thread.h"
194
195 /* Data to handle a single periodic alarm */
196 static int timer_alive = 0;
197 static SDL_Thread *timer = NULL;
198
199 static int RunTimer(void *unused)
200 {
201 while ( timer_alive ) {
202 if ( SDL_timer_running ) {
203 SDL_ThreadedTimerCheck();
204 }
205 SDL_Delay(1);
206 }
207 return(0);
208 }
209
210 /* This is only called if the event thread is not running */
211 int SDL_SYS_TimerInit(void)
212 {
213 timer_alive = 1;
214 timer = SDL_CreateThread(RunTimer, NULL);
215 if ( timer == NULL )
216 return(-1);
217 return(SDL_SetTimerThreaded(1));
218 }
219
220 void SDL_SYS_TimerQuit(void)
221 {
222 timer_alive = 0;
223 if ( timer ) {
224 SDL_WaitThread(timer, NULL);
225 timer = NULL;
226 }
227 }
228
229 int SDL_SYS_StartTimer(void)
230 {
231 SDL_SetError("Internal logic error: Linux uses threaded timer");
232 return(-1);
233 }
234
235 void SDL_SYS_StopTimer(void)
236 {
237 return;
238 }
239
240 #endif /* USE_ITIMER */