| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 3 |
|
| 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 |
|
| 8 |
Permission is granted to anyone to use this software for any purpose, |
| 9 |
including commercial applications, and to alter it and redistribute it |
| 10 |
freely. |
| 11 |
*/ |
| 12 |
#include <stdio.h> |
| 13 |
|
| 14 |
#include "SDL.h" |
| 15 |
#include "SDL_test.h" |
| 16 |
|
| 17 |
/* |
| 18 |
Absolutely basic tests just to see if we get the expected value |
| 19 |
after calling each function. |
| 20 |
*/ |
| 21 |
|
| 22 |
static const char * |
| 23 |
tf(SDL_bool _tf) |
| 24 |
{ |
| 25 |
static const char *t = "TRUE"; |
| 26 |
static const char *f = "FALSE"; |
| 27 |
|
| 28 |
if (_tf) { |
| 29 |
return t; |
| 30 |
} |
| 31 |
|
| 32 |
return f; |
| 33 |
} |
| 34 |
|
| 35 |
static void RunBasicTest(void) |
| 36 |
{ |
| 37 |
int value; |
| 38 |
SDL_SpinLock lock = 0; |
| 39 |
|
| 40 |
SDL_atomic_t v; |
| 41 |
SDL_bool tfret = SDL_FALSE; |
| 42 |
|
| 43 |
SDL_Log("\nspin lock---------------------------------------\n\n"); |
| 44 |
|
| 45 |
SDL_AtomicLock(&lock); |
| 46 |
SDL_Log("AtomicLock lock=%d\n", lock); |
| 47 |
SDL_AtomicUnlock(&lock); |
| 48 |
SDL_Log("AtomicUnlock lock=%d\n", lock); |
| 49 |
|
| 50 |
SDL_Log("\natomic -----------------------------------------\n\n"); |
| 51 |
|
| 52 |
SDL_AtomicSet(&v, 0); |
| 53 |
tfret = SDL_AtomicSet(&v, 10) == 0 ? SDL_TRUE : SDL_FALSE; |
| 54 |
SDL_Log("AtomicSet(10) tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v)); |
| 55 |
tfret = SDL_AtomicAdd(&v, 10) == 10 ? SDL_TRUE : SDL_FALSE; |
| 56 |
SDL_Log("AtomicAdd(10) tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v)); |
| 57 |
|
| 58 |
SDL_AtomicSet(&v, 0); |
| 59 |
SDL_AtomicIncRef(&v); |
| 60 |
tfret = (SDL_AtomicGet(&v) == 1) ? SDL_TRUE : SDL_FALSE; |
| 61 |
SDL_Log("AtomicIncRef() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v)); |
| 62 |
SDL_AtomicIncRef(&v); |
| 63 |
tfret = (SDL_AtomicGet(&v) == 2) ? SDL_TRUE : SDL_FALSE; |
| 64 |
SDL_Log("AtomicIncRef() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v)); |
| 65 |
tfret = (SDL_AtomicDecRef(&v) == SDL_FALSE) ? SDL_TRUE : SDL_FALSE; |
| 66 |
SDL_Log("AtomicDecRef() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v)); |
| 67 |
tfret = (SDL_AtomicDecRef(&v) == SDL_TRUE) ? SDL_TRUE : SDL_FALSE; |
| 68 |
SDL_Log("AtomicDecRef() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v)); |
| 69 |
|
| 70 |
SDL_AtomicSet(&v, 10); |
| 71 |
tfret = (SDL_AtomicCAS(&v, 0, 20) == SDL_FALSE) ? SDL_TRUE : SDL_FALSE; |
| 72 |
SDL_Log("AtomicCAS() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v)); |
| 73 |
value = SDL_AtomicGet(&v); |
| 74 |
tfret = (SDL_AtomicCAS(&v, value, 20) == SDL_TRUE) ? SDL_TRUE : SDL_FALSE; |
| 75 |
SDL_Log("AtomicCAS() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v)); |
| 76 |
} |
| 77 |
|
| 78 |
/**************************************************************************/ |
| 79 |
/* Atomic operation test |
| 80 |
* Adapted with permission from code by Michael Davidsaver at: |
| 81 |
* http://bazaar.launchpad.net/~mdavidsaver/epics-base/atomic/revision/12105#src/libCom/test/epicsAtomicTest.c |
| 82 |
* Original copyright 2010 Brookhaven Science Associates as operator of Brookhaven National Lab |
| 83 |
* http://www.aps.anl.gov/epics/license/open.php |
| 84 |
*/ |
| 85 |
|
| 86 |
/* Tests semantics of atomic operations. Also a stress test |
| 87 |
* to see if they are really atomic. |
| 88 |
* |
| 89 |
* Several threads adding to the same variable. |
| 90 |
* at the end the value is compared with the expected |
| 91 |
* and with a non-atomic counter. |
| 92 |
*/ |
| 93 |
|
| 94 |
/* Number of concurrent incrementers */ |
| 95 |
#define NThreads 2 |
| 96 |
#define CountInc 100 |
| 97 |
#define VALBITS (sizeof(atomicValue) * 8) |
| 98 |
|
| 99 |
#define atomicValue int |
| 100 |
#define CountTo ((atomicValue)((unsigned int)(1 << (VALBITS - 1)) - 1)) |
| 101 |
#define NInter (CountTo / CountInc / NThreads) |
| 102 |
#define Expect (CountTo - NInter * CountInc * NThreads) |
| 103 |
|
| 104 |
enum |
| 105 |
{ |
| 106 |
CountTo_GreaterThanZero = CountTo > 0, |
| 107 |
}; |
| 108 |
SDL_COMPILE_TIME_ASSERT(size, CountTo_GreaterThanZero); /* check for rollover */ |
| 109 |
|
| 110 |
static SDL_atomic_t good = { 42 }; |
| 111 |
|
| 112 |
static atomicValue bad = 42; |
| 113 |
|
| 114 |
static SDL_atomic_t threadsRunning; |
| 115 |
|
| 116 |
static SDL_sem *threadDone; |
| 117 |
|
| 118 |
static int SDLCALL adder(void *junk) |
| 119 |
{ |
| 120 |
unsigned long N = NInter; |
| 121 |
SDL_Log("Thread subtracting %d %lu times\n", CountInc, N); |
| 122 |
while (N--) { |
| 123 |
SDL_AtomicAdd(&good, -CountInc); |
| 124 |
bad -= CountInc; |
| 125 |
} |
| 126 |
SDL_AtomicAdd(&threadsRunning, -1); |
| 127 |
SDL_SemPost(threadDone); |
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
static void runAdder(void) |
| 132 |
{ |
| 133 |
Uint32 start, end; |
| 134 |
int i; |
| 135 |
SDL_Thread *threads[NThreads]; |
| 136 |
|
| 137 |
start = SDL_GetTicks(); |
| 138 |
|
| 139 |
threadDone = SDL_CreateSemaphore(0); |
| 140 |
|
| 141 |
SDL_AtomicSet(&threadsRunning, NThreads); |
| 142 |
|
| 143 |
for (i = 0; i < NThreads; i++) { |
| 144 |
threads[i] = SDL_CreateThread(adder, "Adder", NULL); |
| 145 |
} |
| 146 |
|
| 147 |
while (SDL_AtomicGet(&threadsRunning) > 0) { |
| 148 |
SDL_SemWait(threadDone); |
| 149 |
} |
| 150 |
|
| 151 |
for (i = 0; i < NThreads; i++) { |
| 152 |
SDL_WaitThread(threads[i], NULL); |
| 153 |
} |
| 154 |
|
| 155 |
SDL_DestroySemaphore(threadDone); |
| 156 |
|
| 157 |
end = SDL_GetTicks(); |
| 158 |
|
| 159 |
SDL_Log("Finished in %f sec\n", (end - start) / 1000.f); |
| 160 |
} |
| 161 |
|
| 162 |
static void RunEpicTest(void) |
| 163 |
{ |
| 164 |
int b; |
| 165 |
atomicValue v; |
| 166 |
|
| 167 |
SDL_Log("\nepic test---------------------------------------\n\n"); |
| 168 |
|
| 169 |
SDL_Log("Size asserted to be >= 32-bit\n"); |
| 170 |
SDL_assert(sizeof(atomicValue) >= 4); |
| 171 |
|
| 172 |
SDL_Log("Check static initializer\n"); |
| 173 |
v = SDL_AtomicGet(&good); |
| 174 |
SDL_assert(v == 42); |
| 175 |
|
| 176 |
SDL_assert(bad == 42); |
| 177 |
|
| 178 |
SDL_Log("Test negative values\n"); |
| 179 |
SDL_AtomicSet(&good, -5); |
| 180 |
v = SDL_AtomicGet(&good); |
| 181 |
SDL_assert(v == -5); |
| 182 |
|
| 183 |
SDL_Log("Verify maximum value\n"); |
| 184 |
SDL_AtomicSet(&good, CountTo); |
| 185 |
v = SDL_AtomicGet(&good); |
| 186 |
SDL_assert(v == CountTo); |
| 187 |
|
| 188 |
SDL_Log("Test compare and exchange\n"); |
| 189 |
|
| 190 |
b = SDL_AtomicCAS(&good, 500, 43); |
| 191 |
SDL_assert(!b); /* no swap since CountTo!=500 */ |
| 192 |
v = SDL_AtomicGet(&good); |
| 193 |
SDL_assert(v == CountTo); /* ensure no swap */ |
| 194 |
|
| 195 |
b = SDL_AtomicCAS(&good, CountTo, 44); |
| 196 |
SDL_assert(!!b); /* will swap */ |
| 197 |
v = SDL_AtomicGet(&good); |
| 198 |
SDL_assert(v == 44); |
| 199 |
|
| 200 |
SDL_Log("Test Add\n"); |
| 201 |
|
| 202 |
v = SDL_AtomicAdd(&good, 1); |
| 203 |
SDL_assert(v == 44); |
| 204 |
v = SDL_AtomicGet(&good); |
| 205 |
SDL_assert(v == 45); |
| 206 |
|
| 207 |
v = SDL_AtomicAdd(&good, 10); |
| 208 |
SDL_assert(v == 45); |
| 209 |
v = SDL_AtomicGet(&good); |
| 210 |
SDL_assert(v == 55); |
| 211 |
|
| 212 |
SDL_Log("Test Add (Negative values)\n"); |
| 213 |
|
| 214 |
v = SDL_AtomicAdd(&good, -20); |
| 215 |
SDL_assert(v == 55); |
| 216 |
v = SDL_AtomicGet(&good); |
| 217 |
SDL_assert(v == 35); |
| 218 |
|
| 219 |
v = SDL_AtomicAdd(&good, -50); /* crossing zero down */ |
| 220 |
SDL_assert(v == 35); |
| 221 |
v = SDL_AtomicGet(&good); |
| 222 |
SDL_assert(v == -15); |
| 223 |
|
| 224 |
v = SDL_AtomicAdd(&good, 30); /* crossing zero up */ |
| 225 |
SDL_assert(v == -15); |
| 226 |
v = SDL_AtomicGet(&good); |
| 227 |
SDL_assert(v == 15); |
| 228 |
|
| 229 |
SDL_Log("Reset before count down test\n"); |
| 230 |
SDL_AtomicSet(&good, CountTo); |
| 231 |
v = SDL_AtomicGet(&good); |
| 232 |
SDL_assert(v == CountTo); |
| 233 |
|
| 234 |
bad = CountTo; |
| 235 |
SDL_assert(bad == CountTo); |
| 236 |
|
| 237 |
SDL_Log("Counting down from %d, Expect %d remaining\n", CountTo, Expect); |
| 238 |
runAdder(); |
| 239 |
|
| 240 |
v = SDL_AtomicGet(&good); |
| 241 |
SDL_Log("Atomic %d Non-Atomic %d\n", v, bad); |
| 242 |
SDL_assert(v == Expect); |
| 243 |
/* We can't guarantee that bad != Expect, this would happen on a single core system, for example. */ |
| 244 |
/*SDL_assert(bad != Expect);*/ |
| 245 |
} |
| 246 |
|
| 247 |
/* End atomic operation test */ |
| 248 |
/**************************************************************************/ |
| 249 |
|
| 250 |
/**************************************************************************/ |
| 251 |
/* Lock-free FIFO test */ |
| 252 |
|
| 253 |
/* This is useful to test the impact of another thread locking the queue |
| 254 |
entirely for heavy-weight manipulation. |
| 255 |
*/ |
| 256 |
#define TEST_SPINLOCK_FIFO |
| 257 |
|
| 258 |
#define NUM_READERS 4 |
| 259 |
#define NUM_WRITERS 4 |
| 260 |
#define EVENTS_PER_WRITER 1000000 |
| 261 |
|
| 262 |
/* The number of entries must be a power of 2 */ |
| 263 |
#define MAX_ENTRIES 256 |
| 264 |
#define WRAP_MASK (MAX_ENTRIES - 1) |
| 265 |
|
| 266 |
typedef struct |
| 267 |
{ |
| 268 |
SDL_atomic_t sequence; |
| 269 |
SDL_Event event; |
| 270 |
} SDL_EventQueueEntry; |
| 271 |
|
| 272 |
typedef struct |
| 273 |
{ |
| 274 |
SDL_EventQueueEntry entries[MAX_ENTRIES]; |
| 275 |
|
| 276 |
char cache_pad1[SDL_CACHELINE_SIZE - ((sizeof(SDL_EventQueueEntry) * MAX_ENTRIES) % SDL_CACHELINE_SIZE)]; |
| 277 |
|
| 278 |
SDL_atomic_t enqueue_pos; |
| 279 |
|
| 280 |
char cache_pad2[SDL_CACHELINE_SIZE - sizeof(SDL_atomic_t)]; |
| 281 |
|
| 282 |
SDL_atomic_t dequeue_pos; |
| 283 |
|
| 284 |
char cache_pad3[SDL_CACHELINE_SIZE - sizeof(SDL_atomic_t)]; |
| 285 |
|
| 286 |
#ifdef TEST_SPINLOCK_FIFO |
| 287 |
SDL_SpinLock lock; |
| 288 |
SDL_atomic_t rwcount; |
| 289 |
SDL_atomic_t watcher; |
| 290 |
|
| 291 |
char cache_pad4[SDL_CACHELINE_SIZE - sizeof(SDL_SpinLock) - 2 * sizeof(SDL_atomic_t)]; |
| 292 |
#endif |
| 293 |
|
| 294 |
SDL_atomic_t active; |
| 295 |
|
| 296 |
/* Only needed for the mutex test */ |
| 297 |
SDL_mutex *mutex; |
| 298 |
|
| 299 |
} SDL_EventQueue; |
| 300 |
|
| 301 |
static void InitEventQueue(SDL_EventQueue *queue) |
| 302 |
{ |
| 303 |
int i; |
| 304 |
|
| 305 |
for (i = 0; i < MAX_ENTRIES; ++i) { |
| 306 |
SDL_AtomicSet(&queue->entries[i].sequence, i); |
| 307 |
} |
| 308 |
SDL_AtomicSet(&queue->enqueue_pos, 0); |
| 309 |
SDL_AtomicSet(&queue->dequeue_pos, 0); |
| 310 |
#ifdef TEST_SPINLOCK_FIFO |
| 311 |
queue->lock = 0; |
| 312 |
SDL_AtomicSet(&queue->rwcount, 0); |
| 313 |
SDL_AtomicSet(&queue->watcher, 0); |
| 314 |
#endif |
| 315 |
SDL_AtomicSet(&queue->active, 1); |
| 316 |
} |
| 317 |
|
| 318 |
static SDL_bool EnqueueEvent_LockFree(SDL_EventQueue *queue, const SDL_Event *event) |
| 319 |
{ |
| 320 |
SDL_EventQueueEntry *entry; |
| 321 |
unsigned queue_pos; |
| 322 |
unsigned entry_seq; |
| 323 |
int delta; |
| 324 |
SDL_bool status; |
| 325 |
|
| 326 |
#ifdef TEST_SPINLOCK_FIFO |
| 327 |
/* This is a gate so an external thread can lock the queue */ |
| 328 |
SDL_AtomicLock(&queue->lock); |
| 329 |
SDL_assert(SDL_AtomicGet(&queue->watcher) == 0); |
| 330 |
SDL_AtomicIncRef(&queue->rwcount); |
| 331 |
SDL_AtomicUnlock(&queue->lock); |
| 332 |
#endif |
| 333 |
|
| 334 |
queue_pos = (unsigned)SDL_AtomicGet(&queue->enqueue_pos); |
| 335 |
for (;;) { |
| 336 |
entry = &queue->entries[queue_pos & WRAP_MASK]; |
| 337 |
entry_seq = (unsigned)SDL_AtomicGet(&entry->sequence); |
| 338 |
|
| 339 |
delta = (int)(entry_seq - queue_pos); |
| 340 |
if (delta == 0) { |
| 341 |
/* The entry and the queue position match, try to increment the queue position */ |
| 342 |
if (SDL_AtomicCAS(&queue->enqueue_pos, (int)queue_pos, (int)(queue_pos + 1))) { |
| 343 |
/* We own the object, fill it! */ |
| 344 |
entry->event = *event; |
| 345 |
SDL_AtomicSet(&entry->sequence, (int)(queue_pos + 1)); |
| 346 |
status = SDL_TRUE; |
| 347 |
break; |
| 348 |
} |
| 349 |
} else if (delta < 0) { |
| 350 |
/* We ran into an old queue entry, which means it still needs to be dequeued */ |
| 351 |
status = SDL_FALSE; |
| 352 |
break; |
| 353 |
} else { |
| 354 |
/* We ran into a new queue entry, get the new queue position */ |
| 355 |
queue_pos = (unsigned)SDL_AtomicGet(&queue->enqueue_pos); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
#ifdef TEST_SPINLOCK_FIFO |
| 360 |
(void)SDL_AtomicDecRef(&queue->rwcount); |
| 361 |
#endif |
| 362 |
return status; |
| 363 |
} |
| 364 |
|
| 365 |
static SDL_bool DequeueEvent_LockFree(SDL_EventQueue *queue, SDL_Event *event) |
| 366 |
{ |
| 367 |
SDL_EventQueueEntry *entry; |
| 368 |
unsigned queue_pos; |
| 369 |
unsigned entry_seq; |
| 370 |
int delta; |
| 371 |
SDL_bool status; |
| 372 |
|
| 373 |
#ifdef TEST_SPINLOCK_FIFO |
| 374 |
/* This is a gate so an external thread can lock the queue */ |
| 375 |
SDL_AtomicLock(&queue->lock); |
| 376 |
SDL_assert(SDL_AtomicGet(&queue->watcher) == 0); |
| 377 |
SDL_AtomicIncRef(&queue->rwcount); |
| 378 |
SDL_AtomicUnlock(&queue->lock); |
| 379 |
#endif |
| 380 |
|
| 381 |
queue_pos = (unsigned)SDL_AtomicGet(&queue->dequeue_pos); |
| 382 |
for (;;) { |
| 383 |
entry = &queue->entries[queue_pos & WRAP_MASK]; |
| 384 |
entry_seq = (unsigned)SDL_AtomicGet(&entry->sequence); |
| 385 |
|
| 386 |
delta = (int)(entry_seq - (queue_pos + 1)); |
| 387 |
if (delta == 0) { |
| 388 |
/* The entry and the queue position match, try to increment the queue position */ |
| 389 |
if (SDL_AtomicCAS(&queue->dequeue_pos, (int)queue_pos, (int)(queue_pos + 1))) { |
| 390 |
/* We own the object, fill it! */ |
| 391 |
*event = entry->event; |
| 392 |
SDL_AtomicSet(&entry->sequence, (int)(queue_pos + MAX_ENTRIES)); |
| 393 |
status = SDL_TRUE; |
| 394 |
break; |
| 395 |
} |
| 396 |
} else if (delta < 0) { |
| 397 |
/* We ran into an old queue entry, which means we've hit empty */ |
| 398 |
status = SDL_FALSE; |
| 399 |
break; |
| 400 |
} else { |
| 401 |
/* We ran into a new queue entry, get the new queue position */ |
| 402 |
queue_pos = (unsigned)SDL_AtomicGet(&queue->dequeue_pos); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
#ifdef TEST_SPINLOCK_FIFO |
| 407 |
(void)SDL_AtomicDecRef(&queue->rwcount); |
| 408 |
#endif |
| 409 |
return status; |
| 410 |
} |
| 411 |
|
| 412 |
static SDL_bool EnqueueEvent_Mutex(SDL_EventQueue *queue, const SDL_Event *event) |
| 413 |
{ |
| 414 |
SDL_EventQueueEntry *entry; |
| 415 |
unsigned queue_pos; |
| 416 |
unsigned entry_seq; |
| 417 |
int delta; |
| 418 |
SDL_bool status = SDL_FALSE; |
| 419 |
|
| 420 |
SDL_LockMutex(queue->mutex); |
| 421 |
|
| 422 |
queue_pos = (unsigned)queue->enqueue_pos.value; |
| 423 |
entry = &queue->entries[queue_pos & WRAP_MASK]; |
| 424 |
entry_seq = (unsigned)entry->sequence.value; |
| 425 |
|
| 426 |
delta = (int)(entry_seq - queue_pos); |
| 427 |
if (delta == 0) { |
| 428 |
++queue->enqueue_pos.value; |
| 429 |
|
| 430 |
/* We own the object, fill it! */ |
| 431 |
entry->event = *event; |
| 432 |
entry->sequence.value = (int)(queue_pos + 1); |
| 433 |
status = SDL_TRUE; |
| 434 |
} else if (delta < 0) { |
| 435 |
/* We ran into an old queue entry, which means it still needs to be dequeued */ |
| 436 |
} else { |
| 437 |
SDL_Log("ERROR: mutex failed!\n"); |
| 438 |
} |
| 439 |
|
| 440 |
SDL_UnlockMutex(queue->mutex); |
| 441 |
|
| 442 |
return status; |
| 443 |
} |
| 444 |
|
| 445 |
static SDL_bool DequeueEvent_Mutex(SDL_EventQueue *queue, SDL_Event *event) |
| 446 |
{ |
| 447 |
SDL_EventQueueEntry *entry; |
| 448 |
unsigned queue_pos; |
| 449 |
unsigned entry_seq; |
| 450 |
int delta; |
| 451 |
SDL_bool status = SDL_FALSE; |
| 452 |
|
| 453 |
SDL_LockMutex(queue->mutex); |
| 454 |
|
| 455 |
queue_pos = (unsigned)queue->dequeue_pos.value; |
| 456 |
entry = &queue->entries[queue_pos & WRAP_MASK]; |
| 457 |
entry_seq = (unsigned)entry->sequence.value; |
| 458 |
|
| 459 |
delta = (int)(entry_seq - (queue_pos + 1)); |
| 460 |
if (delta == 0) { |
| 461 |
++queue->dequeue_pos.value; |
| 462 |
|
| 463 |
/* We own the object, fill it! */ |
| 464 |
*event = entry->event; |
| 465 |
entry->sequence.value = (int)(queue_pos + MAX_ENTRIES); |
| 466 |
status = SDL_TRUE; |
| 467 |
} else if (delta < 0) { |
| 468 |
/* We ran into an old queue entry, which means we've hit empty */ |
| 469 |
} else { |
| 470 |
SDL_Log("ERROR: mutex failed!\n"); |
| 471 |
} |
| 472 |
|
| 473 |
SDL_UnlockMutex(queue->mutex); |
| 474 |
|
| 475 |
return status; |
| 476 |
} |
| 477 |
|
| 478 |
typedef struct |
| 479 |
{ |
| 480 |
SDL_EventQueue *queue; |
| 481 |
int index; |
| 482 |
char padding1[SDL_CACHELINE_SIZE - (sizeof(SDL_EventQueue *) + sizeof(int)) % SDL_CACHELINE_SIZE]; |
| 483 |
int waits; |
| 484 |
SDL_bool lock_free; |
| 485 |
char padding2[SDL_CACHELINE_SIZE - sizeof(int) - sizeof(SDL_bool)]; |
| 486 |
SDL_Thread *thread; |
| 487 |
} WriterData; |
| 488 |
|
| 489 |
typedef struct |
| 490 |
{ |
| 491 |
SDL_EventQueue *queue; |
| 492 |
int counters[NUM_WRITERS]; |
| 493 |
int waits; |
| 494 |
SDL_bool lock_free; |
| 495 |
char padding[SDL_CACHELINE_SIZE - (sizeof(SDL_EventQueue *) + sizeof(int) * NUM_WRITERS + sizeof(int) + sizeof(SDL_bool)) % SDL_CACHELINE_SIZE]; |
| 496 |
SDL_Thread *thread; |
| 497 |
} ReaderData; |
| 498 |
|
| 499 |
static int SDLCALL FIFO_Writer(void *_data) |
| 500 |
{ |
| 501 |
WriterData *data = (WriterData *)_data; |
| 502 |
SDL_EventQueue *queue = data->queue; |
| 503 |
int i; |
| 504 |
SDL_Event event; |
| 505 |
|
| 506 |
event.type = SDL_USEREVENT; |
| 507 |
event.user.windowID = 0; |
| 508 |
event.user.code = 0; |
| 509 |
event.user.data1 = data; |
| 510 |
event.user.data2 = NULL; |
| 511 |
|
| 512 |
if (data->lock_free) { |
| 513 |
for (i = 0; i < EVENTS_PER_WRITER; ++i) { |
| 514 |
event.user.code = i; |
| 515 |
while (!EnqueueEvent_LockFree(queue, &event)) { |
| 516 |
++data->waits; |
| 517 |
SDL_Delay(0); |
| 518 |
} |
| 519 |
} |
| 520 |
} else { |
| 521 |
for (i = 0; i < EVENTS_PER_WRITER; ++i) { |
| 522 |
event.user.code = i; |
| 523 |
while (!EnqueueEvent_Mutex(queue, &event)) { |
| 524 |
++data->waits; |
| 525 |
SDL_Delay(0); |
| 526 |
} |
| 527 |
} |
| 528 |
} |
| 529 |
return 0; |
| 530 |
} |
| 531 |
|
| 532 |
static int SDLCALL FIFO_Reader(void *_data) |
| 533 |
{ |
| 534 |
ReaderData *data = (ReaderData *)_data; |
| 535 |
SDL_EventQueue *queue = data->queue; |
| 536 |
SDL_Event event; |
| 537 |
|
| 538 |
if (data->lock_free) { |
| 539 |
for (;;) { |
| 540 |
if (DequeueEvent_LockFree(queue, &event)) { |
| 541 |
WriterData *writer = (WriterData *)event.user.data1; |
| 542 |
++data->counters[writer->index]; |
| 543 |
} else if (SDL_AtomicGet(&queue->active)) { |
| 544 |
++data->waits; |
| 545 |
SDL_Delay(0); |
| 546 |
} else { |
| 547 |
/* We drained the queue, we're done! */ |
| 548 |
break; |
| 549 |
} |
| 550 |
} |
| 551 |
} else { |
| 552 |
for (;;) { |
| 553 |
if (DequeueEvent_Mutex(queue, &event)) { |
| 554 |
WriterData *writer = (WriterData *)event.user.data1; |
| 555 |
++data->counters[writer->index]; |
| 556 |
} else if (SDL_AtomicGet(&queue->active)) { |
| 557 |
++data->waits; |
| 558 |
SDL_Delay(0); |
| 559 |
} else { |
| 560 |
/* We drained the queue, we're done! */ |
| 561 |
break; |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
return 0; |
| 566 |
} |
| 567 |
|
| 568 |
#ifdef TEST_SPINLOCK_FIFO |
| 569 |
/* This thread periodically locks the queue for no particular reason */ |
| 570 |
static int SDLCALL FIFO_Watcher(void *_data) |
| 571 |
{ |
| 572 |
SDL_EventQueue *queue = (SDL_EventQueue *)_data; |
| 573 |
|
| 574 |
while (SDL_AtomicGet(&queue->active)) { |
| 575 |
SDL_AtomicLock(&queue->lock); |
| 576 |
SDL_AtomicIncRef(&queue->watcher); |
| 577 |
while (SDL_AtomicGet(&queue->rwcount) > 0) { |
| 578 |
SDL_Delay(0); |
| 579 |
} |
| 580 |
/* Do queue manipulation here... */ |
| 581 |
(void)SDL_AtomicDecRef(&queue->watcher); |
| 582 |
SDL_AtomicUnlock(&queue->lock); |
| 583 |
|
| 584 |
/* Wait a bit... */ |
| 585 |
SDL_Delay(1); |
| 586 |
} |
| 587 |
return 0; |
| 588 |
} |
| 589 |
#endif /* TEST_SPINLOCK_FIFO */ |
| 590 |
|
| 591 |
static void RunFIFOTest(SDL_bool lock_free) |
| 592 |
{ |
| 593 |
SDL_EventQueue queue; |
| 594 |
SDL_Thread *fifo_thread = NULL; |
| 595 |
WriterData writerData[NUM_WRITERS]; |
| 596 |
ReaderData readerData[NUM_READERS]; |
| 597 |
Uint32 start, end; |
| 598 |
int i, j; |
| 599 |
int grand_total; |
| 600 |
char textBuffer[1024]; |
| 601 |
size_t len; |
| 602 |
|
| 603 |
SDL_Log("\nFIFO test---------------------------------------\n\n"); |
| 604 |
SDL_Log("Mode: %s\n", lock_free ? "LockFree" : "Mutex"); |
| 605 |
|
| 606 |
SDL_memset(&queue, 0xff, sizeof(queue)); |
| 607 |
|
| 608 |
InitEventQueue(&queue); |
| 609 |
if (!lock_free) { |
| 610 |
queue.mutex = SDL_CreateMutex(); |
| 611 |
} |
| 612 |
|
| 613 |
start = SDL_GetTicks(); |
| 614 |
|
| 615 |
#ifdef TEST_SPINLOCK_FIFO |
| 616 |
/* Start a monitoring thread */ |
| 617 |
if (lock_free) { |
| 618 |
fifo_thread = SDL_CreateThread(FIFO_Watcher, "FIFOWatcher", &queue); |
| 619 |
} |
| 620 |
#endif |
| 621 |
|
| 622 |
/* Start the readers first */ |
| 623 |
SDL_Log("Starting %d readers\n", NUM_READERS); |
| 624 |
SDL_zeroa(readerData); |
| 625 |
for (i = 0; i < NUM_READERS; ++i) { |
| 626 |
char name[64]; |
| 627 |
(void)SDL_snprintf(name, sizeof(name), "FIFOReader%d", i); |
| 628 |
readerData[i].queue = &queue; |
| 629 |
readerData[i].lock_free = lock_free; |
| 630 |
readerData[i].thread = SDL_CreateThread(FIFO_Reader, name, &readerData[i]); |
| 631 |
} |
| 632 |
|
| 633 |
/* Start up the writers */ |
| 634 |
SDL_Log("Starting %d writers\n", NUM_WRITERS); |
| 635 |
SDL_zeroa(writerData); |
| 636 |
for (i = 0; i < NUM_WRITERS; ++i) { |
| 637 |
char name[64]; |
| 638 |
(void)SDL_snprintf(name, sizeof(name), "FIFOWriter%d", i); |
| 639 |
writerData[i].queue = &queue; |
| 640 |
writerData[i].index = i; |
| 641 |
writerData[i].lock_free = lock_free; |
| 642 |
writerData[i].thread = SDL_CreateThread(FIFO_Writer, name, &writerData[i]); |
| 643 |
} |
| 644 |
|
| 645 |
/* Wait for the writers */ |
| 646 |
for (i = 0; i < NUM_WRITERS; ++i) { |
| 647 |
SDL_WaitThread(writerData[i].thread, NULL); |
| 648 |
} |
| 649 |
|
| 650 |
/* Shut down the queue so readers exit */ |
| 651 |
SDL_AtomicSet(&queue.active, 0); |
| 652 |
|
| 653 |
/* Wait for the readers */ |
| 654 |
for (i = 0; i < NUM_READERS; ++i) { |
| 655 |
SDL_WaitThread(readerData[i].thread, NULL); |
| 656 |
} |
| 657 |
|
| 658 |
end = SDL_GetTicks(); |
| 659 |
|
| 660 |
/* Wait for the FIFO thread */ |
| 661 |
if (fifo_thread) { |
| 662 |
SDL_WaitThread(fifo_thread, NULL); |
| 663 |
} |
| 664 |
|
| 665 |
if (!lock_free) { |
| 666 |
SDL_DestroyMutex(queue.mutex); |
| 667 |
} |
| 668 |
|
| 669 |
SDL_Log("Finished in %f sec\n", (end - start) / 1000.f); |
| 670 |
|
| 671 |
SDL_Log("\n"); |
| 672 |
for (i = 0; i < NUM_WRITERS; ++i) { |
| 673 |
SDL_Log("Writer %d wrote %d events, had %d waits\n", i, EVENTS_PER_WRITER, writerData[i].waits); |
| 674 |
} |
| 675 |
SDL_Log("Writers wrote %d total events\n", NUM_WRITERS * EVENTS_PER_WRITER); |
| 676 |
|
| 677 |
/* Print a breakdown of which readers read messages from which writer */ |
| 678 |
SDL_Log("\n"); |
| 679 |
grand_total = 0; |
| 680 |
for (i = 0; i < NUM_READERS; ++i) { |
| 681 |
int total = 0; |
| 682 |
for (j = 0; j < NUM_WRITERS; ++j) { |
| 683 |
total += readerData[i].counters[j]; |
| 684 |
} |
| 685 |
grand_total += total; |
| 686 |
SDL_Log("Reader %d read %d events, had %d waits\n", i, total, readerData[i].waits); |
| 687 |
(void)SDL_snprintf(textBuffer, sizeof(textBuffer), " { "); |
| 688 |
for (j = 0; j < NUM_WRITERS; ++j) { |
| 689 |
if (j > 0) { |
| 690 |
len = SDL_strlen(textBuffer); |
| 691 |
(void)SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, ", "); |
| 692 |
} |
| 693 |
len = SDL_strlen(textBuffer); |
| 694 |
(void)SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, "%d", readerData[i].counters[j]); |
| 695 |
} |
| 696 |
len = SDL_strlen(textBuffer); |
| 697 |
(void)SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, " }\n"); |
| 698 |
SDL_Log("%s", textBuffer); |
| 699 |
} |
| 700 |
SDL_Log("Readers read %d total events\n", grand_total); |
| 701 |
} |
| 702 |
|
| 703 |
/* End FIFO test */ |
| 704 |
/**************************************************************************/ |
| 705 |
|
| 706 |
int main(int argc, char *argv[]) |
| 707 |
{ |
| 708 |
SDLTest_CommonState *state; |
| 709 |
|
| 710 |
state = SDLTest_CommonCreateState(argv, 0); |
| 711 |
if (!state) { |
| 712 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError()); |
| 713 |
return 1; |
| 714 |
} |
| 715 |
|
| 716 |
/* Enable standard application logging */ |
| 717 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 718 |
|
| 719 |
if (!SDLTest_CommonDefaultArgs(state, argc, argv)) { |
| 720 |
return 1; |
| 721 |
} |
| 722 |
|
| 723 |
if (!SDLTest_CommonInit(state)) { |
| 724 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
| 725 |
return 1; |
| 726 |
} |
| 727 |
|
| 728 |
RunBasicTest(); |
| 729 |
|
| 730 |
if (SDL_getenv("SDL_TESTS_QUICK") != NULL) { |
| 731 |
SDL_Log("Not running slower tests"); |
| 732 |
return 0; |
| 733 |
} |
| 734 |
|
| 735 |
RunEpicTest(); |
| 736 |
/* This test is really slow, so don't run it by default */ |
| 737 |
#if 0 |
| 738 |
RunFIFOTest(SDL_FALSE); |
| 739 |
#endif |
| 740 |
RunFIFOTest(SDL_TRUE); |
| 741 |
SDL_Quit(); |
| 742 |
SDLTest_CommonQuit(state); |
| 743 |
return 0; |
| 744 |
} |
| 745 |
|
| 746 |
/* vi: set ts=4 sw=4 expandtab: */ |