Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
386 lines (327 loc) · 9.96 KB

SDL_timer.c

File metadata and controls

386 lines (327 loc) · 9.96 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Jan 24, 2010
Jan 24, 2010
3
Copyright (C) 1997-2010 Sam Lantinga
Apr 26, 2001
Apr 26, 2001
4
5
This library is free software; you can redistribute it and/or
Feb 1, 2006
Feb 1, 2006
6
modify it under the terms of the GNU Lesser General Public
Apr 26, 2001
Apr 26, 2001
7
License as published by the Free Software Foundation; either
Feb 1, 2006
Feb 1, 2006
8
version 2.1 of the License, or (at your option) any later version.
Apr 26, 2001
Apr 26, 2001
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Feb 1, 2006
Feb 1, 2006
13
Lesser General Public License for more details.
Apr 26, 2001
Apr 26, 2001
14
Feb 1, 2006
Feb 1, 2006
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Apr 26, 2001
Apr 26, 2001
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Apr 26, 2001
Apr 26, 2001
21
*/
Feb 21, 2006
Feb 21, 2006
22
#include "SDL_config.h"
Apr 26, 2001
Apr 26, 2001
23
24
25
#include "SDL_timer.h"
#include "SDL_timer_c.h"
Jan 27, 2011
Jan 27, 2011
26
#include "SDL_atomic.h"
Jan 28, 2011
Jan 28, 2011
27
#include "SDL_cpuinfo.h"
Jan 27, 2011
Jan 27, 2011
28
#include "SDL_thread.h"
Apr 26, 2001
Apr 26, 2001
29
30
31
/* #define DEBUG_TIMERS */
Jan 27, 2011
Jan 27, 2011
32
33
34
35
36
37
38
39
40
41
typedef struct _SDL_Timer
{
int timerID;
SDL_TimerCallback callback;
void *param;
Uint32 interval;
Uint32 scheduled;
volatile SDL_bool canceled;
struct _SDL_Timer *next;
} SDL_Timer;
Apr 26, 2001
Apr 26, 2001
42
Jan 27, 2011
Jan 27, 2011
43
44
45
46
47
48
typedef struct _SDL_TimerMap
{
int timerID;
SDL_Timer *timer;
struct _SDL_TimerMap *next;
} SDL_TimerMap;
Apr 26, 2001
Apr 26, 2001
49
Jan 27, 2011
Jan 27, 2011
50
51
52
53
54
55
56
/* The timers are kept in a sorted list */
typedef struct {
/* Data used by the main thread */
SDL_Thread *thread;
SDL_atomic_t nextID;
SDL_TimerMap *timermap;
SDL_mutex *timermap_lock;
Apr 26, 2001
Apr 26, 2001
57
Jan 27, 2011
Jan 27, 2011
58
/* Padding to separate cache lines between threads */
Jan 28, 2011
Jan 28, 2011
59
char cache_pad[SDL_CACHELINE_SIZE];
Apr 26, 2001
Apr 26, 2001
60
Jan 27, 2011
Jan 27, 2011
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* Data used to communicate with the timer thread */
SDL_SpinLock lock;
SDL_sem *sem;
SDL_Timer * volatile pending;
SDL_Timer * volatile freelist;
volatile SDL_bool active;
/* List of timers - this is only touched by the timer thread */
SDL_Timer *timers;
} SDL_TimerData;
static SDL_TimerData SDL_timer_data;
/* The idea here is that any thread might add a timer, but a single
* thread manages the active timer queue, sorted by scheduling time.
*
* Timers are removed by simply setting a canceled flag
*/
static void
SDL_AddTimerInternal(SDL_TimerData *data, SDL_Timer *timer)
Apr 26, 2001
Apr 26, 2001
83
{
Jan 27, 2011
Jan 27, 2011
84
SDL_Timer *prev, *curr;
Apr 26, 2001
Apr 26, 2001
85
Jan 27, 2011
Jan 27, 2011
86
87
88
89
90
91
92
93
94
95
prev = NULL;
for (curr = data->timers; curr; prev = curr, curr = curr->next) {
if ((Sint32)(timer->scheduled-curr->scheduled) < 0) {
break;
}
}
/* Insert the timer here! */
if (prev) {
prev->next = timer;
Jul 10, 2006
Jul 10, 2006
96
} else {
Jan 27, 2011
Jan 27, 2011
97
data->timers = timer;
Jul 10, 2006
Jul 10, 2006
98
}
Jan 27, 2011
Jan 27, 2011
99
timer->next = curr;
Apr 26, 2001
Apr 26, 2001
100
101
}
Jan 27, 2011
Jan 27, 2011
102
103
static int
SDL_TimerThread(void *_data)
Apr 26, 2001
Apr 26, 2001
104
{
Jan 27, 2011
Jan 27, 2011
105
106
107
108
109
110
SDL_TimerData *data = (SDL_TimerData *)_data;
SDL_Timer *pending;
SDL_Timer *current;
SDL_Timer *freelist_head = NULL;
SDL_Timer *freelist_tail = NULL;
Uint32 tick, now, interval, delay;
Apr 26, 2001
Apr 26, 2001
111
Jan 27, 2011
Jan 27, 2011
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* Threaded timer loop:
* 1. Queue timers added by other threads
* 2. Handle any timers that should dispatch this cycle
* 3. Wait until next dispatch time or new timer arrives
*/
for ( ; ; ) {
/* Pending and freelist maintenance */
SDL_AtomicLock(&data->lock);
{
/* Get any timers ready to be queued */
pending = data->pending;
data->pending = NULL;
/* Make any unused timer structures available */
if (freelist_head) {
freelist_tail->next = data->freelist;
data->freelist = freelist_head;
}
}
SDL_AtomicUnlock(&data->lock);
/* Sort the pending timers into our list */
while (pending) {
current = pending;
pending = pending->next;
SDL_AddTimerInternal(data, current);
}
freelist_head = NULL;
freelist_tail = NULL;
/* Check to see if we're still running, after maintenance */
if (!data->active) {
break;
}
/* Initial delay if there are no timers */
delay = SDL_MUTEX_MAXWAIT;
tick = SDL_GetTicks();
/* Process all the pending timers for this tick */
while (data->timers) {
current = data->timers;
if ((Sint32)(tick-current->scheduled) < 0) {
/* Scheduled for the future, wait a bit */
delay = (current->scheduled - tick);
break;
}
/* We're going to do something with this timer */
data->timers = current->next;
if (current->canceled) {
interval = 0;
} else {
interval = current->callback(current->interval, current->param);
}
if (interval > 0) {
/* Reschedule this timer */
current->scheduled = tick + interval;
SDL_AddTimerInternal(data, current);
} else {
if (!freelist_head) {
freelist_head = current;
}
if (freelist_tail) {
freelist_tail->next = current;
}
freelist_tail = current;
current->canceled = SDL_TRUE;
}
}
/* Adjust the delay based on processing time */
now = SDL_GetTicks();
interval = (now - tick);
if (interval > delay) {
delay = 0;
} else {
delay -= interval;
}
/* Note that each time a timer is added, this will return
immediately, but we process the timers added all at once.
That's okay, it just means we run through the loop a few
extra times.
*/
SDL_SemWaitTimeout(data->sem, delay);
Jul 10, 2006
Jul 10, 2006
203
}
Jan 27, 2011
Jan 27, 2011
204
return 0;
Apr 26, 2001
Apr 26, 2001
205
206
}
Jan 27, 2011
Jan 27, 2011
207
208
int
SDL_TimerInit(void)
Apr 26, 2001
Apr 26, 2001
209
{
Jan 27, 2011
Jan 27, 2011
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
SDL_TimerData *data = &SDL_timer_data;
if (!data->active) {
data->timermap_lock = SDL_CreateMutex();
if (!data->timermap_lock) {
return -1;
}
data->sem = SDL_CreateSemaphore(0);
if (!data->sem) {
SDL_DestroyMutex(data->timermap_lock);
return -1;
}
data->active = SDL_TRUE;
Jan 27, 2011
Jan 27, 2011
225
226
227
228
229
/* !!! FIXME: this is nasty. */
#if (defined(__WIN32__) && !defined(_WIN32_WCE)) && !defined(HAVE_LIBC)
#undef SDL_CreateThread
data->thread = SDL_CreateThread(SDL_TimerThread, data, NULL, NULL);
#else
Jan 27, 2011
Jan 27, 2011
230
data->thread = SDL_CreateThread(SDL_TimerThread, data);
Jan 27, 2011
Jan 27, 2011
231
#endif
Jan 27, 2011
Jan 27, 2011
232
233
234
235
236
237
if (!data->thread) {
SDL_TimerQuit();
return -1;
}
SDL_AtomicSet(&data->nextID, 1);
Jul 10, 2006
Jul 10, 2006
238
}
Jan 27, 2011
Jan 27, 2011
239
return 0;
Apr 26, 2001
Apr 26, 2001
240
241
}
Jul 10, 2006
Jul 10, 2006
242
void
Jan 27, 2011
Jan 27, 2011
243
SDL_TimerQuit(void)
Apr 26, 2001
Apr 26, 2001
244
{
Jan 27, 2011
Jan 27, 2011
245
246
247
248
249
250
251
252
253
254
255
256
SDL_TimerData *data = &SDL_timer_data;
SDL_Timer *timer;
SDL_TimerMap *entry;
if (data->active) {
data->active = SDL_FALSE;
/* Shutdown the timer thread */
if (data->thread) {
SDL_SemPost(data->sem);
SDL_WaitThread(data->thread, NULL);
data->thread = NULL;
Jul 10, 2006
Jul 10, 2006
257
}
Jan 22, 2011
Jan 22, 2011
258
Jan 27, 2011
Jan 27, 2011
259
260
SDL_DestroySemaphore(data->sem);
data->sem = NULL;
Apr 26, 2001
Apr 26, 2001
261
Jan 27, 2011
Jan 27, 2011
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* Clean up the timer entries */
while (data->timers) {
timer = data->timers;
data->timers = timer->next;
SDL_free(timer);
}
while (data->freelist) {
timer = data->freelist;
data->freelist = timer->next;
SDL_free(timer);
}
while (data->timermap) {
entry = data->timermap;
data->timermap = entry->next;
SDL_free(entry);
}
SDL_DestroyMutex(data->timermap_lock);
data->timermap_lock = NULL;
Jul 10, 2006
Jul 10, 2006
281
}
Jan 13, 2005
Jan 13, 2005
282
283
}
Jul 10, 2006
Jul 10, 2006
284
SDL_TimerID
Jan 27, 2011
Jan 27, 2011
285
SDL_AddTimer(Uint32 interval, SDL_TimerCallback callback, void *param)
Apr 26, 2001
Apr 26, 2001
286
{
Jan 27, 2011
Jan 27, 2011
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
SDL_TimerData *data = &SDL_timer_data;
SDL_Timer *timer;
SDL_TimerMap *entry;
if (!data->active) {
int status = 0;
SDL_AtomicLock(&data->lock);
if (!data->active) {
status = SDL_TimerInit();
}
SDL_AtomicUnlock(&data->lock);
if (status < 0) {
return 0;
Jul 10, 2006
Jul 10, 2006
302
303
}
}
Jan 27, 2011
Jan 27, 2011
304
305
306
307
308
SDL_AtomicLock(&data->lock);
timer = data->freelist;
if (timer) {
data->freelist = timer->next;
Jul 10, 2006
Jul 10, 2006
309
}
Jan 27, 2011
Jan 27, 2011
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
SDL_AtomicUnlock(&data->lock);
if (timer) {
SDL_RemoveTimer(timer->timerID);
} else {
timer = (SDL_Timer *)SDL_malloc(sizeof(*timer));
if (!timer) {
SDL_OutOfMemory();
return 0;
}
}
timer->timerID = SDL_AtomicIncRef(&data->nextID);
timer->callback = callback;
timer->param = param;
timer->interval = interval;
timer->scheduled = SDL_GetTicks() + interval;
timer->canceled = SDL_FALSE;
entry = (SDL_TimerMap *)SDL_malloc(sizeof(*entry));
if (!entry) {
SDL_free(timer);
SDL_OutOfMemory();
return 0;
}
entry->timer = timer;
entry->timerID = timer->timerID;
SDL_mutexP(data->timermap_lock);
entry->next = data->timermap;
data->timermap = entry;
SDL_mutexV(data->timermap_lock);
/* Add the timer to the pending list for the timer thread */
SDL_AtomicLock(&data->lock);
timer->next = data->pending;
data->pending = timer;
SDL_AtomicUnlock(&data->lock);
/* Wake up the timer thread if necessary */
SDL_SemPost(data->sem);
return entry->timerID;
Apr 26, 2001
Apr 26, 2001
352
353
}
Jul 10, 2006
Jul 10, 2006
354
355
SDL_bool
SDL_RemoveTimer(SDL_TimerID id)
Apr 26, 2001
Apr 26, 2001
356
{
Jan 27, 2011
Jan 27, 2011
357
358
359
360
361
362
363
364
365
SDL_TimerData *data = &SDL_timer_data;
SDL_TimerMap *prev, *entry;
SDL_bool canceled = SDL_FALSE;
/* Find the timer */
SDL_mutexP(data->timermap_lock);
prev = NULL;
for (entry = data->timermap; entry; prev = entry, entry = entry->next) {
if (entry->timerID == id) {
Jul 10, 2006
Jul 10, 2006
366
if (prev) {
Jan 27, 2011
Jan 27, 2011
367
prev->next = entry->next;
Jul 10, 2006
Jul 10, 2006
368
} else {
Jan 27, 2011
Jan 27, 2011
369
data->timermap = entry->next;
Jul 10, 2006
Jul 10, 2006
370
371
372
373
}
break;
}
}
Jan 27, 2011
Jan 27, 2011
374
SDL_mutexV(data->timermap_lock);
Apr 26, 2001
Apr 26, 2001
375
Jan 27, 2011
Jan 27, 2011
376
377
378
379
if (entry) {
if (!entry->timer->canceled) {
entry->timer->canceled = SDL_TRUE;
canceled = SDL_TRUE;
Jul 10, 2006
Jul 10, 2006
380
}
Jan 27, 2011
Jan 27, 2011
381
SDL_free(entry);
Jul 10, 2006
Jul 10, 2006
382
}
Jan 27, 2011
Jan 27, 2011
383
return canceled;
Apr 26, 2001
Apr 26, 2001
384
}
Jul 10, 2006
Jul 10, 2006
385
386
/* vi: set ts=4 sw=4 expandtab: */