Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
703 lines (575 loc) · 18.8 KB

testatomic.c

File metadata and controls

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