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