testatomic.c 21 KB

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