litmus: wrc: add macro for memory ordering
[c11tester.git] / test / litmus / wrc.cc
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <threads.h>
4 #include <atomic>
5
6 static int N = 2;
7
8 /* Can be tested for different behavior with relaxed vs. release/acquire/seq-cst */
9 #define load_mo std::memory_order_relaxed
10 #define store_mo std::memory_order_relaxed
11
12 static std::atomic_int *x;
13
14 static void a(void *obj)
15 {
16         int idx = *((int *)obj);
17
18         if (idx > 0)
19                 x[idx - 1].load(load_mo);
20
21         if (idx < N)
22                 x[idx].store(1, store_mo);
23         else
24                 x[0].load(load_mo);
25 }
26
27 int user_main(int argc, char **argv)
28 {
29         thrd_t *threads;
30         int *indexes;
31
32         if (argc > 1)
33                 N = atoi(argv[1]);
34         if (N < 2) {
35                 printf("Error: must have N >= 2\n");
36                 return 1;
37         }
38         printf("N: %d\n", N);
39
40         threads = (thrd_t *)malloc((N + 1) * sizeof(thrd_t));
41         x = (std::atomic_int *)malloc(N * sizeof(std::atomic_int));
42         indexes = (int *)malloc((N + 1) * sizeof(int));
43         
44         for (int i = 0; i < N + 1; i++)
45                 indexes[i] = i;
46
47         for (int i = 0; i < N; i++)
48                 atomic_init(&x[i], 0);
49
50         for (int i = 0; i < N + 1; i++)
51                 thrd_create(&threads[i], (thrd_start_t)&a, (void *)&indexes[i]);
52
53         for (int i = 0; i < N + 1; i++)
54                 thrd_join(threads[i]);
55
56         return 0;
57 }