|
16 | 16 |
|
17 | 17 | #define T uint64_t |
18 | 18 |
|
19 | | -void *thread_fetch_and_add(void *arg) |
20 | | -{ |
21 | | - for(int i = 0; i < 10000; ++i) |
22 | | - __sync_fetch_and_add((T*)arg, 0x0000000100000001ULL); |
23 | | - pthread_exit(0); |
| 19 | +void *thread_fetch_and_add(void *arg) { |
| 20 | + for (int i = 0; i < 10000; ++i) { |
| 21 | + __sync_fetch_and_add((T*)arg, 0x0000000100000001ULL); |
| 22 | + } |
| 23 | + pthread_exit(0); |
24 | 24 | } |
25 | 25 |
|
26 | | -void *thread_fetch_and_sub(void *arg) |
27 | | -{ |
28 | | - for(int i = 0; i < 10000; ++i) |
29 | | - __sync_fetch_and_sub((T*)arg, 0x0000000100000001ULL); |
30 | | - pthread_exit(0); |
| 26 | +void *thread_fetch_and_sub(void *arg) { |
| 27 | + for (int i = 0; i < 10000; ++i) { |
| 28 | + __sync_fetch_and_sub((T*)arg, 0x0000000100000001ULL); |
| 29 | + } |
| 30 | + pthread_exit(0); |
31 | 31 | } |
| 32 | + |
32 | 33 | volatile T fetch_and_or_data = 0; |
33 | | -void *thread_fetch_and_or(void *arg) |
34 | | -{ |
35 | | - for(int i = 0; i < 10000; ++i) |
36 | | - __sync_fetch_and_or((T*)&fetch_and_or_data, *(T*)arg); |
37 | | - pthread_exit(0); |
| 34 | +void *thread_fetch_and_or(void *arg) { |
| 35 | + for (int i = 0; i < 10000; ++i) { |
| 36 | + __sync_fetch_and_or((T*)&fetch_and_or_data, *(T*)arg); |
| 37 | + } |
| 38 | + pthread_exit(0); |
38 | 39 | } |
39 | 40 |
|
40 | 41 | volatile T fetch_and_and_data = 0; |
41 | | -void *thread_fetch_and_and(void *arg) |
42 | | -{ |
43 | | - for(int i = 0; i < 10000; ++i) |
44 | | - __sync_fetch_and_and((T*)&fetch_and_and_data, *(T*)arg); |
45 | | - pthread_exit(0); |
| 42 | +void *thread_fetch_and_and(void *arg) { |
| 43 | + for (int i = 0; i < 10000; ++i) { |
| 44 | + __sync_fetch_and_and((T*)&fetch_and_and_data, *(T*)arg); |
| 45 | + } |
| 46 | + pthread_exit(0); |
46 | 47 | } |
47 | 48 |
|
48 | 49 | volatile T fetch_and_xor_data = 0; |
49 | | -void *thread_fetch_and_xor(void *arg) |
50 | | -{ |
51 | | - for(int i = 0; i < 9999; ++i) // Odd number of times so that the operation doesn't cancel itself out. |
52 | | - __sync_fetch_and_xor((T*)&fetch_and_xor_data, *(T*)arg); |
53 | | - pthread_exit(0); |
| 50 | +void *thread_fetch_and_xor(void *arg) { |
| 51 | + for (int i = 0; i < 9999; ++i) { // Odd number of times so that the operation doesn't cancel itself out. |
| 52 | + __sync_fetch_and_xor((T*)&fetch_and_xor_data, *(T*)arg); |
| 53 | + } |
| 54 | + pthread_exit(0); |
54 | 55 | } |
55 | 56 |
|
56 | 57 | volatile long fetch_and_nand_data = 0; |
57 | | -void *thread_fetch_and_nand(void *arg) |
58 | | -{ |
59 | | - for(int i = 0; i < 9999; ++i) // Odd number of times so that the operation doesn't cancel itself out. |
60 | | - __sync_fetch_and_nand((long*)&fetch_and_nand_data, (long)arg); |
61 | | - pthread_exit(0); |
| 58 | +void *thread_fetch_and_nand(void *arg) { |
| 59 | + for (int i = 0; i < 9999; ++i) { // Odd number of times so that the operation doesn't cancel itself out. |
| 60 | + __sync_fetch_and_nand((long*)&fetch_and_nand_data, (long)arg); |
| 61 | + } |
| 62 | + pthread_exit(0); |
62 | 63 | } |
63 | 64 |
|
64 | | -T threadArg[NUM_THREADS]; |
65 | | -pthread_t thread[NUM_THREADS]; |
66 | | - |
67 | 65 | #define HILO(hi, lo) ((((uint64_t)(hi)) << 32) | ((uint64_t)(lo))) |
68 | 66 | #define DUP(x) HILO((x), (x)) |
69 | 67 |
|
70 | | -int main() |
71 | | -{ |
72 | | - { |
73 | | - T x = HILO(5, 3); |
74 | | - T y = __sync_fetch_and_add(&x, DUP(1)); |
75 | | - assert(y == HILO(5, 3)); |
76 | | - assert(x == HILO(6, 4)); |
77 | | - volatile T n = HILO(2, 1); |
78 | | - if (emscripten_has_threading_support()) |
79 | | - { |
80 | | - for(int i = 0; i < NUM_THREADS; ++i) pthread_create(&thread[i], NULL, thread_fetch_and_add, (void*)&n); |
81 | | - for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL); |
82 | | - printf("n: %llx\n", n); |
83 | | - assert(n == HILO(NUM_THREADS*10000ULL+2ULL, NUM_THREADS*10000ULL+1ULL)); |
84 | | - } |
85 | | - } |
86 | | - { |
87 | | - T x = HILO(15, 13); |
88 | | - T y = __sync_fetch_and_sub(&x, HILO(10, 10)); |
89 | | - assert(y == HILO(15, 13)); |
90 | | - assert(x == HILO(5, 3)); |
91 | | - volatile T n = HILO(NUM_THREADS*10000ULL+5ULL, NUM_THREADS*10000ULL+3ULL); |
92 | | - if (emscripten_has_threading_support()) |
93 | | - { |
94 | | - for(int i = 0; i < NUM_THREADS; ++i) pthread_create(&thread[i], NULL, thread_fetch_and_sub, (void*)&n); |
95 | | - for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL); |
96 | | - printf("n: %llx\n", n); |
97 | | - assert(n == HILO(5,3)); |
98 | | - } |
99 | | - } |
100 | | - { |
101 | | - T x = HILO(32768 + 5, 5); |
102 | | - T y = __sync_fetch_and_or(&x, HILO(65536 + 9, 9)); |
103 | | - assert(y == HILO(32768 + 5, 5)); |
104 | | - assert(x == HILO(32768 + 65536 + 13, 13)); |
105 | | - if (emscripten_has_threading_support()) |
106 | | - { |
107 | | - for(int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived. |
108 | | - { |
109 | | - fetch_and_or_data = HILO(65536 + (1<<NUM_THREADS), 1<<NUM_THREADS); |
110 | | - for(int i = 0; i < NUM_THREADS; ++i) |
111 | | - { |
112 | | - threadArg[i] = DUP(1 << i); |
113 | | - pthread_create(&thread[i], NULL, thread_fetch_and_or, &threadArg[i]); |
114 | | - } |
115 | | - for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL); |
116 | | - assert(fetch_and_or_data == HILO(65536 + (1<<(NUM_THREADS+1))-1, (1<<(NUM_THREADS+1))-1)); |
117 | | - } |
118 | | - } |
119 | | - } |
120 | | - { |
121 | | - T x = HILO(32768 + 5, 5); |
122 | | - T y = __sync_fetch_and_and(&x, HILO(32768 + 9, 9)); |
123 | | - assert(y == HILO(32768 + 5, 5)); |
124 | | - assert(x == HILO(32768 + 1, 1)); |
125 | | - if (emscripten_has_threading_support()) |
126 | | - { |
127 | | - for(int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived. |
128 | | - { |
129 | | - fetch_and_and_data = HILO(65536 + (1<<(NUM_THREADS+1))-1, (1<<(NUM_THREADS+1))-1); |
130 | | - for(int i = 0; i < NUM_THREADS; ++i) |
131 | | - { |
132 | | - threadArg[i] = DUP(~(1u<<i)); |
133 | | - pthread_create(&thread[i], NULL, thread_fetch_and_and, (void*)&threadArg[i]); |
134 | | - } |
135 | | - for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL); |
136 | | - assert(fetch_and_and_data == HILO(65536 + (1<<NUM_THREADS), 1<<NUM_THREADS)); |
137 | | - } |
138 | | - } |
139 | | - } |
140 | | - { |
141 | | - T x = HILO(32768 + 5, 5); |
142 | | - T y = __sync_fetch_and_xor(&x, HILO(16384 + 9, 9)); |
143 | | - assert(y == HILO(32768 + 5, 5)); |
144 | | - assert(x == HILO(32768 + 16384 + 12, 12)); |
145 | | - if (emscripten_has_threading_support()) |
146 | | - { |
147 | | - for(int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived. |
148 | | - { |
149 | | - fetch_and_xor_data = HILO(32768 + (1<<NUM_THREADS), 1<<NUM_THREADS); |
150 | | - for(int i = 0; i < NUM_THREADS; ++i) |
151 | | - { |
152 | | - threadArg[i] = DUP(~(1u<<i)); |
153 | | - pthread_create(&thread[i], NULL, thread_fetch_and_xor, (void*)&threadArg[i]); |
154 | | - } |
155 | | - for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL); |
156 | | - assert(fetch_and_xor_data == HILO(32768 + ((1<<(NUM_THREADS+1))-1), (1<<(NUM_THREADS+1))-1)); |
157 | | - } |
158 | | - } |
159 | | - } |
160 | | - { |
161 | | - T x = 5; |
162 | | - T y = __sync_fetch_and_nand(&x, 9); |
163 | | - assert(y == 5); |
164 | | - assert(x == -2); |
165 | | - const int oddNThreads = NUM_THREADS-1; |
166 | | - for(int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived. |
167 | | - { |
168 | | - fetch_and_nand_data = 0; |
169 | | - for(int i = 0; i < oddNThreads; ++i) pthread_create(&thread[i], NULL, thread_fetch_and_nand, (void*)-1); |
170 | | - for(int i = 0; i < oddNThreads; ++i) pthread_join(thread[i], NULL); |
171 | | - assert(fetch_and_nand_data == -1); |
172 | | - } |
173 | | - } |
| 68 | +int main() { |
| 69 | + T threadArg[NUM_THREADS]; |
| 70 | + pthread_t thread[NUM_THREADS]; |
| 71 | + { |
| 72 | + printf("__sync_fetch_and_add ..\n"); |
| 73 | + T x = HILO(5, 3); |
| 74 | + T y = __sync_fetch_and_add(&x, DUP(1)); |
| 75 | + assert(y == HILO(5, 3)); |
| 76 | + assert(x == HILO(6, 4)); |
| 77 | + volatile T n = HILO(2, 1); |
| 78 | + if (emscripten_has_threading_support()) { |
| 79 | + for (int i = 0; i < NUM_THREADS; ++i) pthread_create(&thread[i], NULL, thread_fetch_and_add, (void*)&n); |
| 80 | + for (int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL); |
| 81 | + printf("n: %llx\n", n); |
| 82 | + assert(n == HILO(NUM_THREADS*10000ULL+2ULL, NUM_THREADS*10000ULL+1ULL)); |
| 83 | + } |
| 84 | + } |
| 85 | + { |
| 86 | + printf("__sync_fetch_and_sub ..\n"); |
| 87 | + T x = HILO(15, 13); |
| 88 | + T y = __sync_fetch_and_sub(&x, HILO(10, 10)); |
| 89 | + assert(y == HILO(15, 13)); |
| 90 | + assert(x == HILO(5, 3)); |
| 91 | + volatile T n = HILO(NUM_THREADS*10000ULL+5ULL, NUM_THREADS*10000ULL+3ULL); |
| 92 | + if (emscripten_has_threading_support()) { |
| 93 | + for (int i = 0; i < NUM_THREADS; ++i) pthread_create(&thread[i], NULL, thread_fetch_and_sub, (void*)&n); |
| 94 | + for (int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL); |
| 95 | + printf("n: %llx\n", n); |
| 96 | + assert(n == HILO(5,3)); |
| 97 | + } |
| 98 | + } |
| 99 | + { |
| 100 | + printf("__sync_fetch_and_or ..\n"); |
| 101 | + T x = HILO(32768 + 5, 5); |
| 102 | + T y = __sync_fetch_and_or(&x, HILO(65536 + 9, 9)); |
| 103 | + assert(y == HILO(32768 + 5, 5)); |
| 104 | + assert(x == HILO(32768 + 65536 + 13, 13)); |
| 105 | + if (emscripten_has_threading_support()) { |
| 106 | + for (int x = 0; x < 100; ++x) { // Test a few times for robustness, since this test is so short-lived. |
| 107 | + fetch_and_or_data = HILO(65536 + (1<<NUM_THREADS), 1<<NUM_THREADS); |
| 108 | + for (int i = 0; i < NUM_THREADS; ++i) { |
| 109 | + threadArg[i] = DUP(1 << i); |
| 110 | + pthread_create(&thread[i], NULL, thread_fetch_and_or, &threadArg[i]); |
| 111 | + } |
| 112 | + for (int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL); |
| 113 | + assert(fetch_and_or_data == HILO(65536 + (1<<(NUM_THREADS+1))-1, (1<<(NUM_THREADS+1))-1)); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + { |
| 118 | + printf("__sync_fetch_and_and ..\n"); |
| 119 | + T x = HILO(32768 + 5, 5); |
| 120 | + T y = __sync_fetch_and_and(&x, HILO(32768 + 9, 9)); |
| 121 | + assert(y == HILO(32768 + 5, 5)); |
| 122 | + assert(x == HILO(32768 + 1, 1)); |
| 123 | + if (emscripten_has_threading_support()) { |
| 124 | + for (int x = 0; x < 100; ++x) { // Test a few times for robustness, since this test is so short-lived. |
| 125 | + fetch_and_and_data = HILO(65536 + (1<<(NUM_THREADS+1))-1, (1<<(NUM_THREADS+1))-1); |
| 126 | + for (int i = 0; i < NUM_THREADS; ++i) { |
| 127 | + threadArg[i] = DUP(~(1u<<i)); |
| 128 | + pthread_create(&thread[i], NULL, thread_fetch_and_and, (void*)&threadArg[i]); |
| 129 | + } |
| 130 | + for (int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL); |
| 131 | + assert(fetch_and_and_data == HILO(65536 + (1<<NUM_THREADS), 1<<NUM_THREADS)); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + { |
| 136 | + printf("__sync_fetch_and_xor ..\n"); |
| 137 | + T x = HILO(32768 + 5, 5); |
| 138 | + T y = __sync_fetch_and_xor(&x, HILO(16384 + 9, 9)); |
| 139 | + assert(y == HILO(32768 + 5, 5)); |
| 140 | + assert(x == HILO(32768 + 16384 + 12, 12)); |
| 141 | + if (emscripten_has_threading_support()) { |
| 142 | + for (int x = 0; x < 100; ++x) { // Test a few times for robustness, since this test is so short-lived. |
| 143 | + fetch_and_xor_data = HILO(32768 + (1<<NUM_THREADS), 1<<NUM_THREADS); |
| 144 | + for (int i = 0; i < NUM_THREADS; ++i) { |
| 145 | + threadArg[i] = DUP(~(1u<<i)); |
| 146 | + pthread_create(&thread[i], NULL, thread_fetch_and_xor, (void*)&threadArg[i]); |
| 147 | + } |
| 148 | + for (int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL); |
| 149 | + assert(fetch_and_xor_data == HILO(32768 + ((1<<(NUM_THREADS+1))-1), (1<<(NUM_THREADS+1))-1)); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + { |
| 154 | + printf("__sync_fetch_and_nand ..\n"); |
| 155 | + T x = 5; |
| 156 | + T y = __sync_fetch_and_nand(&x, 9); |
| 157 | + assert(y == 5); |
| 158 | + assert(x == -2); |
| 159 | + const int oddNThreads = NUM_THREADS-1; |
| 160 | + for (int x = 0; x < 100; ++x) { // Test a few times for robustness, since this test is so short-lived. |
| 161 | + fetch_and_nand_data = 0; |
| 162 | + for (int i = 0; i < oddNThreads; ++i) pthread_create(&thread[i], NULL, thread_fetch_and_nand, (void*)-1); |
| 163 | + for (int i = 0; i < oddNThreads; ++i) pthread_join(thread[i], NULL); |
| 164 | + assert(fetch_and_nand_data == -1); |
| 165 | + } |
| 166 | + } |
174 | 167 |
|
175 | | - return 0; |
| 168 | + return 0; |
176 | 169 | } |
0 commit comments