prune mod order
[c11tester.git] / pthread_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 <pthread.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         return NULL;
32 }
33
34 static void *b(void *obj)
35 {
36         r3 = y.load(memory_order_relaxed);
37         idx.store(r3, memory_order_relaxed);
38         return NULL;
39 }
40
41 int user_main(int argc, char **argv)
42 {
43         pthread_t t1, t2;
44
45         atomic_init(&x[0], 1);
46         atomic_init(&idx, 0);
47         atomic_init(&y, 0);
48
49         printf("Main thread: creating 2 threads\n");
50         pthread_create(&t1,NULL, &a, NULL);
51         pthread_create(&t2,NULL, &b, NULL);
52
53         pthread_join(t1,NULL);
54         pthread_join(t2,NULL);
55         printf("Main thread is finished\n");
56
57         printf("r1 = %d\n", r1);
58         printf("r2 = %d\n", r2);
59         printf("r3 = %d\n", r3);
60
61         /*
62          * This condition should not be hit because it only occurs under a
63          * satisfaction cycle
64          */
65         bool cycle = (r1 == 1 && r2 == 1 && r3 == 1);
66         MODEL_ASSERT(!cycle);
67
68         return 0;
69 }