🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

724 lines
20 KiB

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