test: mo-satcycle: add new MO satisfaction cycle example
[model-checker.git] / test / mo-satcycle.cc
1 /**
2  * @file mo-satcycle.cc
3  * @brief MO 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, y;
20 int r0, r1, r2, r3; /* "local" variables */
21
22 static void a(void *obj)
23 {
24         y.store(10, memory_order_relaxed);
25         x.store(1, memory_order_release);
26 }
27
28 static void b(void *obj)
29 {
30         r0 = x.load(memory_order_relaxed);
31         r1 = x.load(memory_order_acquire);
32         y.store(11, memory_order_relaxed);
33 }
34
35 static void c(void *obj)
36 {
37         r2 = y.load(memory_order_relaxed);
38         r3 = y.load(memory_order_relaxed);
39         if (r2 == 11 && r3 == 10)
40                 x.store(0, memory_order_relaxed);
41 }
42
43 int user_main(int argc, char **argv)
44 {
45         thrd_t t1, t2, t3;
46
47         atomic_init(&x, 0);
48         atomic_init(&y, 0);
49
50         printf("Main thread: creating 3 threads\n");
51         thrd_create(&t1, (thrd_start_t)&a, NULL);
52         thrd_create(&t2, (thrd_start_t)&b, NULL);
53         thrd_create(&t3, (thrd_start_t)&c, NULL);
54
55         thrd_join(t1);
56         thrd_join(t2);
57         thrd_join(t3);
58         printf("Main thread is finished\n");
59
60         /*
61          * This condition should not be hit because it only occurs under a
62          * satisfaction cycle
63          */
64         bool cycle = (r0 == 1 && r1 == 0 && r2 == 11 && r3 == 10);
65         MODEL_ASSERT(!cycle);
66
67         return 0;
68 }