litmus: seq-lock: add MODEL_ASSERT() for the important behavior
[c11tester.git] / test / litmus / iriw.cc
1 #include <stdio.h>
2 #include <threads.h>
3 #include <atomic>
4
5 std::atomic_int x;
6 std::atomic_int y;
7
8 std::memory_order store_mo = std::memory_order_release;
9 std::memory_order load_mo = std::memory_order_acquire;
10
11 static void a(void *obj)
12 {
13         x.store(1, store_mo);
14 }
15
16 static void b(void *obj)
17 {
18         y.store(1, store_mo);
19 }
20
21 static void c(void *obj)
22 {
23         printf("x1: %d\n", x.load(load_mo));
24         printf("y1: %d\n", y.load(load_mo));
25 }
26
27 static void d(void *obj)
28 {
29         printf("y2: %d\n", y.load(load_mo));
30         printf("x2: %d\n", x.load(load_mo));
31 }
32
33 int user_main(int argc, char **argv)
34 {
35         thrd_t t1, t2, t3, t4;
36
37         /* Command-line argument 's' enables seq_cst test */
38         if (argc > 1 && *argv[1] == 's')
39                 store_mo = load_mo = std::memory_order_seq_cst;
40
41         atomic_init(&x, 0);
42         atomic_init(&y, 0);
43
44         printf("Main thread: creating 4 threads\n");
45         thrd_create(&t1, (thrd_start_t)&a, NULL);
46         thrd_create(&t2, (thrd_start_t)&b, NULL);
47         thrd_create(&t3, (thrd_start_t)&c, NULL);
48         thrd_create(&t4, (thrd_start_t)&d, NULL);
49
50         thrd_join(t1);
51         thrd_join(t2);
52         thrd_join(t3);
53         thrd_join(t4);
54         printf("Main thread is finished\n");
55
56         return 0;
57 }