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

Latest commit

 

History

History
234 lines (179 loc) · 5.72 KB

testatomic.c

File metadata and controls

234 lines (179 loc) · 5.72 KB
 
Oct 4, 2009
Oct 4, 2009
1
#include <stdio.h>
Jan 17, 2011
Jan 17, 2011
2
3
#include "SDL.h"
Jan 17, 2011
Jan 17, 2011
4
#include "SDL_atomic.h"
Jan 16, 2011
Jan 16, 2011
5
#include "SDL_assert.h"
Jun 24, 2009
Jun 24, 2009
7
/*
Jun 29, 2009
Jun 29, 2009
8
9
Absolutely basic tests just to see if we get the expected value
after calling each function.
Jun 24, 2009
Jun 24, 2009
10
11
*/
Jan 16, 2011
Jan 16, 2011
12
static
Oct 4, 2009
Oct 4, 2009
13
char *
Jun 29, 2009
Jun 29, 2009
14
15
tf(SDL_bool tf)
{
Jan 15, 2011
Jan 15, 2011
16
17
static char *t = "TRUE";
static char *f = "FALSE";
Jun 29, 2009
Jun 29, 2009
18
Jan 15, 2011
Jan 15, 2011
19
20
21
22
if (tf)
{
return t;
}
Jun 29, 2009
Jun 29, 2009
23
Jan 15, 2011
Jan 15, 2011
24
return f;
Jun 29, 2009
Jun 29, 2009
25
}
Oct 4, 2009
Oct 4, 2009
26
Jan 16, 2011
Jan 16, 2011
27
28
static
void RunBasicTest()
Jan 15, 2011
Jan 15, 2011
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
int value;
SDL_SpinLock lock = 0;
SDL_bool tfret = SDL_FALSE;
printf("\nspin lock---------------------------------------\n\n");
SDL_AtomicLock(&lock);
printf("AtomicLock lock=%d\n", lock);
SDL_AtomicUnlock(&lock);
printf("AtomicUnlock lock=%d\n", lock);
printf("\natomic -----------------------------------------\n\n");
SDL_atomic_t v;
SDL_AtomicSet(&v, 0);
tfret = SDL_AtomicSet(&v, 10) == 0;
printf("AtomicSet(10) tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
tfret = SDL_AtomicAdd(&v, 10) == 10;
printf("AtomicAdd(10) tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
SDL_AtomicSet(&v, 0);
SDL_AtomicIncRef(&v);
tfret = (SDL_AtomicGet(&v) == 1);
printf("AtomicIncRef() tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
SDL_AtomicIncRef(&v);
tfret = (SDL_AtomicGet(&v) == 2);
printf("AtomicIncRef() tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
tfret = (SDL_AtomicDecRef(&v) == SDL_FALSE);
printf("AtomicDecRef() tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
tfret = (SDL_AtomicDecRef(&v) == SDL_TRUE);
printf("AtomicDecRef() tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
SDL_AtomicSet(&v, 10);
Jan 16, 2011
Jan 16, 2011
65
tfret = (SDL_AtomicCAS(&v, 0, 20) == SDL_FALSE);
Jan 15, 2011
Jan 15, 2011
66
67
printf("AtomicCAS() tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
value = SDL_AtomicGet(&v);
Jan 16, 2011
Jan 16, 2011
68
tfret = (SDL_AtomicCAS(&v, value, 20) == SDL_TRUE);
Jan 15, 2011
Jan 15, 2011
69
printf("AtomicCAS() tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
Jan 16, 2011
Jan 16, 2011
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
}
/* Atomic operation test, adapted from code by Michael Davidsaver at:
http://bazaar.launchpad.net/~mdavidsaver/epics-base/atomic/revision/12105#src/libCom/test/epicsAtomicTest.c
*/
/* Tests semantics of atomic operations. Also a stress test
* to see if they are really atomic.
*
* Serveral threads adding to the same variable.
* at the end the value is compared with the expected
* and with a non-atomic counter.
*/
/* Number of concurrent incrementers */
#define NThreads 2
#define CountInc 100
#define VALBITS (sizeof(atomicValue)*8)
#define atomicValue int
#define CountTo ((atomicValue)((unsigned int)(1<<(VALBITS-1))-1))
#define NInter (CountTo/CountInc/NThreads)
#define Expect (CountTo-NInter*CountInc*NThreads)
SDL_COMPILE_TIME_ASSERT(size, CountTo>0); /* check for rollover */
static SDL_atomic_t good = { 42 };
static atomicValue bad = 42;
static SDL_atomic_t threadsRunning;
static SDL_sem *threadDone;
static
int adder(void* junk)
{
unsigned long N=NInter;
printf("Thread subtracting %d %lu times\n",CountInc,N);
while (N--) {
SDL_AtomicAdd(&good, -CountInc);
bad-=CountInc;
}
SDL_AtomicAdd(&threadsRunning, -1);
SDL_SemPost(threadDone);
return 0;
}
static
void runAdder(void)
{
Uint32 start, end;
int T=NThreads;
start = SDL_GetTicks();
threadDone = SDL_CreateSemaphore(0);
SDL_AtomicSet(&threadsRunning, NThreads);
while (T--)
SDL_CreateThread(adder, NULL);
while (SDL_AtomicGet(&threadsRunning) > 0)
SDL_SemWait(threadDone);
SDL_DestroySemaphore(threadDone);
end = SDL_GetTicks();
printf("Finished in %f sec\n", (end - start) / 1000.f);
}
static
void RunEpicTest()
{
int b;
atomicValue v;
printf("\nepic test---------------------------------------\n\n");
Jan 15, 2011
Jan 15, 2011
150
Jan 16, 2011
Jan 16, 2011
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
printf("Size asserted to be >= 32-bit\n");
SDL_assert(sizeof(atomicValue)>=4);
printf("Check static initializer\n");
v=SDL_AtomicGet(&good);
SDL_assert(v==42);
SDL_assert(bad==42);
printf("Test negative values\n");
SDL_AtomicSet(&good, -5);
v=SDL_AtomicGet(&good);
SDL_assert(v==-5);
printf("Verify maximum value\n");
SDL_AtomicSet(&good, CountTo);
v=SDL_AtomicGet(&good);
SDL_assert(v==CountTo);
printf("Test compare and exchange\n");
b=SDL_AtomicCAS(&good, 500, 43);
SDL_assert(!b); /* no swap since CountTo!=500 */
v=SDL_AtomicGet(&good);
SDL_assert(v==CountTo); /* ensure no swap */
b=SDL_AtomicCAS(&good, CountTo, 44);
SDL_assert(!!b); /* will swap */
v=SDL_AtomicGet(&good);
SDL_assert(v==44);
printf("Test Add\n");
v=SDL_AtomicAdd(&good, 1);
SDL_assert(v==44);
v=SDL_AtomicGet(&good);
SDL_assert(v==45);
v=SDL_AtomicAdd(&good, 10);
SDL_assert(v==45);
v=SDL_AtomicGet(&good);
SDL_assert(v==55);
printf("Test Add (Negative values)\n");
v=SDL_AtomicAdd(&good, -20);
SDL_assert(v==55);
v=SDL_AtomicGet(&good);
SDL_assert(v==35);
v=SDL_AtomicAdd(&good, -50); /* crossing zero down */
SDL_assert(v==35);
v=SDL_AtomicGet(&good);
SDL_assert(v==-15);
v=SDL_AtomicAdd(&good, 30); /* crossing zero up */
SDL_assert(v==-15);
v=SDL_AtomicGet(&good);
SDL_assert(v==15);
printf("Reset before count down test\n");
SDL_AtomicSet(&good, CountTo);
v=SDL_AtomicGet(&good);
SDL_assert(v==CountTo);
bad=CountTo;
SDL_assert(bad==CountTo);
printf("Counting down from %d, Expect %d remaining\n",CountTo,Expect);
runAdder();
v=SDL_AtomicGet(&good);
printf("Atomic %d Non-Atomic %d\n",v,bad);
SDL_assert(v==Expect);
SDL_assert(bad!=Expect);
}
int
main(int argc, char *argv[])
{
RunBasicTest();
RunEpicTest();
Jan 15, 2011
Jan 15, 2011
233
234
return 0;
}