testatomic.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*
  2. Copyright (C) 1997-2014 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. #include "SDL_atomic.h"
  13. #include "SDL_assert.h"
  14. #include "SDL_cpuinfo.h"
  15. /*
  16. Absolutely basic tests just to see if we get the expected value
  17. after calling each function.
  18. */
  19. static
  20. char *
  21. tf(SDL_bool tf)
  22. {
  23. static char *t = "TRUE";
  24. static char *f = "FALSE";
  25. if (tf)
  26. {
  27. return t;
  28. }
  29. return f;
  30. }
  31. static
  32. void RunBasicTest()
  33. {
  34. int value;
  35. SDL_SpinLock lock = 0;
  36. SDL_atomic_t v;
  37. SDL_bool tfret = SDL_FALSE;
  38. SDL_Log("\nspin lock---------------------------------------\n\n");
  39. SDL_AtomicLock(&lock);
  40. SDL_Log("AtomicLock lock=%d\n", lock);
  41. SDL_AtomicUnlock(&lock);
  42. SDL_Log("AtomicUnlock lock=%d\n", lock);
  43. SDL_Log("\natomic -----------------------------------------\n\n");
  44. SDL_AtomicSet(&v, 0);
  45. tfret = SDL_AtomicSet(&v, 10) == 0 ? SDL_TRUE : SDL_FALSE;
  46. SDL_Log("AtomicSet(10) tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
  47. tfret = SDL_AtomicAdd(&v, 10) == 10 ? SDL_TRUE : SDL_FALSE;
  48. SDL_Log("AtomicAdd(10) tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
  49. SDL_AtomicSet(&v, 0);
  50. SDL_AtomicIncRef(&v);
  51. tfret = (SDL_AtomicGet(&v) == 1) ? SDL_TRUE : SDL_FALSE;
  52. SDL_Log("AtomicIncRef() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
  53. SDL_AtomicIncRef(&v);
  54. tfret = (SDL_AtomicGet(&v) == 2) ? SDL_TRUE : SDL_FALSE;
  55. SDL_Log("AtomicIncRef() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
  56. tfret = (SDL_AtomicDecRef(&v) == SDL_FALSE) ? SDL_TRUE : SDL_FALSE;
  57. SDL_Log("AtomicDecRef() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
  58. tfret = (SDL_AtomicDecRef(&v) == SDL_TRUE) ? SDL_TRUE : SDL_FALSE;
  59. SDL_Log("AtomicDecRef() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
  60. SDL_AtomicSet(&v, 10);
  61. tfret = (SDL_AtomicCAS(&v, 0, 20) == SDL_FALSE) ? SDL_TRUE : SDL_FALSE;
  62. SDL_Log("AtomicCAS() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
  63. value = SDL_AtomicGet(&v);
  64. tfret = (SDL_AtomicCAS(&v, value, 20) == SDL_TRUE) ? SDL_TRUE : SDL_FALSE;
  65. SDL_Log("AtomicCAS() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
  66. }
  67. /**************************************************************************/
  68. /* Atomic operation test
  69. * Adapted with permission from code by Michael Davidsaver at:
  70. * http://bazaar.launchpad.net/~mdavidsaver/epics-base/atomic/revision/12105#src/libCom/test/epicsAtomicTest.c
  71. * Original copyright 2010 Brookhaven Science Associates as operator of Brookhaven National Lab
  72. * http://www.aps.anl.gov/epics/license/open.php
  73. */
  74. /* Tests semantics of atomic operations. Also a stress test
  75. * to see if they are really atomic.
  76. *
  77. * Several threads adding to the same variable.
  78. * at the end the value is compared with the expected
  79. * and with a non-atomic counter.
  80. */
  81. /* Number of concurrent incrementers */
  82. #define NThreads 2
  83. #define CountInc 100
  84. #define VALBITS (sizeof(atomicValue)*8)
  85. #define atomicValue int
  86. #define CountTo ((atomicValue)((unsigned int)(1<<(VALBITS-1))-1))
  87. #define NInter (CountTo/CountInc/NThreads)
  88. #define Expect (CountTo-NInter*CountInc*NThreads)
  89. SDL_COMPILE_TIME_ASSERT(size, CountTo>0); /* 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 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. volatile SDL_bool 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. #endif
  236. queue->active = SDL_TRUE;
  237. }
  238. static SDL_bool EnqueueEvent_LockFree(SDL_EventQueue *queue, const SDL_Event *event)
  239. {
  240. SDL_EventQueueEntry *entry;
  241. unsigned queue_pos;
  242. unsigned entry_seq;
  243. int delta;
  244. SDL_bool status;
  245. #ifdef TEST_SPINLOCK_FIFO
  246. /* This is a gate so an external thread can lock the queue */
  247. SDL_AtomicLock(&queue->lock);
  248. SDL_assert(SDL_AtomicGet(&queue->watcher) == 0);
  249. SDL_AtomicIncRef(&queue->rwcount);
  250. SDL_AtomicUnlock(&queue->lock);
  251. #endif
  252. queue_pos = (unsigned)SDL_AtomicGet(&queue->enqueue_pos);
  253. for ( ; ; ) {
  254. entry = &queue->entries[queue_pos & WRAP_MASK];
  255. entry_seq = (unsigned)SDL_AtomicGet(&entry->sequence);
  256. delta = (int)(entry_seq - queue_pos);
  257. if (delta == 0) {
  258. /* The entry and the queue position match, try to increment the queue position */
  259. if (SDL_AtomicCAS(&queue->enqueue_pos, (int)queue_pos, (int)(queue_pos+1))) {
  260. /* We own the object, fill it! */
  261. entry->event = *event;
  262. SDL_AtomicSet(&entry->sequence, (int)(queue_pos + 1));
  263. status = SDL_TRUE;
  264. break;
  265. }
  266. } else if (delta < 0) {
  267. /* We ran into an old queue entry, which means it still needs to be dequeued */
  268. status = SDL_FALSE;
  269. break;
  270. } else {
  271. /* We ran into a new queue entry, get the new queue position */
  272. queue_pos = (unsigned)SDL_AtomicGet(&queue->enqueue_pos);
  273. }
  274. }
  275. #ifdef TEST_SPINLOCK_FIFO
  276. SDL_AtomicDecRef(&queue->rwcount);
  277. #endif
  278. return status;
  279. }
  280. static SDL_bool DequeueEvent_LockFree(SDL_EventQueue *queue, SDL_Event *event)
  281. {
  282. SDL_EventQueueEntry *entry;
  283. unsigned queue_pos;
  284. unsigned entry_seq;
  285. int delta;
  286. SDL_bool status;
  287. #ifdef TEST_SPINLOCK_FIFO
  288. /* This is a gate so an external thread can lock the queue */
  289. SDL_AtomicLock(&queue->lock);
  290. SDL_assert(SDL_AtomicGet(&queue->watcher) == 0);
  291. SDL_AtomicIncRef(&queue->rwcount);
  292. SDL_AtomicUnlock(&queue->lock);
  293. #endif
  294. queue_pos = (unsigned)SDL_AtomicGet(&queue->dequeue_pos);
  295. for ( ; ; ) {
  296. entry = &queue->entries[queue_pos & WRAP_MASK];
  297. entry_seq = (unsigned)SDL_AtomicGet(&entry->sequence);
  298. delta = (int)(entry_seq - (queue_pos + 1));
  299. if (delta == 0) {
  300. /* The entry and the queue position match, try to increment the queue position */
  301. if (SDL_AtomicCAS(&queue->dequeue_pos, (int)queue_pos, (int)(queue_pos+1))) {
  302. /* We own the object, fill it! */
  303. *event = entry->event;
  304. SDL_AtomicSet(&entry->sequence, (int)(queue_pos+MAX_ENTRIES));
  305. status = SDL_TRUE;
  306. break;
  307. }
  308. } else if (delta < 0) {
  309. /* We ran into an old queue entry, which means we've hit empty */
  310. status = SDL_FALSE;
  311. break;
  312. } else {
  313. /* We ran into a new queue entry, get the new queue position */
  314. queue_pos = (unsigned)SDL_AtomicGet(&queue->dequeue_pos);
  315. }
  316. }
  317. #ifdef TEST_SPINLOCK_FIFO
  318. SDL_AtomicDecRef(&queue->rwcount);
  319. #endif
  320. return status;
  321. }
  322. static SDL_bool EnqueueEvent_Mutex(SDL_EventQueue *queue, const SDL_Event *event)
  323. {
  324. SDL_EventQueueEntry *entry;
  325. unsigned queue_pos;
  326. unsigned entry_seq;
  327. int delta;
  328. SDL_bool status = SDL_FALSE;
  329. SDL_LockMutex(queue->mutex);
  330. queue_pos = (unsigned)queue->enqueue_pos.value;
  331. entry = &queue->entries[queue_pos & WRAP_MASK];
  332. entry_seq = (unsigned)entry->sequence.value;
  333. delta = (int)(entry_seq - queue_pos);
  334. if (delta == 0) {
  335. ++queue->enqueue_pos.value;
  336. /* We own the object, fill it! */
  337. entry->event = *event;
  338. entry->sequence.value = (int)(queue_pos + 1);
  339. status = SDL_TRUE;
  340. } else if (delta < 0) {
  341. /* We ran into an old queue entry, which means it still needs to be dequeued */
  342. } else {
  343. SDL_Log("ERROR: mutex failed!\n");
  344. }
  345. SDL_UnlockMutex(queue->mutex);
  346. return status;
  347. }
  348. static SDL_bool DequeueEvent_Mutex(SDL_EventQueue *queue, SDL_Event *event)
  349. {
  350. SDL_EventQueueEntry *entry;
  351. unsigned queue_pos;
  352. unsigned entry_seq;
  353. int delta;
  354. SDL_bool status = SDL_FALSE;
  355. SDL_LockMutex(queue->mutex);
  356. queue_pos = (unsigned)queue->dequeue_pos.value;
  357. entry = &queue->entries[queue_pos & WRAP_MASK];
  358. entry_seq = (unsigned)entry->sequence.value;
  359. delta = (int)(entry_seq - (queue_pos + 1));
  360. if (delta == 0) {
  361. ++queue->dequeue_pos.value;
  362. /* We own the object, fill it! */
  363. *event = entry->event;
  364. entry->sequence.value = (int)(queue_pos + MAX_ENTRIES);
  365. status = SDL_TRUE;
  366. } else if (delta < 0) {
  367. /* We ran into an old queue entry, which means we've hit empty */
  368. } else {
  369. SDL_Log("ERROR: mutex failed!\n");
  370. }
  371. SDL_UnlockMutex(queue->mutex);
  372. return status;
  373. }
  374. static SDL_sem *writersDone;
  375. static SDL_sem *readersDone;
  376. static SDL_atomic_t writersRunning;
  377. static SDL_atomic_t readersRunning;
  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. SDL_bool lock_free;
  385. char padding2[SDL_CACHELINE_SIZE-sizeof(int)-sizeof(SDL_bool)];
  386. } WriterData;
  387. typedef struct
  388. {
  389. SDL_EventQueue *queue;
  390. int counters[NUM_WRITERS];
  391. int waits;
  392. SDL_bool lock_free;
  393. char padding[SDL_CACHELINE_SIZE-(sizeof(SDL_EventQueue*)+sizeof(int)*NUM_WRITERS+sizeof(int)+sizeof(SDL_bool))%SDL_CACHELINE_SIZE];
  394. } ReaderData;
  395. static int FIFO_Writer(void* _data)
  396. {
  397. WriterData *data = (WriterData *)_data;
  398. SDL_EventQueue *queue = data->queue;
  399. int i;
  400. SDL_Event event;
  401. event.type = SDL_USEREVENT;
  402. event.user.windowID = 0;
  403. event.user.code = 0;
  404. event.user.data1 = data;
  405. event.user.data2 = NULL;
  406. if (data->lock_free) {
  407. for (i = 0; i < EVENTS_PER_WRITER; ++i) {
  408. event.user.code = i;
  409. while (!EnqueueEvent_LockFree(queue, &event)) {
  410. ++data->waits;
  411. SDL_Delay(0);
  412. }
  413. }
  414. } else {
  415. for (i = 0; i < EVENTS_PER_WRITER; ++i) {
  416. event.user.code = i;
  417. while (!EnqueueEvent_Mutex(queue, &event)) {
  418. ++data->waits;
  419. SDL_Delay(0);
  420. }
  421. }
  422. }
  423. SDL_AtomicAdd(&writersRunning, -1);
  424. SDL_SemPost(writersDone);
  425. return 0;
  426. }
  427. static int 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 (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 (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. SDL_AtomicAdd(&readersRunning, -1);
  460. SDL_SemPost(readersDone);
  461. return 0;
  462. }
  463. #ifdef TEST_SPINLOCK_FIFO
  464. /* This thread periodically locks the queue for no particular reason */
  465. static int FIFO_Watcher(void* _data)
  466. {
  467. SDL_EventQueue *queue = (SDL_EventQueue *)_data;
  468. while (queue->active) {
  469. SDL_AtomicLock(&queue->lock);
  470. SDL_AtomicIncRef(&queue->watcher);
  471. while (SDL_AtomicGet(&queue->rwcount) > 0) {
  472. SDL_Delay(0);
  473. }
  474. /* Do queue manipulation here... */
  475. SDL_AtomicDecRef(&queue->watcher);
  476. SDL_AtomicUnlock(&queue->lock);
  477. /* Wait a bit... */
  478. SDL_Delay(1);
  479. }
  480. return 0;
  481. }
  482. #endif /* TEST_SPINLOCK_FIFO */
  483. static void RunFIFOTest(SDL_bool lock_free)
  484. {
  485. SDL_EventQueue queue;
  486. WriterData writerData[NUM_WRITERS];
  487. ReaderData readerData[NUM_READERS];
  488. Uint32 start, end;
  489. int i, j;
  490. int grand_total;
  491. char textBuffer[1024];
  492. int len;
  493. SDL_Log("\nFIFO test---------------------------------------\n\n");
  494. SDL_Log("Mode: %s\n", lock_free ? "LockFree" : "Mutex");
  495. readersDone = SDL_CreateSemaphore(0);
  496. writersDone = SDL_CreateSemaphore(0);
  497. SDL_memset(&queue, 0xff, sizeof(queue));
  498. InitEventQueue(&queue);
  499. if (!lock_free) {
  500. queue.mutex = SDL_CreateMutex();
  501. }
  502. start = SDL_GetTicks();
  503. #ifdef TEST_SPINLOCK_FIFO
  504. /* Start a monitoring thread */
  505. if (lock_free) {
  506. SDL_CreateThread(FIFO_Watcher, "FIFOWatcher", &queue);
  507. }
  508. #endif
  509. /* Start the readers first */
  510. SDL_Log("Starting %d readers\n", NUM_READERS);
  511. SDL_zero(readerData);
  512. SDL_AtomicSet(&readersRunning, NUM_READERS);
  513. for (i = 0; i < NUM_READERS; ++i) {
  514. char name[64];
  515. SDL_snprintf(name, sizeof (name), "FIFOReader%d", i);
  516. readerData[i].queue = &queue;
  517. readerData[i].lock_free = lock_free;
  518. SDL_CreateThread(FIFO_Reader, name, &readerData[i]);
  519. }
  520. /* Start up the writers */
  521. SDL_Log("Starting %d writers\n", NUM_WRITERS);
  522. SDL_zero(writerData);
  523. SDL_AtomicSet(&writersRunning, NUM_WRITERS);
  524. for (i = 0; i < NUM_WRITERS; ++i) {
  525. char name[64];
  526. SDL_snprintf(name, sizeof (name), "FIFOWriter%d", i);
  527. writerData[i].queue = &queue;
  528. writerData[i].index = i;
  529. writerData[i].lock_free = lock_free;
  530. SDL_CreateThread(FIFO_Writer, name, &writerData[i]);
  531. }
  532. /* Wait for the writers */
  533. while (SDL_AtomicGet(&writersRunning) > 0) {
  534. SDL_SemWait(writersDone);
  535. }
  536. /* Shut down the queue so readers exit */
  537. queue.active = SDL_FALSE;
  538. /* Wait for the readers */
  539. while (SDL_AtomicGet(&readersRunning) > 0) {
  540. SDL_SemWait(readersDone);
  541. }
  542. end = SDL_GetTicks();
  543. SDL_DestroySemaphore(readersDone);
  544. SDL_DestroySemaphore(writersDone);
  545. if (!lock_free) {
  546. SDL_DestroyMutex(queue.mutex);
  547. }
  548. SDL_Log("Finished in %f sec\n", (end - start) / 1000.f);
  549. SDL_Log("\n");
  550. for (i = 0; i < NUM_WRITERS; ++i) {
  551. SDL_Log("Writer %d wrote %d events, had %d waits\n", i, EVENTS_PER_WRITER, writerData[i].waits);
  552. }
  553. SDL_Log("Writers wrote %d total events\n", NUM_WRITERS*EVENTS_PER_WRITER);
  554. /* Print a breakdown of which readers read messages from which writer */
  555. SDL_Log("\n");
  556. grand_total = 0;
  557. for (i = 0; i < NUM_READERS; ++i) {
  558. int total = 0;
  559. for (j = 0; j < NUM_WRITERS; ++j) {
  560. total += readerData[i].counters[j];
  561. }
  562. grand_total += total;
  563. SDL_Log("Reader %d read %d events, had %d waits\n", i, total, readerData[i].waits);
  564. SDL_snprintf(textBuffer, sizeof(textBuffer), " { ");
  565. for (j = 0; j < NUM_WRITERS; ++j) {
  566. if (j > 0) {
  567. len = SDL_strlen(textBuffer);
  568. SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, ", ");
  569. }
  570. len = SDL_strlen(textBuffer);
  571. SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, "%d", readerData[i].counters[j]);
  572. }
  573. len = SDL_strlen(textBuffer);
  574. SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, " }\n");
  575. SDL_Log(textBuffer);
  576. }
  577. SDL_Log("Readers read %d total events\n", grand_total);
  578. }
  579. /* End FIFO test */
  580. /**************************************************************************/
  581. int
  582. main(int argc, char *argv[])
  583. {
  584. /* Enable standard application logging */
  585. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  586. RunBasicTest();
  587. RunEpicTest();
  588. /* This test is really slow, so don't run it by default */
  589. #if 0
  590. RunFIFOTest(SDL_FALSE);
  591. #endif
  592. RunFIFOTest(SDL_TRUE);
  593. return 0;
  594. }
  595. /* vi: set ts=4 sw=4 expandtab: */