Mercurial > sdl-ios-xcode
annotate src/timer/linux/SDL_systimer.c @ 297:f6ffac90895c
Updated copyright information for 2002
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 06 Mar 2002 11:23:08 +0000 |
parents | e8157fcb3114 |
children | d85fc19bf840 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
297
f6ffac90895c
Updated copyright information for 2002
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 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 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
166
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 #ifdef SAVE_RCSID | |
24 static char rcsid = | |
25 "@(#) $Id$"; | |
26 #endif | |
27 | |
28 #include <stdio.h> | |
29 #include <sys/time.h> | |
30 #include <signal.h> | |
31 #include <unistd.h> | |
32 #include <string.h> | |
33 #include <errno.h> | |
34 | |
35 #include "SDL_error.h" | |
36 #include "SDL_timer.h" | |
37 #include "SDL_timer_c.h" | |
38 | |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
39 #if _POSIX_THREAD_SYSCALL_SOFT |
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
40 #include <pthread.h> |
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
41 #endif |
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
42 |
0 | 43 #if defined(DISABLE_THREADS) || defined(FORK_HACK) |
44 #define USE_ITIMER | |
45 #endif | |
46 | |
47 /* The following defines should really be determined at configure time */ | |
48 | |
166
39877400bd1e
Fixed Solaris nitpicks (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
1
diff
changeset
|
49 #if defined(linux) |
0 | 50 /* Linux select() changes its timeout parameter upon return to contain |
51 the remaining time. Most other unixen leave it unchanged or undefined. */ | |
52 #define SELECT_SETS_REMAINING | |
166
39877400bd1e
Fixed Solaris nitpicks (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
1
diff
changeset
|
53 #elif defined(__bsdi__) || defined(__FreeBSD__) || defined(__sun) |
0 | 54 #define USE_NANOSLEEP |
55 #endif | |
56 | |
57 | |
58 /* The first ticks value of the application */ | |
59 static struct timeval start; | |
60 | |
61 void SDL_StartTicks(void) | |
62 { | |
63 /* Set first ticks value */ | |
64 gettimeofday(&start, NULL); | |
65 } | |
66 | |
67 Uint32 SDL_GetTicks (void) | |
68 { | |
69 struct timeval now; | |
70 Uint32 ticks; | |
71 | |
72 gettimeofday(&now, NULL); | |
73 ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000; | |
74 return(ticks); | |
75 } | |
76 | |
77 void SDL_Delay (Uint32 ms) | |
78 { | |
79 int was_error; | |
80 | |
81 #ifdef USE_NANOSLEEP | |
82 struct timespec elapsed, tv; | |
83 #else | |
84 struct timeval tv; | |
85 #ifndef SELECT_SETS_REMAINING | |
86 Uint32 then, now, elapsed; | |
87 #endif | |
88 #endif | |
89 | |
90 /* Set the timeout interval - Linux only needs to do this once */ | |
91 #ifdef SELECT_SETS_REMAINING | |
92 tv.tv_sec = ms/1000; | |
93 tv.tv_usec = (ms%1000)*1000; | |
94 #elif defined(USE_NANOSLEEP) | |
95 elapsed.tv_sec = ms/1000; | |
96 elapsed.tv_nsec = (ms%1000)*1000000; | |
97 #else | |
98 then = SDL_GetTicks(); | |
99 #endif | |
100 do { | |
101 errno = 0; | |
102 | |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
103 #if _POSIX_THREAD_SYSCALL_SOFT |
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
104 pthread_yield_np(); |
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
105 #endif |
0 | 106 #ifdef USE_NANOSLEEP |
107 tv.tv_sec = elapsed.tv_sec; | |
108 tv.tv_nsec = elapsed.tv_nsec; | |
109 was_error = nanosleep(&tv, &elapsed); | |
110 #else | |
111 #ifndef SELECT_SETS_REMAINING | |
112 /* Calculate the time interval left (in case of interrupt) */ | |
113 now = SDL_GetTicks(); | |
114 elapsed = (now-then); | |
115 then = now; | |
116 if ( elapsed >= ms ) { | |
117 break; | |
118 } | |
119 ms -= elapsed; | |
120 tv.tv_sec = ms/1000; | |
121 tv.tv_usec = (ms%1000)*1000; | |
122 #endif | |
123 was_error = select(0, NULL, NULL, NULL, &tv); | |
124 #endif /* USE_NANOSLEEP */ | |
125 } while ( was_error && (errno == EINTR) ); | |
126 } | |
127 | |
128 #ifdef USE_ITIMER | |
129 | |
130 static void HandleAlarm(int sig) | |
131 { | |
132 Uint32 ms; | |
133 | |
134 if ( SDL_alarm_callback ) { | |
135 ms = (*SDL_alarm_callback)(SDL_alarm_interval); | |
136 if ( ms != SDL_alarm_interval ) { | |
137 SDL_SetTimer(ms, SDL_alarm_callback); | |
138 } | |
139 } | |
140 } | |
141 | |
142 int SDL_SYS_TimerInit(void) | |
143 { | |
144 struct sigaction action; | |
145 | |
146 /* Set the alarm handler (Linux specific) */ | |
147 memset(&action, 0, sizeof(action)); | |
148 action.sa_handler = HandleAlarm; | |
149 action.sa_flags = SA_RESTART; | |
150 sigemptyset(&action.sa_mask); | |
151 sigaction(SIGALRM, &action, NULL); | |
152 return(0); | |
153 } | |
154 | |
155 void SDL_SYS_TimerQuit(void) | |
156 { | |
157 SDL_SetTimer(0, NULL); | |
158 } | |
159 | |
160 int SDL_SYS_StartTimer(void) | |
161 { | |
162 struct itimerval timer; | |
163 | |
164 timer.it_value.tv_sec = (SDL_alarm_interval/1000); | |
165 timer.it_value.tv_usec = (SDL_alarm_interval%1000)*1000; | |
166 timer.it_interval.tv_sec = (SDL_alarm_interval/1000); | |
167 timer.it_interval.tv_usec = (SDL_alarm_interval%1000)*1000; | |
168 setitimer(ITIMER_REAL, &timer, NULL); | |
169 return(0); | |
170 } | |
171 | |
172 void SDL_SYS_StopTimer(void) | |
173 { | |
174 struct itimerval timer; | |
175 | |
176 memset(&timer, 0, (sizeof timer)); | |
177 setitimer(ITIMER_REAL, &timer, NULL); | |
178 } | |
179 | |
180 #else /* USE_ITIMER */ | |
181 | |
182 #include "SDL_thread.h" | |
183 | |
184 /* Data to handle a single periodic alarm */ | |
185 static int timer_alive = 0; | |
186 static SDL_Thread *timer = NULL; | |
187 | |
188 static int RunTimer(void *unused) | |
189 { | |
190 while ( timer_alive ) { | |
191 if ( SDL_timer_running ) { | |
192 SDL_ThreadedTimerCheck(); | |
193 } | |
194 SDL_Delay(1); | |
195 } | |
196 return(0); | |
197 } | |
198 | |
199 /* This is only called if the event thread is not running */ | |
200 int SDL_SYS_TimerInit(void) | |
201 { | |
202 timer_alive = 1; | |
203 timer = SDL_CreateThread(RunTimer, NULL); | |
204 if ( timer == NULL ) | |
205 return(-1); | |
206 return(SDL_SetTimerThreaded(1)); | |
207 } | |
208 | |
209 void SDL_SYS_TimerQuit(void) | |
210 { | |
211 timer_alive = 0; | |
212 if ( timer ) { | |
213 SDL_WaitThread(timer, NULL); | |
214 timer = NULL; | |
215 } | |
216 } | |
217 | |
218 int SDL_SYS_StartTimer(void) | |
219 { | |
220 SDL_SetError("Internal logic error: Linux uses threaded timer"); | |
221 return(-1); | |
222 } | |
223 | |
224 void SDL_SYS_StopTimer(void) | |
225 { | |
226 return; | |
227 } | |
228 | |
229 #endif /* USE_ITIMER */ |