This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
testatomic.c
129 lines (94 loc) · 3.82 KB
1
#include <stdio.h>
2
3
#include "SDL.h"
4
5
6
7
8
9
10
11
12
13
14
15
/* Make sure we have good macros for printing 32 and 64 bit values */
#ifndef PRIu32
#define PRIu32 "u"
#endif
#ifndef PRIu64
#ifdef __WIN32__
#define PRIu64 "I64u"
#else
#define PRIu64 "llu"
#endif
#endif
16
/*
17
18
Absolutely basic tests just to see if we get the expected value
after calling each function.
19
20
*/
21
char *
22
23
24
25
26
27
28
29
30
31
32
33
tf(SDL_bool tf)
{
static char *t = "true";
static char *f = "false";
if (tf)
{
return t;
}
return f;
}
34
35
int
36
main(int argc, char *argv[])
37
{
38
39
volatile Uint32 val32 = 0;
40
41
Uint32 ret32 = 0;
42
volatile Uint64 val64 = 0;
43
44
Uint64 ret64 = 0;
45
46
SDL_SpinLock lock = 0;
47
48
SDL_bool tfret = SDL_FALSE;
49
printf("\nspin lock---------------------------------------\n\n");
50
51
52
53
54
SDL_AtomicLock(&lock);
printf("AtomicLock lock=%d\n", lock);
SDL_AtomicUnlock(&lock);
printf("AtomicUnlock lock=%d\n", lock);
55
56
printf("\n32 bit -----------------------------------------\n\n");
57
58
59
val32 = 0;
tfret = SDL_AtomicTestThenSet32(&val32);
60
printf("TestThenSet32 tfret=%s val=%"PRIu32"\n", tf(tfret), val32);
61
tfret = SDL_AtomicTestThenSet32(&val32);
62
printf("TestThenSet32 tfret=%s val=%"PRIu32"\n", tf(tfret), val32);
63
64
SDL_AtomicClear32(&val32);
65
printf("Clear32 val=%"PRIu32"\n", val32);
66
67
ret32 = SDL_AtomicFetchThenIncrement32(&val32);
68
printf("FetchThenIncrement32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
69
70
ret32 = SDL_AtomicFetchThenDecrement32(&val32);
71
printf("FetchThenDecrement32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
72
73
ret32 = SDL_AtomicFetchThenAdd32(&val32, 10);
74
printf("FetchThenAdd32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
75
76
ret32 = SDL_AtomicFetchThenSubtract32(&val32, 10);
77
printf("FetchThenSubtract32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
78
79
ret32 = SDL_AtomicIncrementThenFetch32(&val32);
80
printf("IncrementThenFetch32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
81
82
ret32 = SDL_AtomicDecrementThenFetch32(&val32);
83
printf("DecrementThenFetch32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
84
85
ret32 = SDL_AtomicAddThenFetch32(&val32, 10);
86
printf("AddThenFetch32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
87
88
ret32 = SDL_AtomicSubtractThenFetch32(&val32, 10);
89
printf("SubtractThenFetch32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
90
91
#ifdef SDL_HAS_64BIT_TYPE
92
printf("\n64 bit -----------------------------------------\n\n");
93
94
95
val64 = 0;
tfret = SDL_AtomicTestThenSet64(&val64);
96
printf("TestThenSet64 tfret=%s val=%"PRIu64"\n", tf(tfret), val64);
97
tfret = SDL_AtomicTestThenSet64(&val64);
98
printf("TestThenSet64 tfret=%s val=%"PRIu64"\n", tf(tfret), val64);
99
100
SDL_AtomicClear64(&val64);
101
printf("Clear64 val=%"PRIu64"\n", val64);
102
103
ret64 = SDL_AtomicFetchThenIncrement64(&val64);
104
printf("FetchThenIncrement64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
105
106
ret64 = SDL_AtomicFetchThenDecrement64(&val64);
107
printf("FetchThenDecrement64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
108
109
ret64 = SDL_AtomicFetchThenAdd64(&val64, 10);
110
printf("FetchThenAdd64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
111
112
ret64 = SDL_AtomicFetchThenSubtract64(&val64, 10);
113
printf("FetchThenSubtract64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
114
115
ret64 = SDL_AtomicIncrementThenFetch64(&val64);
116
printf("IncrementThenFetch64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
117
118
ret64 = SDL_AtomicDecrementThenFetch64(&val64);
119
printf("DecrementThenFetch64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
120
121
ret64 = SDL_AtomicAddThenFetch64(&val64, 10);
122
printf("AddThenFetch64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
123
124
ret64 = SDL_AtomicSubtractThenFetch64(&val64, 10);
125
printf("SubtractThenFetch64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
126
127
128
129
#endif
return 0;
}