Hash :
d950b9e2
Author :
Date :
2021-11-07T20:40:54
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
/*
Simple DirectMedia Layer
Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../SDL_internal.h"
#include "SDL_timer.h"
#include "SDL_timer_c.h"
#include "SDL_atomic.h"
#include "SDL_cpuinfo.h"
#include "../thread/SDL_systhread.h"
/* #define DEBUG_TIMERS */
#if !defined(__EMSCRIPTEN__) || !SDL_THREADS_DISABLED
typedef struct _SDL_Timer
{
int timerID;
SDL_TimerCallback callback;
void *param;
Uint32 interval;
Uint32 scheduled;
SDL_atomic_t canceled;
struct _SDL_Timer *next;
} SDL_Timer;
typedef struct _SDL_TimerMap
{
int timerID;
SDL_Timer *timer;
struct _SDL_TimerMap *next;
} SDL_TimerMap;
/* 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;
/* Padding to separate cache lines between threads */
char cache_pad[SDL_CACHELINE_SIZE];
/* Data used to communicate with the timer thread */
SDL_SpinLock lock;
SDL_sem *sem;
SDL_Timer *pending;
SDL_Timer *freelist;
SDL_atomic_t 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)
{
SDL_Timer *prev, *curr;
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;
} else {
data->timers = timer;
}
timer->next = curr;
}
static int SDLCALL
SDL_TimerThread(void *_data)
{
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;
/* 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 (!SDL_AtomicGet(&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 (SDL_AtomicGet(¤t->canceled)) {
interval = 0;
} else {
interval = current->callback(current->interval, current->param);
}
if (interval > 0) {
/* Reschedule this timer */
current->interval = interval;
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;
SDL_AtomicSet(¤t->canceled, 1);
}
}
/* 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);
}
return 0;
}
int
SDL_TimerInit(void)
{
SDL_TimerData *data = &SDL_timer_data;
if (!SDL_AtomicGet(&data->active)) {
const char *name = "SDLTimer";
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;
}
SDL_AtomicSet(&data->active, 1);
/* Timer threads use a callback into the app, so we can't set a limited stack size here. */
data->thread = SDL_CreateThreadInternal(SDL_TimerThread, name, 0, data);
if (!data->thread) {
SDL_TimerQuit();
return -1;
}
SDL_AtomicSet(&data->nextID, 1);
}
return 0;
}
void
SDL_TimerQuit(void)
{
SDL_TimerData *data = &SDL_timer_data;
SDL_Timer *timer;
SDL_TimerMap *entry;
if (SDL_AtomicCAS(&data->active, 1, 0)) { /* active? Move to inactive. */
/* Shutdown the timer thread */
if (data->thread) {
SDL_SemPost(data->sem);
SDL_WaitThread(data->thread, NULL);
data->thread = NULL;
}
SDL_DestroySemaphore(data->sem);
data->sem = NULL;
/* 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;
}
}
SDL_TimerID
SDL_AddTimer(Uint32 interval, SDL_TimerCallback callback, void *param)
{
SDL_TimerData *data = &SDL_timer_data;
SDL_Timer *timer;
SDL_TimerMap *entry;
SDL_AtomicLock(&data->lock);
if (!SDL_AtomicGet(&data->active)) {
if (SDL_TimerInit() < 0) {
SDL_AtomicUnlock(&data->lock);
return 0;
}
}
timer = data->freelist;
if (timer) {
data->freelist = timer->next;
}
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;
SDL_AtomicSet(&timer->canceled, 0);
entry = (SDL_TimerMap *)SDL_malloc(sizeof(*entry));
if (!entry) {
SDL_free(timer);
SDL_OutOfMemory();
return 0;
}
entry->timer = timer;
entry->timerID = timer->timerID;
SDL_LockMutex(data->timermap_lock);
entry->next = data->timermap;
data->timermap = entry;
SDL_UnlockMutex(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;
}
SDL_bool
SDL_RemoveTimer(SDL_TimerID id)
{
SDL_TimerData *data = &SDL_timer_data;
SDL_TimerMap *prev, *entry;
SDL_bool canceled = SDL_FALSE;
/* Find the timer */
SDL_LockMutex(data->timermap_lock);
prev = NULL;
for (entry = data->timermap; entry; prev = entry, entry = entry->next) {
if (entry->timerID == id) {
if (prev) {
prev->next = entry->next;
} else {
data->timermap = entry->next;
}
break;
}
}
SDL_UnlockMutex(data->timermap_lock);
if (entry) {
if (!SDL_AtomicGet(&entry->timer->canceled)) {
SDL_AtomicSet(&entry->timer->canceled, 1);
canceled = SDL_TRUE;
}
SDL_free(entry);
}
return canceled;
}
#else
#include <emscripten/emscripten.h>
typedef struct _SDL_TimerMap
{
int timerID;
int timeoutID;
struct _SDL_TimerMap *next;
} SDL_TimerMap;
typedef struct {
int nextID;
SDL_TimerMap *timermap;
} SDL_TimerData;
static SDL_TimerData SDL_timer_data;
static void
SDL_Emscripten_TimerHelper(SDL_TimerMap *entry, Uint32 interval, SDL_TimerCallback callback, void *param)
{
Uint32 new_timeout;
new_timeout = callback(interval, param);
if (new_timeout != 0) {
entry->timeoutID = EM_ASM_INT({
return Browser.safeSetTimeout(function() {
dynCall('viiii', $0, [$1, $2, $3, $4]);
}, $2);
}, &SDL_Emscripten_TimerHelper, entry, interval, callback, param);
}
}
int
SDL_TimerInit(void)
{
return 0;
}
void
SDL_TimerQuit(void)
{
SDL_TimerData *data = &SDL_timer_data;
SDL_TimerMap *entry;
while (data->timermap) {
entry = data->timermap;
data->timermap = entry->next;
SDL_free(entry);
}
}
SDL_TimerID
SDL_AddTimer(Uint32 interval, SDL_TimerCallback callback, void *param)
{
SDL_TimerData *data = &SDL_timer_data;
SDL_TimerMap *entry;
entry = (SDL_TimerMap *)SDL_malloc(sizeof(*entry));
if (!entry) {
SDL_OutOfMemory();
return 0;
}
entry->timerID = ++data->nextID;
entry->timeoutID = EM_ASM_INT({
return Browser.safeSetTimeout(function() {
dynCall('viiii', $0, [$1, $2, $3, $4]);
}, $2);
}, &SDL_Emscripten_TimerHelper, entry, interval, callback, param);
entry->next = data->timermap;
data->timermap = entry;
return entry->timerID;
}
SDL_bool
SDL_RemoveTimer(SDL_TimerID id)
{
SDL_TimerData *data = &SDL_timer_data;
SDL_TimerMap *prev, *entry;
/* Find the timer */
prev = NULL;
for (entry = data->timermap; entry; prev = entry, entry = entry->next) {
if (entry->timerID == id) {
if (prev) {
prev->next = entry->next;
} else {
data->timermap = entry->next;
}
break;
}
}
if (entry) {
EM_ASM_({
window.clearTimeout($0);
}, entry->timeoutID);
SDL_free(entry);
return SDL_TRUE;
}
return SDL_FALSE;
}
#endif
/* This is a legacy support function; SDL_GetTicks() returns a Uint32,
which wraps back to zero every ~49 days. The newer SDL_GetTicks64()
doesn't have this problem, so we just wrap that function and clamp to
the low 32-bits for binary compatibility. */
Uint32
SDL_GetTicks(void)
{
return (Uint32) (SDL_GetTicks64() & 0xFFFFFFFF);
}
/* vi: set ts=4 sw=4 expandtab: */