comparison src/timer/dummy/SDL_systimer.c @ 5113:481dabb098ef

Improved timer implementation The new timer model is formalized as using a separate thread to handle timer callbacks. This was the case on almost every platform before, but it's now a requirement, and simplifies the implementation and makes it perform consistently across platforms. Goals: * Minimize timer thread blocking * Dispatch timers as accurately as possible * SDL_AddTimer() and SDL_RemoveTimer() are completely threadsafe * SDL_RemoveTimer() doesn't crash with a timer that's expired or removed
author Sam Lantinga <slouken@libsdl.org>
date Thu, 27 Jan 2011 14:45:06 -0800
parents f7b03b6838cb
children b530ef003506
comparison
equal deleted inserted replaced
5112:0846f18eb625 5113:481dabb098ef
22 #include "SDL_config.h" 22 #include "SDL_config.h"
23 23
24 #if defined(SDL_TIMER_DUMMY) || defined(SDL_TIMERS_DISABLED) 24 #if defined(SDL_TIMER_DUMMY) || defined(SDL_TIMERS_DISABLED)
25 25
26 #include "SDL_timer.h" 26 #include "SDL_timer.h"
27 #include "../SDL_timer_c.h"
28 27
29 void 28 void
30 SDL_StartTicks(void) 29 SDL_StartTicks(void)
31 { 30 {
32 } 31 }
42 SDL_Delay(Uint32 ms) 41 SDL_Delay(Uint32 ms)
43 { 42 {
44 SDL_Unsupported(); 43 SDL_Unsupported();
45 } 44 }
46 45
47 #include "SDL_thread.h" 46 #endif /* SDL_TIMER_DUMMY || SDL_TIMERS_DISABLED */
48 47
49 /* Data to handle a single periodic alarm */
50 static int timer_alive = 0;
51 static SDL_Thread *timer = NULL;
52
53 static int
54 RunTimer(void *unused)
55 {
56 while (timer_alive) {
57 if (SDL_timer_running) {
58 SDL_ThreadedTimerCheck();
59 }
60 SDL_Delay(1);
61 }
62 return (0);
63 }
64
65 /* This is only called if the event thread is not running */
66 int
67 SDL_SYS_TimerInit(void)
68 {
69 timer_alive = 1;
70 timer = SDL_CreateThread(RunTimer, NULL);
71 if (timer == NULL)
72 return (-1);
73 return (SDL_SetTimerThreaded(1));
74 }
75
76 void
77 SDL_SYS_TimerQuit(void)
78 {
79 timer_alive = 0;
80 if (timer) {
81 SDL_WaitThread(timer, NULL);
82 timer = NULL;
83 }
84 }
85
86 int
87 SDL_SYS_StartTimer(void)
88 {
89 SDL_SetError("Internal logic error: threaded timer in use");
90 return (-1);
91 }
92
93 void
94 SDL_SYS_StopTimer(void)
95 {
96 return;
97 }
98
99 #endif /* SDL_TIMER_DUMMY || SDL_TIMERS_DISABLED */
100 /* vi: set ts=4 sw=4 expandtab: */ 48 /* vi: set ts=4 sw=4 expandtab: */