comparison test/testatomic.c @ 5003:3a95a2b93eb3

Updated the atomic API for better use cases
author Sam Lantinga <slouken@libsdl.org>
date Sat, 15 Jan 2011 12:41:59 -0800
parents 9de326b3099c
children 0c72ae7b7cb2
comparison
equal deleted inserted replaced
5002:c5b9486688ce 5003:3a95a2b93eb3
1 #include <stdio.h> 1 #include <stdio.h>
2 #include "SDL.h" 2 #include "SDL.h"
3
4 /* Make sure we have good macros for printing 32 and 64 bit values */
5 #ifndef PRIu32
6 #define PRIu32 "u"
7 #endif
8 #ifndef PRIu64
9 #ifdef __WIN32__
10 #define PRIu64 "I64u"
11 #else
12 #define PRIu64 "llu"
13 #endif
14 #endif
15 3
16 /* 4 /*
17 Absolutely basic tests just to see if we get the expected value 5 Absolutely basic tests just to see if we get the expected value
18 after calling each function. 6 after calling each function.
19 */ 7 */
20 8
21 char * 9 char *
22 tf(SDL_bool tf) 10 tf(SDL_bool tf)
23 { 11 {
24 static char *t = "true"; 12 static char *t = "TRUE";
25 static char *f = "false"; 13 static char *f = "FALSE";
26 14
27 if (tf) 15 if (tf)
28 { 16 {
29 return t; 17 return t;
30 } 18 }
31 19
32 return f; 20 return f;
33 } 21 }
34 22
35 int 23 int
36 main(int argc, char *argv[]) 24 main(int argc, char *argv[])
37 { 25 {
26 int value;
27 SDL_SpinLock lock = 0;
38 28
39 volatile Uint32 val32 = 0; 29 SDL_bool tfret = SDL_FALSE;
40 Uint32 ret32 = 0;
41 30
42 volatile Uint64 val64 = 0; 31 printf("\nspin lock---------------------------------------\n\n");
43 Uint64 ret64 = 0;
44 32
45 SDL_SpinLock lock = 0; 33 SDL_AtomicLock(&lock);
34 printf("AtomicLock lock=%d\n", lock);
35 SDL_AtomicUnlock(&lock);
36 printf("AtomicUnlock lock=%d\n", lock);
46 37
47 SDL_bool tfret = SDL_FALSE; 38 printf("\natomic -----------------------------------------\n\n");
48 39
49 printf("\nspin lock---------------------------------------\n\n"); 40 SDL_atomic_t v;
41
42 SDL_AtomicSet(&v, 0);
43 tfret = SDL_AtomicSet(&v, 10) == 0;
44 printf("AtomicSet(10) tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
45 tfret = SDL_AtomicAdd(&v, 10) == 10;
46 printf("AtomicAdd(10) tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
50 47
51 SDL_AtomicLock(&lock); 48 SDL_AtomicSet(&v, 0);
52 printf("AtomicLock lock=%d\n", lock); 49 SDL_AtomicIncRef(&v);
53 SDL_AtomicUnlock(&lock); 50 tfret = (SDL_AtomicGet(&v) == 1);
54 printf("AtomicUnlock lock=%d\n", lock); 51 printf("AtomicIncRef() tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
52 SDL_AtomicIncRef(&v);
53 tfret = (SDL_AtomicGet(&v) == 2);
54 printf("AtomicIncRef() tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
55 tfret = (SDL_AtomicDecRef(&v) == SDL_FALSE);
56 printf("AtomicDecRef() tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
57 tfret = (SDL_AtomicDecRef(&v) == SDL_TRUE);
58 printf("AtomicDecRef() tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
55 59
56 printf("\n32 bit -----------------------------------------\n\n"); 60 SDL_AtomicSet(&v, 10);
61 tfret = (SDL_AtomicCAS(&v, 0, 20) != 0);
62 printf("AtomicCAS() tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
63 value = SDL_AtomicGet(&v);
64 tfret = (SDL_AtomicCAS(&v, value, 20) == value);
65 printf("AtomicCAS() tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
57 66
58 val32 = 0; 67 return 0;
59 tfret = SDL_AtomicTestThenSet32(&val32); 68 }
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 val64 = 0;
95 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 #endif
127
128 return 0;
129 }