annotate test/testatomic.c @ 5117:427998ff3bcf

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