1 // Copyright 2015 The Emscripten Authors.  All rights reserved.
2 // Emscripten is available under two separate licenses, the MIT license and the
3 // University of Illinois/NCSA Open Source License.  Both these licenses can be
4 // found in the LICENSE file.
5 
6 #include <assert.h>
7 #include <stdio.h>
8 #include <pthread.h>
9 #include <emscripten.h>
10 #include <emscripten/threading.h>
11 
12 // This file tests the old GCC built-in atomic operations of the form __sync_fetch_and_op().
13 // See https://gcc.gnu.org/onlinedocs/gcc-4.6.4/gcc/Atomic-Builtins.html
14 
15 #define NUM_THREADS 8
16 
17 #define T uint64_t
18 
19 #if 0
20 // TEMP to make this test pass:
21 // Our Clang backend doesn't define this builtin function, so implement it ourselves.
22 // The current Atomics spec doesn't have the nand atomic op either, so must use a cas loop.
23 // TODO: Move this to Clang backend?
24 T __sync_nand_and_fetch(T *ptr, T x)
25 {
26 	for(;;)
27 	{
28 		T old = emscripten_atomic_load_u32(ptr);
29 		T newVal = ~(old & x);
30 		T old2 = emscripten_atomic_cas_u32(ptr, old, newVal);
31 		if (old2 == old) return old;
32 	}
33 }
34 #endif
35 
thread_add_and_fetch(void * arg)36 void *thread_add_and_fetch(void *arg)
37 {
38 	for(int i = 0; i < 10000; ++i)
39 		__sync_add_and_fetch((T*)arg, 0x0000000100000001ULL);
40 	pthread_exit(0);
41 }
42 
thread_sub_and_fetch(void * arg)43 void *thread_sub_and_fetch(void *arg)
44 {
45 	for(int i = 0; i < 10000; ++i)
46 		__sync_sub_and_fetch((T*)arg, 0x0000000100000001ULL);
47 	pthread_exit(0);
48 }
49 volatile T or_and_fetch_data = 0;
thread_or_and_fetch(void * arg)50 void *thread_or_and_fetch(void *arg)
51 {
52 	for(int i = 0; i < 10000; ++i)
53 		__sync_or_and_fetch((T*)&or_and_fetch_data, *(T*)arg);
54 	pthread_exit(0);
55 }
56 
57 volatile T and_and_fetch_data = 0;
thread_and_and_fetch(void * arg)58 void *thread_and_and_fetch(void *arg)
59 {
60 	for(int i = 0; i < 10000; ++i)
61 		__sync_and_and_fetch((T*)&and_and_fetch_data, *(T*)arg);
62 	pthread_exit(0);
63 }
64 
65 volatile T xor_and_fetch_data = 0;
thread_xor_and_fetch(void * arg)66 void *thread_xor_and_fetch(void *arg)
67 {
68 	for(int i = 0; i < 9999; ++i) // Odd number of times so that the operation doesn't cancel itself out.
69 		__sync_xor_and_fetch((T*)&xor_and_fetch_data, *(T*)arg);
70 	pthread_exit(0);
71 }
72 #if 0
73 
74 // XXX NAND support does not exist in Atomics API.
75 #if 0
76 volatile int nand_and_fetch_data = 0;
77 void *thread_nand_and_fetch(void *arg)
78 {
79 	for(int i = 0; i < 9999; ++i) // Odd number of times so that the operation doesn't cancel itself out.
80 		__sync_nand_and_fetch((int*)&nand_and_fetch_data, (int)arg);
81 	pthread_exit(0);
82 }
83 #endif
84 #endif
85 T threadArg[NUM_THREADS];
86 pthread_t thread[NUM_THREADS];
87 
88 #define HILO(hi, lo) ((((uint64_t)(hi)) << 32) | ((uint64_t)(lo)))
89 #define DUP(x) HILO((x), (x))
90 
main()91 int main()
92 {
93 	{
94 		T x = HILO(5, 3);
95 		T y = __sync_add_and_fetch(&x, DUP(1));
96 		assert(y == HILO(6, 4));
97 		assert(x == HILO(6, 4));
98 		volatile T n = HILO(2, 1);
99 		if (emscripten_has_threading_support())
100 		{
101 			for(int i = 0; i < NUM_THREADS; ++i) pthread_create(&thread[i], NULL, thread_add_and_fetch, (void*)&n);
102 			for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL);
103 			printf("n: %llx\n", n);
104 			assert(n == HILO(NUM_THREADS*10000ULL+2ULL, NUM_THREADS*10000ULL+1ULL));
105 		}
106 	}
107 	{
108 		T x = HILO(15, 13);
109 		T y = __sync_sub_and_fetch(&x, HILO(10, 10));
110 		assert(y == HILO(5, 3));
111 		assert(x == HILO(5, 3));
112 		volatile T n = HILO(NUM_THREADS*10000ULL+5ULL, NUM_THREADS*10000ULL+3ULL);
113 		if (emscripten_has_threading_support())
114 		{
115 			for(int i = 0; i < NUM_THREADS; ++i) pthread_create(&thread[i], NULL, thread_sub_and_fetch, (void*)&n);
116 			for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL);
117 			printf("n: %llx\n", n);
118 			assert(n == HILO(5,3));
119 		}
120 	}
121 	{
122 		T x = HILO(32768 + 5, 5);
123 		T y = __sync_or_and_fetch(&x, HILO(65536 + 9, 9));
124 		assert(y == HILO(32768 + 65536 + 13, 13));
125 		assert(x == HILO(32768 + 65536 + 13, 13));
126 		for(int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived.
127 		{
128 			or_and_fetch_data = HILO(65536 + (1<<NUM_THREADS), 1<<NUM_THREADS);
129 			if (emscripten_has_threading_support())
130 			{
131 				for(int i = 0; i < NUM_THREADS; ++i)
132 				{
133 					threadArg[i] = DUP(1 << i);
134 					pthread_create(&thread[i], NULL, thread_or_and_fetch, (void*)&threadArg[i]);
135 				}
136 				for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL);
137 				assert(or_and_fetch_data == HILO(65536 + (1<<(NUM_THREADS+1))-1, (1<<(NUM_THREADS+1))-1));
138 			}
139 		}
140 	}
141 	{
142 		T x = HILO(32768 + 5, 5);
143 		T y = __sync_and_and_fetch(&x, HILO(32768 + 9, 9));
144 		assert(y == HILO(32768 + 1, 1));
145 		assert(x == HILO(32768 + 1, 1));
146 		if (emscripten_has_threading_support())
147 		{
148 			for(int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived.
149 			{
150 				and_and_fetch_data = HILO(65536 + (1<<(NUM_THREADS+1))-1, (1<<(NUM_THREADS+1))-1);
151 				for(int i = 0; i < NUM_THREADS; ++i)
152 				{
153 					threadArg[i] = DUP(~(1UL<<i));
154 					pthread_create(&thread[i], NULL, thread_and_and_fetch, (void*)&threadArg[i]);
155 				}
156 				for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL);
157 				assert(and_and_fetch_data == HILO(65536 + (1<<NUM_THREADS), 1<<NUM_THREADS));
158 			}
159 		}
160 	}
161 	{
162 		T x = HILO(32768 + 5, 5);
163 		T y = __sync_xor_and_fetch(&x, HILO(16384 + 9, 9));
164 		assert(y == HILO(32768 + 16384 + 12, 12));
165 		assert(x == HILO(32768 + 16384 + 12, 12));
166 		if (emscripten_has_threading_support())
167 		{
168 			for(int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived.
169 			{
170 				xor_and_fetch_data = HILO(32768 + (1<<NUM_THREADS), 1<<NUM_THREADS);
171 				for(int i = 0; i < NUM_THREADS; ++i)
172 				{
173 					threadArg[i] = DUP(~(1UL<<i));
174 					pthread_create(&thread[i], NULL, thread_xor_and_fetch, (void*)&threadArg[i]);
175 				}
176 				for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL);
177 				assert(xor_and_fetch_data == HILO(32768 + ((1<<(NUM_THREADS+1))-1), (1<<(NUM_THREADS+1))-1));
178 			}
179 		}
180 	}
181 // XXX NAND support does not exist in Atomics API.
182 #if 0
183 	{
184 		T x = 5;
185 		T y = __sync_nand_and_fetch(&x, 9);
186 		assert(y == 5);
187 		assert(x == -2);
188 		const int oddNThreads = NUM_THREADS-1;
189 		for(int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived.
190 		{
191 			nand_and_fetch_data = 0;
192 			for(int i = 0; i < oddNThreads; ++i) pthread_create(&thread[i], NULL, thread_nand_and_fetch, (void*)-1);
193 			for(int i = 0; i < oddNThreads; ++i) pthread_join(thread[i], NULL);
194 			assert(nand_and_fetch_data == -1);
195 		}
196 	}
197 #endif
198 
199 #ifdef REPORT_RESULT
200 	REPORT_RESULT(0);
201 #endif
202 }
203