Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc / support / regression / tcc / 124_atomic_counter.c
bloba817fc2681c14931722ce1bd71a1cc06783f6166
1 #include <stddef.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <pthread.h>
6 #include <stdatomic.h>
8 #define NR_THREADS 16
9 #define NR_STEPS ((uint32_t)UINT16_MAX * 4)
11 #define BUG_ON(COND) \
12 do { \
13 if (!!(COND)) \
14 abort(); \
15 } while (0)
17 static
18 void *adder_simple(void *arg)
20 size_t step;
21 atomic_size_t *counter = arg;
23 for (step = 0; step < NR_STEPS; ++step)
24 atomic_fetch_add_explicit(counter, 1, memory_order_relaxed);
26 return NULL;
29 static
30 void *adder_cmpxchg(void *arg)
32 size_t step;
33 atomic_size_t *counter = arg;
35 for (step = 0; step < NR_STEPS; ++step) {
36 size_t xchg;
37 size_t cmp = atomic_load_explicit(counter, memory_order_relaxed);
39 do {
40 xchg = (cmp + 1);
41 } while (!atomic_compare_exchange_strong_explicit(counter,
42 &cmp, xchg, memory_order_relaxed, memory_order_relaxed));
45 return NULL;
48 static
49 void atomic_counter_test(void *(*adder)(void *arg))
51 size_t index;
52 atomic_size_t counter;
53 pthread_t thread[NR_THREADS];
55 atomic_init(&counter, 0);
57 for (index = 0; index < NR_THREADS; ++index)
58 BUG_ON(pthread_create(&thread[index], NULL, adder, (void *)&counter));
60 for (index = 0; index < NR_THREADS; ++index)
61 BUG_ON(pthread_join(thread[index], NULL));
63 if (atomic_load(&counter) == (NR_THREADS * NR_STEPS))
64 printf("SUCCESS\n");
65 else
66 printf("FAILURE\n");
69 int main(void)
71 atomic_counter_test(adder_simple);
72 atomic_counter_test(adder_cmpxchg);
74 return 0;