SDL_timer.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../SDL_internal.h"
  19. #include "SDL_timer.h"
  20. #include "SDL_timer_c.h"
  21. #include "SDL_atomic.h"
  22. #include "SDL_cpuinfo.h"
  23. #include "SDL_thread.h"
  24. /* #define DEBUG_TIMERS */
  25. typedef struct _SDL_Timer
  26. {
  27. int timerID;
  28. SDL_TimerCallback callback;
  29. void *param;
  30. Uint32 interval;
  31. Uint32 scheduled;
  32. volatile SDL_bool canceled;
  33. struct _SDL_Timer *next;
  34. } SDL_Timer;
  35. typedef struct _SDL_TimerMap
  36. {
  37. int timerID;
  38. SDL_Timer *timer;
  39. struct _SDL_TimerMap *next;
  40. } SDL_TimerMap;
  41. /* The timers are kept in a sorted list */
  42. typedef struct {
  43. /* Data used by the main thread */
  44. SDL_Thread *thread;
  45. SDL_atomic_t nextID;
  46. SDL_TimerMap *timermap;
  47. SDL_mutex *timermap_lock;
  48. /* Padding to separate cache lines between threads */
  49. char cache_pad[SDL_CACHELINE_SIZE];
  50. /* Data used to communicate with the timer thread */
  51. SDL_SpinLock lock;
  52. SDL_sem *sem;
  53. SDL_Timer * volatile pending;
  54. SDL_Timer * volatile freelist;
  55. volatile SDL_bool active;
  56. /* List of timers - this is only touched by the timer thread */
  57. SDL_Timer *timers;
  58. } SDL_TimerData;
  59. static SDL_TimerData SDL_timer_data;
  60. /* The idea here is that any thread might add a timer, but a single
  61. * thread manages the active timer queue, sorted by scheduling time.
  62. *
  63. * Timers are removed by simply setting a canceled flag
  64. */
  65. static void
  66. SDL_AddTimerInternal(SDL_TimerData *data, SDL_Timer *timer)
  67. {
  68. SDL_Timer *prev, *curr;
  69. prev = NULL;
  70. for (curr = data->timers; curr; prev = curr, curr = curr->next) {
  71. if ((Sint32)(timer->scheduled-curr->scheduled) < 0) {
  72. break;
  73. }
  74. }
  75. /* Insert the timer here! */
  76. if (prev) {
  77. prev->next = timer;
  78. } else {
  79. data->timers = timer;
  80. }
  81. timer->next = curr;
  82. }
  83. static int
  84. SDL_TimerThread(void *_data)
  85. {
  86. SDL_TimerData *data = (SDL_TimerData *)_data;
  87. SDL_Timer *pending;
  88. SDL_Timer *current;
  89. SDL_Timer *freelist_head = NULL;
  90. SDL_Timer *freelist_tail = NULL;
  91. Uint32 tick, now, interval, delay;
  92. /* Threaded timer loop:
  93. * 1. Queue timers added by other threads
  94. * 2. Handle any timers that should dispatch this cycle
  95. * 3. Wait until next dispatch time or new timer arrives
  96. */
  97. for ( ; ; ) {
  98. /* Pending and freelist maintenance */
  99. SDL_AtomicLock(&data->lock);
  100. {
  101. /* Get any timers ready to be queued */
  102. pending = data->pending;
  103. data->pending = NULL;
  104. /* Make any unused timer structures available */
  105. if (freelist_head) {
  106. freelist_tail->next = data->freelist;
  107. data->freelist = freelist_head;
  108. }
  109. }
  110. SDL_AtomicUnlock(&data->lock);
  111. /* Sort the pending timers into our list */
  112. while (pending) {
  113. current = pending;
  114. pending = pending->next;
  115. SDL_AddTimerInternal(data, current);
  116. }
  117. freelist_head = NULL;
  118. freelist_tail = NULL;
  119. /* Check to see if we're still running, after maintenance */
  120. if (!data->active) {
  121. break;
  122. }
  123. /* Initial delay if there are no timers */
  124. delay = SDL_MUTEX_MAXWAIT;
  125. tick = SDL_GetTicks();
  126. /* Process all the pending timers for this tick */
  127. while (data->timers) {
  128. current = data->timers;
  129. if ((Sint32)(tick-current->scheduled) < 0) {
  130. /* Scheduled for the future, wait a bit */
  131. delay = (current->scheduled - tick);
  132. break;
  133. }
  134. /* We're going to do something with this timer */
  135. data->timers = current->next;
  136. if (current->canceled) {
  137. interval = 0;
  138. } else {
  139. interval = current->callback(current->interval, current->param);
  140. }
  141. if (interval > 0) {
  142. /* Reschedule this timer */
  143. current->scheduled = tick + interval;
  144. SDL_AddTimerInternal(data, current);
  145. } else {
  146. if (!freelist_head) {
  147. freelist_head = current;
  148. }
  149. if (freelist_tail) {
  150. freelist_tail->next = current;
  151. }
  152. freelist_tail = current;
  153. current->canceled = SDL_TRUE;
  154. }
  155. }
  156. /* Adjust the delay based on processing time */
  157. now = SDL_GetTicks();
  158. interval = (now - tick);
  159. if (interval > delay) {
  160. delay = 0;
  161. } else {
  162. delay -= interval;
  163. }
  164. /* Note that each time a timer is added, this will return
  165. immediately, but we process the timers added all at once.
  166. That's okay, it just means we run through the loop a few
  167. extra times.
  168. */
  169. SDL_SemWaitTimeout(data->sem, delay);
  170. }
  171. return 0;
  172. }
  173. int
  174. SDL_TimerInit(void)
  175. {
  176. SDL_TimerData *data = &SDL_timer_data;
  177. if (!data->active) {
  178. const char *name = "SDLTimer";
  179. data->timermap_lock = SDL_CreateMutex();
  180. if (!data->timermap_lock) {
  181. return -1;
  182. }
  183. data->sem = SDL_CreateSemaphore(0);
  184. if (!data->sem) {
  185. SDL_DestroyMutex(data->timermap_lock);
  186. return -1;
  187. }
  188. data->active = SDL_TRUE;
  189. /* !!! FIXME: this is nasty. */
  190. #if defined(__WIN32__) && !defined(HAVE_LIBC)
  191. #undef SDL_CreateThread
  192. #if SDL_DYNAMIC_API
  193. data->thread = SDL_CreateThread_REAL(SDL_TimerThread, name, data, NULL, NULL);
  194. #else
  195. data->thread = SDL_CreateThread(SDL_TimerThread, name, data, NULL, NULL);
  196. #endif
  197. #else
  198. data->thread = SDL_CreateThread(SDL_TimerThread, name, data);
  199. #endif
  200. if (!data->thread) {
  201. SDL_TimerQuit();
  202. return -1;
  203. }
  204. SDL_AtomicSet(&data->nextID, 1);
  205. }
  206. return 0;
  207. }
  208. void
  209. SDL_TimerQuit(void)
  210. {
  211. SDL_TimerData *data = &SDL_timer_data;
  212. SDL_Timer *timer;
  213. SDL_TimerMap *entry;
  214. if (data->active) {
  215. data->active = SDL_FALSE;
  216. /* Shutdown the timer thread */
  217. if (data->thread) {
  218. SDL_SemPost(data->sem);
  219. SDL_WaitThread(data->thread, NULL);
  220. data->thread = NULL;
  221. }
  222. SDL_DestroySemaphore(data->sem);
  223. data->sem = NULL;
  224. /* Clean up the timer entries */
  225. while (data->timers) {
  226. timer = data->timers;
  227. data->timers = timer->next;
  228. SDL_free(timer);
  229. }
  230. while (data->freelist) {
  231. timer = data->freelist;
  232. data->freelist = timer->next;
  233. SDL_free(timer);
  234. }
  235. while (data->timermap) {
  236. entry = data->timermap;
  237. data->timermap = entry->next;
  238. SDL_free(entry);
  239. }
  240. SDL_DestroyMutex(data->timermap_lock);
  241. data->timermap_lock = NULL;
  242. }
  243. }
  244. SDL_TimerID
  245. SDL_AddTimer(Uint32 interval, SDL_TimerCallback callback, void *param)
  246. {
  247. SDL_TimerData *data = &SDL_timer_data;
  248. SDL_Timer *timer;
  249. SDL_TimerMap *entry;
  250. if (!data->active) {
  251. int status = 0;
  252. SDL_AtomicLock(&data->lock);
  253. if (!data->active) {
  254. status = SDL_TimerInit();
  255. }
  256. SDL_AtomicUnlock(&data->lock);
  257. if (status < 0) {
  258. return 0;
  259. }
  260. }
  261. SDL_AtomicLock(&data->lock);
  262. timer = data->freelist;
  263. if (timer) {
  264. data->freelist = timer->next;
  265. }
  266. SDL_AtomicUnlock(&data->lock);
  267. if (timer) {
  268. SDL_RemoveTimer(timer->timerID);
  269. } else {
  270. timer = (SDL_Timer *)SDL_malloc(sizeof(*timer));
  271. if (!timer) {
  272. SDL_OutOfMemory();
  273. return 0;
  274. }
  275. }
  276. timer->timerID = SDL_AtomicIncRef(&data->nextID);
  277. timer->callback = callback;
  278. timer->param = param;
  279. timer->interval = interval;
  280. timer->scheduled = SDL_GetTicks() + interval;
  281. timer->canceled = SDL_FALSE;
  282. entry = (SDL_TimerMap *)SDL_malloc(sizeof(*entry));
  283. if (!entry) {
  284. SDL_free(timer);
  285. SDL_OutOfMemory();
  286. return 0;
  287. }
  288. entry->timer = timer;
  289. entry->timerID = timer->timerID;
  290. SDL_LockMutex(data->timermap_lock);
  291. entry->next = data->timermap;
  292. data->timermap = entry;
  293. SDL_UnlockMutex(data->timermap_lock);
  294. /* Add the timer to the pending list for the timer thread */
  295. SDL_AtomicLock(&data->lock);
  296. timer->next = data->pending;
  297. data->pending = timer;
  298. SDL_AtomicUnlock(&data->lock);
  299. /* Wake up the timer thread if necessary */
  300. SDL_SemPost(data->sem);
  301. return entry->timerID;
  302. }
  303. SDL_bool
  304. SDL_RemoveTimer(SDL_TimerID id)
  305. {
  306. SDL_TimerData *data = &SDL_timer_data;
  307. SDL_TimerMap *prev, *entry;
  308. SDL_bool canceled = SDL_FALSE;
  309. /* Find the timer */
  310. SDL_LockMutex(data->timermap_lock);
  311. prev = NULL;
  312. for (entry = data->timermap; entry; prev = entry, entry = entry->next) {
  313. if (entry->timerID == id) {
  314. if (prev) {
  315. prev->next = entry->next;
  316. } else {
  317. data->timermap = entry->next;
  318. }
  319. break;
  320. }
  321. }
  322. SDL_UnlockMutex(data->timermap_lock);
  323. if (entry) {
  324. if (!entry->timer->canceled) {
  325. entry->timer->canceled = SDL_TRUE;
  326. canceled = SDL_TRUE;
  327. }
  328. SDL_free(entry);
  329. }
  330. return canceled;
  331. }
  332. /* vi: set ts=4 sw=4 expandtab: */