annotate test/testatomic.c @ 5100:470ede30189c

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