comparison src/timer/SDL_timer.c @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
53 53
54 /* Set whether or not the timer should use a thread. 54 /* Set whether or not the timer should use a thread.
55 This should not be called while the timer subsystem is running. 55 This should not be called while the timer subsystem is running.
56 */ 56 */
57 int 57 int
58 SDL_SetTimerThreaded (int value) 58 SDL_SetTimerThreaded(int value)
59 { 59 {
60 int retval; 60 int retval;
61 61
62 if (SDL_timer_started) { 62 if (SDL_timer_started) {
63 SDL_SetError ("Timer already initialized"); 63 SDL_SetError("Timer already initialized");
64 retval = -1; 64 retval = -1;
65 } else { 65 } else {
66 retval = 0; 66 retval = 0;
67 SDL_timer_threaded = value; 67 SDL_timer_threaded = value;
68 } 68 }
69 return retval; 69 return retval;
70 } 70 }
71 71
72 int 72 int
73 SDL_TimerInit (void) 73 SDL_TimerInit(void)
74 { 74 {
75 int retval; 75 int retval;
76 76
77 retval = 0; 77 retval = 0;
78 if (SDL_timer_started) { 78 if (SDL_timer_started) {
79 SDL_TimerQuit (); 79 SDL_TimerQuit();
80 } 80 }
81 if (!SDL_timer_threaded) { 81 if (!SDL_timer_threaded) {
82 retval = SDL_SYS_TimerInit (); 82 retval = SDL_SYS_TimerInit();
83 } 83 }
84 if (SDL_timer_threaded) { 84 if (SDL_timer_threaded) {
85 SDL_timer_mutex = SDL_CreateMutex (); 85 SDL_timer_mutex = SDL_CreateMutex();
86 } 86 }
87 if (retval == 0) { 87 if (retval == 0) {
88 SDL_timer_started = 1; 88 SDL_timer_started = 1;
89 } 89 }
90 return (retval); 90 return (retval);
91 } 91 }
92 92
93 void 93 void
94 SDL_TimerQuit (void) 94 SDL_TimerQuit(void)
95 { 95 {
96 SDL_SetTimer (0, NULL); 96 SDL_SetTimer(0, NULL);
97 if (SDL_timer_threaded < 2) { 97 if (SDL_timer_threaded < 2) {
98 SDL_SYS_TimerQuit (); 98 SDL_SYS_TimerQuit();
99 } 99 }
100 if (SDL_timer_threaded) { 100 if (SDL_timer_threaded) {
101 SDL_DestroyMutex (SDL_timer_mutex); 101 SDL_DestroyMutex(SDL_timer_mutex);
102 SDL_timer_mutex = NULL; 102 SDL_timer_mutex = NULL;
103 } 103 }
104 SDL_timer_started = 0; 104 SDL_timer_started = 0;
105 SDL_timer_threaded = 0; 105 SDL_timer_threaded = 0;
106 } 106 }
107 107
108 void 108 void
109 SDL_ThreadedTimerCheck (void) 109 SDL_ThreadedTimerCheck(void)
110 { 110 {
111 Uint32 now, ms; 111 Uint32 now, ms;
112 SDL_TimerID t, prev, next; 112 SDL_TimerID t, prev, next;
113 SDL_bool removed; 113 SDL_bool removed;
114 114
115 SDL_mutexP (SDL_timer_mutex); 115 SDL_mutexP(SDL_timer_mutex);
116 list_changed = SDL_FALSE; 116 list_changed = SDL_FALSE;
117 now = SDL_GetTicks (); 117 now = SDL_GetTicks();
118 for (prev = NULL, t = SDL_timers; t; t = next) { 118 for (prev = NULL, t = SDL_timers; t; t = next) {
119 removed = SDL_FALSE; 119 removed = SDL_FALSE;
120 ms = t->interval - SDL_TIMESLICE; 120 ms = t->interval - SDL_TIMESLICE;
121 next = t->next; 121 next = t->next;
122 if ((int) (now - t->last_alarm) > (int) ms) { 122 if ((int) (now - t->last_alarm) > (int) ms) {
126 t->last_alarm += t->interval; 126 t->last_alarm += t->interval;
127 } else { 127 } else {
128 t->last_alarm = now; 128 t->last_alarm = now;
129 } 129 }
130 #ifdef DEBUG_TIMERS 130 #ifdef DEBUG_TIMERS
131 printf ("Executing timer %p (thread = %d)\n", t, SDL_ThreadID ()); 131 printf("Executing timer %p (thread = %d)\n", t, SDL_ThreadID());
132 #endif 132 #endif
133 timer = *t; 133 timer = *t;
134 SDL_mutexV (SDL_timer_mutex); 134 SDL_mutexV(SDL_timer_mutex);
135 ms = timer.cb (timer.interval, timer.param); 135 ms = timer.cb(timer.interval, timer.param);
136 SDL_mutexP (SDL_timer_mutex); 136 SDL_mutexP(SDL_timer_mutex);
137 if (list_changed) { 137 if (list_changed) {
138 /* Abort, list of timers modified */ 138 /* Abort, list of timers modified */
139 /* FIXME: what if ms was changed? */ 139 /* FIXME: what if ms was changed? */
140 break; 140 break;
141 } 141 }
142 if (ms != t->interval) { 142 if (ms != t->interval) {
143 if (ms) { 143 if (ms) {
144 t->interval = ROUND_RESOLUTION (ms); 144 t->interval = ROUND_RESOLUTION(ms);
145 } else { 145 } else {
146 /* Remove timer from the list */ 146 /* Remove timer from the list */
147 #ifdef DEBUG_TIMERS 147 #ifdef DEBUG_TIMERS
148 printf ("SDL: Removing timer %p\n", t); 148 printf("SDL: Removing timer %p\n", t);
149 #endif 149 #endif
150 if (prev) { 150 if (prev) {
151 prev->next = next; 151 prev->next = next;
152 } else { 152 } else {
153 SDL_timers = next; 153 SDL_timers = next;
154 } 154 }
155 SDL_free (t); 155 SDL_free(t);
156 --SDL_timer_running; 156 --SDL_timer_running;
157 removed = SDL_TRUE; 157 removed = SDL_TRUE;
158 } 158 }
159 } 159 }
160 } 160 }
161 /* Don't update prev if the timer has disappeared */ 161 /* Don't update prev if the timer has disappeared */
162 if (!removed) { 162 if (!removed) {
163 prev = t; 163 prev = t;
164 } 164 }
165 } 165 }
166 SDL_mutexV (SDL_timer_mutex); 166 SDL_mutexV(SDL_timer_mutex);
167 } 167 }
168 168
169 static SDL_TimerID 169 static SDL_TimerID
170 SDL_AddTimerInternal (Uint32 interval, SDL_NewTimerCallback callback, 170 SDL_AddTimerInternal(Uint32 interval, SDL_NewTimerCallback callback,
171 void *param) 171 void *param)
172 { 172 {
173 SDL_TimerID t; 173 SDL_TimerID t;
174 t = (SDL_TimerID) SDL_malloc (sizeof (struct _SDL_TimerID)); 174 t = (SDL_TimerID) SDL_malloc(sizeof(struct _SDL_TimerID));
175 if (t) { 175 if (t) {
176 t->interval = ROUND_RESOLUTION (interval); 176 t->interval = ROUND_RESOLUTION(interval);
177 t->cb = callback; 177 t->cb = callback;
178 t->param = param; 178 t->param = param;
179 t->last_alarm = SDL_GetTicks (); 179 t->last_alarm = SDL_GetTicks();
180 t->next = SDL_timers; 180 t->next = SDL_timers;
181 SDL_timers = t; 181 SDL_timers = t;
182 ++SDL_timer_running; 182 ++SDL_timer_running;
183 list_changed = SDL_TRUE; 183 list_changed = SDL_TRUE;
184 } 184 }
185 #ifdef DEBUG_TIMERS 185 #ifdef DEBUG_TIMERS
186 printf ("SDL_AddTimer(%d) = %08x num_timers = %d\n", interval, (Uint32) t, 186 printf("SDL_AddTimer(%d) = %08x num_timers = %d\n", interval, (Uint32) t,
187 SDL_timer_running); 187 SDL_timer_running);
188 #endif 188 #endif
189 return t; 189 return t;
190 } 190 }
191 191
192 SDL_TimerID 192 SDL_TimerID
193 SDL_AddTimer (Uint32 interval, SDL_NewTimerCallback callback, void *param) 193 SDL_AddTimer(Uint32 interval, SDL_NewTimerCallback callback, void *param)
194 { 194 {
195 SDL_TimerID t; 195 SDL_TimerID t;
196 if (!SDL_timer_mutex) { 196 if (!SDL_timer_mutex) {
197 if (SDL_timer_started) { 197 if (SDL_timer_started) {
198 SDL_SetError ("This platform doesn't support multiple timers"); 198 SDL_SetError("This platform doesn't support multiple timers");
199 } else { 199 } else {
200 SDL_SetError ("You must call SDL_Init(SDL_INIT_TIMER) first"); 200 SDL_SetError("You must call SDL_Init(SDL_INIT_TIMER) first");
201 } 201 }
202 return NULL; 202 return NULL;
203 } 203 }
204 if (!SDL_timer_threaded) { 204 if (!SDL_timer_threaded) {
205 SDL_SetError ("Multiple timers require threaded events!"); 205 SDL_SetError("Multiple timers require threaded events!");
206 return NULL; 206 return NULL;
207 } 207 }
208 SDL_mutexP (SDL_timer_mutex); 208 SDL_mutexP(SDL_timer_mutex);
209 t = SDL_AddTimerInternal (interval, callback, param); 209 t = SDL_AddTimerInternal(interval, callback, param);
210 SDL_mutexV (SDL_timer_mutex); 210 SDL_mutexV(SDL_timer_mutex);
211 return t; 211 return t;
212 } 212 }
213 213
214 SDL_bool 214 SDL_bool
215 SDL_RemoveTimer (SDL_TimerID id) 215 SDL_RemoveTimer(SDL_TimerID id)
216 { 216 {
217 SDL_TimerID t, prev = NULL; 217 SDL_TimerID t, prev = NULL;
218 SDL_bool removed; 218 SDL_bool removed;
219 219
220 removed = SDL_FALSE; 220 removed = SDL_FALSE;
221 SDL_mutexP (SDL_timer_mutex); 221 SDL_mutexP(SDL_timer_mutex);
222 /* Look for id in the linked list of timers */ 222 /* Look for id in the linked list of timers */
223 for (t = SDL_timers; t; prev = t, t = t->next) { 223 for (t = SDL_timers; t; prev = t, t = t->next) {
224 if (t == id) { 224 if (t == id) {
225 if (prev) { 225 if (prev) {
226 prev->next = t->next; 226 prev->next = t->next;
227 } else { 227 } else {
228 SDL_timers = t->next; 228 SDL_timers = t->next;
229 } 229 }
230 SDL_free (t); 230 SDL_free(t);
231 --SDL_timer_running; 231 --SDL_timer_running;
232 removed = SDL_TRUE; 232 removed = SDL_TRUE;
233 list_changed = SDL_TRUE; 233 list_changed = SDL_TRUE;
234 break; 234 break;
235 } 235 }
236 } 236 }
237 #ifdef DEBUG_TIMERS 237 #ifdef DEBUG_TIMERS
238 printf ("SDL_RemoveTimer(%08x) = %d num_timers = %d thread = %d\n", 238 printf("SDL_RemoveTimer(%08x) = %d num_timers = %d thread = %d\n",
239 (Uint32) id, removed, SDL_timer_running, SDL_ThreadID ()); 239 (Uint32) id, removed, SDL_timer_running, SDL_ThreadID());
240 #endif 240 #endif
241 SDL_mutexV (SDL_timer_mutex); 241 SDL_mutexV(SDL_timer_mutex);
242 return removed; 242 return removed;
243 } 243 }
244 244
245 /* Old style callback functions are wrapped through this */ 245 /* Old style callback functions are wrapped through this */
246 static Uint32 SDLCALL 246 static Uint32 SDLCALL
247 callback_wrapper (Uint32 ms, void *param) 247 callback_wrapper(Uint32 ms, void *param)
248 { 248 {
249 SDL_TimerCallback func = (SDL_TimerCallback) param; 249 SDL_TimerCallback func = (SDL_TimerCallback) param;
250 return (*func) (ms); 250 return (*func) (ms);
251 } 251 }
252 252
253 int 253 int
254 SDL_SetTimer (Uint32 ms, SDL_TimerCallback callback) 254 SDL_SetTimer(Uint32 ms, SDL_TimerCallback callback)
255 { 255 {
256 int retval; 256 int retval;
257 257
258 #ifdef DEBUG_TIMERS 258 #ifdef DEBUG_TIMERS
259 printf ("SDL_SetTimer(%d)\n", ms); 259 printf("SDL_SetTimer(%d)\n", ms);
260 #endif 260 #endif
261 retval = 0; 261 retval = 0;
262 262
263 if (SDL_timer_threaded) { 263 if (SDL_timer_threaded) {
264 SDL_mutexP (SDL_timer_mutex); 264 SDL_mutexP(SDL_timer_mutex);
265 } 265 }
266 if (SDL_timer_running) { /* Stop any currently running timer */ 266 if (SDL_timer_running) { /* Stop any currently running timer */
267 if (SDL_timer_threaded) { 267 if (SDL_timer_threaded) {
268 while (SDL_timers) { 268 while (SDL_timers) {
269 SDL_TimerID freeme = SDL_timers; 269 SDL_TimerID freeme = SDL_timers;
270 SDL_timers = SDL_timers->next; 270 SDL_timers = SDL_timers->next;
271 SDL_free (freeme); 271 SDL_free(freeme);
272 } 272 }
273 SDL_timer_running = 0; 273 SDL_timer_running = 0;
274 list_changed = SDL_TRUE; 274 list_changed = SDL_TRUE;
275 } else { 275 } else {
276 SDL_SYS_StopTimer (); 276 SDL_SYS_StopTimer();
277 SDL_timer_running = 0; 277 SDL_timer_running = 0;
278 } 278 }
279 } 279 }
280 if (ms) { 280 if (ms) {
281 if (SDL_timer_threaded) { 281 if (SDL_timer_threaded) {
285 } 285 }
286 } else { 286 } else {
287 SDL_timer_running = 1; 287 SDL_timer_running = 1;
288 SDL_alarm_interval = ms; 288 SDL_alarm_interval = ms;
289 SDL_alarm_callback = callback; 289 SDL_alarm_callback = callback;
290 retval = SDL_SYS_StartTimer (); 290 retval = SDL_SYS_StartTimer();
291 } 291 }
292 } 292 }
293 if (SDL_timer_threaded) { 293 if (SDL_timer_threaded) {
294 SDL_mutexV (SDL_timer_mutex); 294 SDL_mutexV(SDL_timer_mutex);
295 } 295 }
296 296
297 return retval; 297 return retval;
298 } 298 }
299 299