1
0

testatomic.c 21 KB

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