annotate test/testatomic.c @ 5266:595814f561f7

There is only one width and height for the window. If those are changed during the course of a fullscreen mode change, then they'll stay that size when returning to windowed mode.
author Sam Lantinga <slouken@libsdl.org>
date Fri, 11 Feb 2011 20:49:13 -0800
parents 02b860cbc7ce
children
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"
5118
02b860cbc7ce Bump up the default cache line size.
Sam Lantinga <slouken@libsdl.org>
parents: 5117
diff changeset
6 #include "SDL_cpuinfo.h"
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
7
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
8 /*
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
9 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
10 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
11 */
c297230efc75 Disabling 64 bit atomics operations until I figure out why they do not link.
Bob Pendleton <bob@pendleton.com>
parents: 3199
diff changeset
12
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
13 static
3338
9de326b3099c Fixed bug #817
Sam Lantinga <slouken@libsdl.org>
parents: 3327
diff changeset
14 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
15 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
16 {
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
17 static char *t = "TRUE";
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
18 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
19
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
20 if (tf)
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
21 {
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
22 return t;
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
23 }
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
24
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
25 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
26 }
3338
9de326b3099c Fixed bug #817
Sam Lantinga <slouken@libsdl.org>
parents: 3327
diff changeset
27
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
28 static
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
29 void RunBasicTest()
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
30 {
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
31 int value;
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
32 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
33
5013
9d6b527e0912 Fixed compiler errors
Sam Lantinga <slouken@libsdl.org>
parents: 5006
diff changeset
34 SDL_atomic_t v;
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
35 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
36
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
37 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
38
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
39 SDL_AtomicLock(&lock);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
40 printf("AtomicLock lock=%d\n", lock);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
41 SDL_AtomicUnlock(&lock);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
42 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
43
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
44 printf("\natomic -----------------------------------------\n\n");
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
45
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
46 SDL_AtomicSet(&v, 0);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
47 tfret = SDL_AtomicSet(&v, 10) == 0;
5013
9d6b527e0912 Fixed compiler errors
Sam Lantinga <slouken@libsdl.org>
parents: 5006
diff changeset
48 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
49 tfret = SDL_AtomicAdd(&v, 10) == 10;
5013
9d6b527e0912 Fixed compiler errors
Sam Lantinga <slouken@libsdl.org>
parents: 5006
diff changeset
50 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
51
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
52 SDL_AtomicSet(&v, 0);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
53 SDL_AtomicIncRef(&v);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
54 tfret = (SDL_AtomicGet(&v) == 1);
5013
9d6b527e0912 Fixed compiler errors
Sam Lantinga <slouken@libsdl.org>
parents: 5006
diff changeset
55 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
56 SDL_AtomicIncRef(&v);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
57 tfret = (SDL_AtomicGet(&v) == 2);
5013
9d6b527e0912 Fixed compiler errors
Sam Lantinga <slouken@libsdl.org>
parents: 5006
diff changeset
58 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
59 tfret = (SDL_AtomicDecRef(&v) == SDL_FALSE);
5013
9d6b527e0912 Fixed compiler errors
Sam Lantinga <slouken@libsdl.org>
parents: 5006
diff changeset
60 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
61 tfret = (SDL_AtomicDecRef(&v) == SDL_TRUE);
5013
9d6b527e0912 Fixed compiler errors
Sam Lantinga <slouken@libsdl.org>
parents: 5006
diff changeset
62 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
63
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
64 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
65 tfret = (SDL_AtomicCAS(&v, 0, 20) == SDL_FALSE);
5013
9d6b527e0912 Fixed compiler errors
Sam Lantinga <slouken@libsdl.org>
parents: 5006
diff changeset
66 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
67 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
68 tfret = (SDL_AtomicCAS(&v, value, 20) == SDL_TRUE);
5013
9d6b527e0912 Fixed compiler errors
Sam Lantinga <slouken@libsdl.org>
parents: 5006
diff changeset
69 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
70 }
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
71
5018
342b158efbbe Michael gave permission to use his test code
Sam Lantinga <slouken@libsdl.org>
parents: 5013
diff changeset
72 /**************************************************************************/
342b158efbbe Michael gave permission to use his test code
Sam Lantinga <slouken@libsdl.org>
parents: 5013
diff changeset
73 /* Atomic operation test
342b158efbbe Michael gave permission to use his test code
Sam Lantinga <slouken@libsdl.org>
parents: 5013
diff changeset
74 * 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
75 * 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
76 * 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
77 * 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
78 */
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
79
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
80 /* 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
81 * 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
82 *
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
83 * 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
84 * 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
85 * 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
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
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
88 /* 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
89 #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
90 #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
91 #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
92
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 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
94 #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
95 #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
96 #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
97
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
98 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
99
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
100 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
101
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
102 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
103
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
104 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
105
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
106 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
107
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
108 static
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
109 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
110 {
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
111 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
112 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
113 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
114 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
115 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
116 }
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_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
118 SDL_SemPost(threadDone);
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
119 return 0;
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
120 }
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
121
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
122 static
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
123 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
124 {
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
125 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
126 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
127
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
128 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
129
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
130 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
131
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
132 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
133
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
134 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
135 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
136
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
137 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
138 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
139
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
140 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
141
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
142 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
143
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
144 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
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
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
147 static
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
148 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
149 {
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
150 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
151 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
152
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
153 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
154
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
155 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
156 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
157
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
158 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
159 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
160 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
161
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
162 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
163
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
164 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
165 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
166 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
167 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
168
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
169 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
170 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
171 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
172 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
173
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
174 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
175
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
176 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
177 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
178 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
179 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
180
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
181 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
182 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
183 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
184 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
185
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
186 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
187
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
188 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
189 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
190 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
191 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
192
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
193 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
194 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
195 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
196 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
197
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
198 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
199
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
200 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
201 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
202 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
203 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
204
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
205 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
206 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
207 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
208 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
209
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
210 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
211 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
212 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
213 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
214
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
215 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
216 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
217 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
218 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
219
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
220 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 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
222
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
223 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
224 runAdder();
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
225
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
226 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
227 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
228 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
229 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
230 }
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
231
5018
342b158efbbe Michael gave permission to use his test code
Sam Lantinga <slouken@libsdl.org>
parents: 5013
diff changeset
232 /* End atomic operation test */
342b158efbbe Michael gave permission to use his test code
Sam Lantinga <slouken@libsdl.org>
parents: 5013
diff changeset
233 /**************************************************************************/
342b158efbbe Michael gave permission to use his test code
Sam Lantinga <slouken@libsdl.org>
parents: 5013
diff changeset
234
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
235 /**************************************************************************/
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
236 /* Lock-free FIFO test */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
237
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
238 /* This is useful to test the impact of another thread locking the queue
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
239 entirely for heavy-weight manipulation.
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
240 */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
241 #define TEST_SPINLOCK_FIFO
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
242
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
243 #define NUM_READERS 4
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
244 #define NUM_WRITERS 4
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
245 #define EVENTS_PER_WRITER 1000000
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
246
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
247 /* 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
248 #define MAX_ENTRIES 256
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
249 #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
250
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
251 typedef struct
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
252 {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
253 SDL_atomic_t sequence;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
254 SDL_Event event;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
255 } SDL_EventQueueEntry;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
256
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
257 typedef struct
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
258 {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
259 SDL_EventQueueEntry entries[MAX_ENTRIES];
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
260
5117
427998ff3bcf Added cache line size info in SDL_cpuinfo.h
Sam Lantinga <slouken@libsdl.org>
parents: 5116
diff changeset
261 char cache_pad1[SDL_CACHELINE_SIZE-((sizeof(SDL_EventQueueEntry)*MAX_ENTRIES)%SDL_CACHELINE_SIZE)];
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
262
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
263 SDL_atomic_t enqueue_pos;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
264
5117
427998ff3bcf Added cache line size info in SDL_cpuinfo.h
Sam Lantinga <slouken@libsdl.org>
parents: 5116
diff changeset
265 char cache_pad2[SDL_CACHELINE_SIZE-sizeof(SDL_atomic_t)];
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
266
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
267 SDL_atomic_t dequeue_pos;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
268
5117
427998ff3bcf Added cache line size info in SDL_cpuinfo.h
Sam Lantinga <slouken@libsdl.org>
parents: 5116
diff changeset
269 char cache_pad3[SDL_CACHELINE_SIZE-sizeof(SDL_atomic_t)];
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
270
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
271 #ifdef TEST_SPINLOCK_FIFO
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
272 SDL_SpinLock lock;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
273 SDL_atomic_t rwcount;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
274 SDL_atomic_t watcher;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
275
5117
427998ff3bcf Added cache line size info in SDL_cpuinfo.h
Sam Lantinga <slouken@libsdl.org>
parents: 5116
diff changeset
276 char cache_pad4[SDL_CACHELINE_SIZE-sizeof(SDL_SpinLock)-2*sizeof(SDL_atomic_t)];
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
277 #endif
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
278
5104
42a7591530d5 Since we're directly reading and writing 'active' from different threads, it needs to be flagged volatile.
Sam Lantinga <slouken@libsdl.org>
parents: 5101
diff changeset
279 volatile SDL_bool active;
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
280
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
281 /* 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
282 SDL_mutex *mutex;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
283
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
284 } SDL_EventQueue;
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 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
287 {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
288 int i;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
289
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
290 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
291 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
292 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
293 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
294 SDL_AtomicSet(&queue->dequeue_pos, 0);
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
295 #ifdef TEST_SPINLOCK_FIFO
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
296 queue->lock = 0;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
297 SDL_AtomicSet(&queue->rwcount, 0);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
298 #endif
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
299 queue->active = SDL_TRUE;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
300 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
301
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
302 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
303 {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
304 SDL_EventQueueEntry *entry;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
305 unsigned queue_pos;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
306 unsigned entry_seq;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
307 int delta;
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
308 SDL_bool status;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
309
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
310 #ifdef TEST_SPINLOCK_FIFO
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
311 /* This is a gate so an external thread can lock the queue */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
312 SDL_AtomicLock(&queue->lock);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
313 SDL_assert(SDL_AtomicGet(&queue->watcher) == 0);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
314 SDL_AtomicIncRef(&queue->rwcount);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
315 SDL_AtomicUnlock(&queue->lock);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
316 #endif
5100
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 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
319 for ( ; ; ) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
320 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
321 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
322
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
323 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
324 if (delta == 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
325 /* 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
326 if (SDL_AtomicCAS(&queue->enqueue_pos, (int)queue_pos, (int)(queue_pos+1))) {
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
327 /* We own the object, fill it! */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
328 entry->event = *event;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
329 SDL_AtomicSet(&entry->sequence, (int)(queue_pos + 1));
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
330 status = SDL_TRUE;
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
331 break;
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 } else if (delta < 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
334 /* We ran into an old queue entry, which means it still needs to be dequeued */
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
335 status = SDL_FALSE;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
336 break;
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
337 } else {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
338 /* 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
339 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
340 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
341 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
342
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
343 #ifdef TEST_SPINLOCK_FIFO
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
344 SDL_AtomicDecRef(&queue->rwcount);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
345 #endif
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
346 return status;
5100
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
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
349 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
350 {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
351 SDL_EventQueueEntry *entry;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
352 unsigned queue_pos;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
353 unsigned entry_seq;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
354 int delta;
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
355 SDL_bool status;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
356
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
357 #ifdef TEST_SPINLOCK_FIFO
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
358 /* This is a gate so an external thread can lock the queue */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
359 SDL_AtomicLock(&queue->lock);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
360 SDL_assert(SDL_AtomicGet(&queue->watcher) == 0);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
361 SDL_AtomicIncRef(&queue->rwcount);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
362 SDL_AtomicUnlock(&queue->lock);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
363 #endif
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
364
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
365 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
366 for ( ; ; ) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
367 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
368 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
369
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
370 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
371 if (delta == 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
372 /* 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
373 if (SDL_AtomicCAS(&queue->dequeue_pos, (int)queue_pos, (int)(queue_pos+1))) {
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
374 /* We own the object, fill it! */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
375 *event = entry->event;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
376 SDL_AtomicSet(&entry->sequence, (int)(queue_pos+MAX_ENTRIES));
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
377 status = SDL_TRUE;
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
378 break;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
379 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
380 } else if (delta < 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
381 /* We ran into an old queue entry, which means we've hit empty */
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
382 status = SDL_FALSE;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
383 break;
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
384 } else {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
385 /* 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
386 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
387 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
388 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
389
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
390 #ifdef TEST_SPINLOCK_FIFO
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
391 SDL_AtomicDecRef(&queue->rwcount);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
392 #endif
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
393 return status;
5100
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
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
396 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
397 {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
398 SDL_EventQueueEntry *entry;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
399 unsigned queue_pos;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
400 unsigned entry_seq;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
401 int delta;
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
402 SDL_bool status = SDL_FALSE;
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
403
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
404 SDL_mutexP(queue->mutex);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
405
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
406 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
407 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
408 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
409
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
410 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
411 if (delta == 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
412 ++queue->enqueue_pos.value;
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
413
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
414 /* We own the object, fill it! */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
415 entry->event = *event;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
416 entry->sequence.value = (int)(queue_pos + 1);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
417 status = SDL_TRUE;
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
418 } else if (delta < 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
419 /* 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
420 } else {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
421 printf("ERROR: mutex failed!\n");
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
422 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
423
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
424 SDL_mutexV(queue->mutex);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
425
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
426 return status;
5100
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
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
429 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
430 {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
431 SDL_EventQueueEntry *entry;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
432 unsigned queue_pos;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
433 unsigned entry_seq;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
434 int delta;
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
435 SDL_bool status = SDL_FALSE;
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
436
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
437 SDL_mutexP(queue->mutex);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
438
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
439 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
440 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
441 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
442
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
443 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
444 if (delta == 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
445 ++queue->dequeue_pos.value;
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
446
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
447 /* We own the object, fill it! */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
448 *event = entry->event;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
449 entry->sequence.value = (int)(queue_pos + MAX_ENTRIES);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
450 status = SDL_TRUE;
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
451 } else if (delta < 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
452 /* 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
453 } else {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
454 printf("ERROR: mutex failed!\n");
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
455 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
456
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
457 SDL_mutexV(queue->mutex);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
458
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
459 return status;
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
460 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
461
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
462 static SDL_sem *writersDone;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
463 static SDL_sem *readersDone;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
464 static SDL_atomic_t writersRunning;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
465 static SDL_atomic_t readersRunning;
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 typedef struct
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
468 {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
469 SDL_EventQueue *queue;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
470 int index;
5117
427998ff3bcf Added cache line size info in SDL_cpuinfo.h
Sam Lantinga <slouken@libsdl.org>
parents: 5116
diff changeset
471 char padding1[SDL_CACHELINE_SIZE-(sizeof(SDL_EventQueue*)+sizeof(int))%SDL_CACHELINE_SIZE];
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
472 int waits;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
473 SDL_bool lock_free;
5117
427998ff3bcf Added cache line size info in SDL_cpuinfo.h
Sam Lantinga <slouken@libsdl.org>
parents: 5116
diff changeset
474 char padding2[SDL_CACHELINE_SIZE-sizeof(int)-sizeof(SDL_bool)];
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
475 } WriterData;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
476
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
477 typedef struct
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
478 {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
479 SDL_EventQueue *queue;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
480 int counters[NUM_WRITERS];
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
481 int waits;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
482 SDL_bool lock_free;
5117
427998ff3bcf Added cache line size info in SDL_cpuinfo.h
Sam Lantinga <slouken@libsdl.org>
parents: 5116
diff changeset
483 char padding[SDL_CACHELINE_SIZE-(sizeof(SDL_EventQueue*)+sizeof(int)*NUM_WRITERS+sizeof(int)+sizeof(SDL_bool))%SDL_CACHELINE_SIZE];
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
484 } ReaderData;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
485
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
486 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
487 {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
488 WriterData *data = (WriterData *)_data;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
489 SDL_EventQueue *queue = data->queue;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
490 int index = data->index;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
491 int i;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
492 SDL_Event event;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
493
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
494 event.type = SDL_USEREVENT;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
495 event.user.windowID = 0;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
496 event.user.code = 0;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
497 event.user.data1 = data;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
498 event.user.data2 = NULL;
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 if (data->lock_free) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
501 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
502 event.user.code = i;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
503 while (!EnqueueEvent_LockFree(queue, &event)) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
504 ++data->waits;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
505 SDL_Delay(0);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
506 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
507 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
508 } else {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
509 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
510 event.user.code = i;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
511 while (!EnqueueEvent_Mutex(queue, &event)) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
512 ++data->waits;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
513 SDL_Delay(0);
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 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
516 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
517 SDL_AtomicAdd(&writersRunning, -1);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
518 SDL_SemPost(writersDone);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
519 return 0;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
520 }
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 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
523 {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
524 ReaderData *data = (ReaderData *)_data;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
525 SDL_EventQueue *queue = data->queue;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
526 SDL_Event event;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
527
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
528 if (data->lock_free) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
529 for ( ; ; ) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
530 if (DequeueEvent_LockFree(queue, &event)) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
531 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
532 ++data->counters[writer->index];
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
533 } else if (queue->active) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
534 ++data->waits;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
535 SDL_Delay(0);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
536 } else {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
537 /* 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
538 break;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
539 }
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 } else {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
542 for ( ; ; ) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
543 if (DequeueEvent_Mutex(queue, &event)) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
544 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
545 ++data->counters[writer->index];
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
546 } else if (queue->active) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
547 ++data->waits;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
548 SDL_Delay(0);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
549 } else {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
550 /* 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
551 break;
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 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
555 SDL_AtomicAdd(&readersRunning, -1);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
556 SDL_SemPost(readersDone);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
557 return 0;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
558 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
559
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
560 #ifdef TEST_SPINLOCK_FIFO
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
561 /* This thread periodically locks the queue for no particular reason */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
562 static int FIFO_Watcher(void* _data)
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
563 {
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
564 SDL_EventQueue *queue = (SDL_EventQueue *)_data;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
565
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
566 while (queue->active) {
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
567 SDL_AtomicLock(&queue->lock);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
568 SDL_AtomicIncRef(&queue->watcher);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
569 while (SDL_AtomicGet(&queue->rwcount) > 0) {
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
570 SDL_Delay(0);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
571 }
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
572 /* Do queue manipulation here... */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
573 SDL_AtomicDecRef(&queue->watcher);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
574 SDL_AtomicUnlock(&queue->lock);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
575
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
576 /* Wait a bit... */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
577 SDL_Delay(1);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
578 }
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
579 return 0;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
580 }
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
581 #endif /* TEST_SPINLOCK_FIFO */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
582
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
583 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
584 {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
585 SDL_EventQueue queue;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
586 WriterData writerData[NUM_WRITERS];
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
587 ReaderData readerData[NUM_READERS];
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
588 Uint32 start, end;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
589 int i, j;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
590 int grand_total;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
591
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
592 printf("\nFIFO test---------------------------------------\n\n");
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
593 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
594
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
595 readersDone = SDL_CreateSemaphore(0);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
596 writersDone = SDL_CreateSemaphore(0);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
597
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
598 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
599
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
600 InitEventQueue(&queue);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
601 if (!lock_free) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
602 queue.mutex = SDL_CreateMutex();
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
603 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
604
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
605 start = SDL_GetTicks();
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
606
5101
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
607 #ifdef TEST_SPINLOCK_FIFO
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
608 /* Start a monitoring thread */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
609 if (lock_free) {
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
610 SDL_CreateThread(FIFO_Watcher, &queue);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
611 }
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
612 #endif
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5100
diff changeset
613
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
614 /* Start the readers first */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
615 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
616 SDL_zero(readerData);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
617 SDL_AtomicSet(&readersRunning, NUM_READERS);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
618 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
619 readerData[i].queue = &queue;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
620 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
621 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
622 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
623
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
624 /* Start up the writers */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
625 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
626 SDL_zero(writerData);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
627 SDL_AtomicSet(&writersRunning, NUM_WRITERS);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
628 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
629 writerData[i].queue = &queue;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
630 writerData[i].index = i;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
631 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
632 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
633 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
634
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
635 /* Wait for the writers */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
636 while (SDL_AtomicGet(&writersRunning) > 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
637 SDL_SemWait(writersDone);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
638 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
639
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
640 /* 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
641 queue.active = SDL_FALSE;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
642
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
643 /* Wait for the readers */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
644 while (SDL_AtomicGet(&readersRunning) > 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
645 SDL_SemWait(readersDone);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
646 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
647
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
648 end = SDL_GetTicks();
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
649
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
650 SDL_DestroySemaphore(readersDone);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
651 SDL_DestroySemaphore(writersDone);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
652
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
653 if (!lock_free) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
654 SDL_DestroyMutex(queue.mutex);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
655 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
656
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
657 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
658
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
659 printf("\n");
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
660 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
661 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
662 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
663 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
664
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
665 /* 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
666 printf("\n");
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
667 grand_total = 0;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
668 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
669 int total = 0;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
670 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
671 total += readerData[i].counters[j];
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
672 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
673 grand_total += total;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
674 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
675 printf(" { ");
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
676 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
677 if (j > 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
678 printf(", ");
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
679 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
680 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
681 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
682 printf(" }\n");
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
683 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
684 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
685 }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
686
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
687 /* End FIFO test */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
688 /**************************************************************************/
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
689
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
690 int
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
691 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
692 {
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
693 RunBasicTest();
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
694 RunEpicTest();
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
695 /* 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
696 #if 0
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
697 RunFIFOTest(SDL_FALSE);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
698 #endif
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
699 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
700 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
701 }
5100
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
702
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
703 /* vi: set ts=4 sw=4 expandtab: */