225eb4b2cbad2f2aadb6c9bd495debbbbf470124
[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 std::atomic_int *x;
9
10 static void a(void *obj)
11 {
12         int idx = *((int *)obj);
13
14         if (idx > 0)
15                 x[idx - 1].load(std::memory_order_relaxed);
16
17         if (idx < N)
18                 x[idx].store(1, std::memory_order_relaxed);
19         else
20                 x[0].load(std::memory_order_relaxed);
21 }
22
23 int user_main(int argc, char **argv)
24 {
25         thrd_t *threads;
26         int *indexes;
27
28         if (argc > 1)
29                 N = atoi(argv[1]);
30         if (N < 2) {
31                 printf("Error: must have N >= 2\n");
32                 return 1;
33         }
34         printf("N: %d\n", N);
35
36         threads = (thrd_t *)malloc((N + 1) * sizeof(thrd_t));
37         x = (std::atomic_int *)malloc(N * sizeof(std::atomic_int));
38         indexes = (int *)malloc((N + 1) * sizeof(int));
39         
40         for (int i = 0; i < N + 1; i++)
41                 indexes[i] = i;
42
43         for (int i = 0; i < N; i++)
44                 atomic_init(&x[i], 0);
45
46         for (int i = 0; i < N + 1; i++)
47                 thrd_create(&threads[i], (thrd_start_t)&a, (void *)&indexes[i]);
48
49         for (int i = 0; i < N + 1; i++)
50                 thrd_join(threads[i]);
51
52         return 0;
53 }