test: insanesync: consolidate lines
[cdsspec-compiler.git] / test / addr-satcycle.cc
1 /**
2  * @file addr-satcycle.cc
3  * @brief Address-based satisfaction cycle test
4  *
5  * This program has a peculiar behavior which is technically legal under the
6  * current C++ memory model but which is a result of a type of satisfaction
7  * cycle. We use this as justification for part of our modifications to the
8  * memory model when proving our model-checker's correctness.
9  */
10
11 #include <atomic>
12 #include <threads.h>
13 #include <stdio.h>
14
15 #include "model-assert.h"
16
17 using namespace std;
18
19 atomic_int x[2], idx, y;
20
21 int r1, r2, r3; /* "local" variables */
22
23 static void a(void *obj)
24 {
25         r1 = idx.load(memory_order_relaxed);
26         x[r1].store(0, memory_order_relaxed);
27
28         /* Key point: can we guarantee that &x[0] == &x[r1]? */
29         r2 = x[0].load(memory_order_relaxed);
30         y.store(r2);
31 }
32
33 static void b(void *obj)
34 {
35         r3 = y.load(memory_order_relaxed);
36         idx.store(r3, memory_order_relaxed);
37 }
38
39 int user_main(int argc, char **argv)
40 {
41         thrd_t t1, t2;
42
43         atomic_init(&x[0], 1);
44         atomic_init(&idx, 0);
45         atomic_init(&y, 0);
46
47         printf("Main thread: creating 2 threads\n");
48         thrd_create(&t1, (thrd_start_t)&a, NULL);
49         thrd_create(&t2, (thrd_start_t)&b, NULL);
50
51         thrd_join(t1);
52         thrd_join(t2);
53         printf("Main thread is finished\n");
54
55         printf("r1 = %d\n", r1);
56         printf("r2 = %d\n", r2);
57         printf("r3 = %d\n", r3);
58
59         /*
60          * This condition should not be hit because it only occurs under a
61          * satisfaction cycle
62          */
63         bool cycle = (r1 == 1 && r2 == 1 && r3 == 1);
64         MODEL_ASSERT(!cycle);
65
66         return 0;
67 }