Bug fix for removing ATOMIC_WAIT actions
[c11tester.git] / pthread_test / iriw.cc
1 /**
2  * @file iriw.cc
3  * @brief Independent read and independent write test
4  */
5
6 #include <atomic>
7 #include <pthread.h>
8 #include <stdio.h>
9
10 #include "wildcard.h"
11 #include "model-assert.h"
12
13 using namespace std;
14
15 atomic_int x, y;
16 int r1, r2, r3, r4; /* "local" variables */
17
18 static void *a(void *obj)
19 {
20         x.store(1, memory_order_seq_cst);
21         return new int(1);
22 }
23
24 static void *b(void *obj)
25 {
26         y.store(1, memory_order_seq_cst);
27         return new int(2);
28 }
29
30 static void *c(void *obj)
31 {
32         r1 = x.load(memory_order_acquire);
33         r2 = y.load(memory_order_seq_cst);
34         return new int(3);
35 }
36
37 static void *d(void *obj)
38 {
39         r3 = y.load(memory_order_acquire);
40         r4 = x.load(memory_order_seq_cst);
41         return new int(4);
42 }
43
44
45 int user_main(int argc, char **argv)
46 {
47         pthread_t t1, t2, t3, t4;
48
49         atomic_init(&x, 0);
50         atomic_init(&y, 0);
51
52         printf("Main thread: creating 4 threads\n");
53         pthread_create(&t1,NULL, &a, NULL);
54         pthread_create(&t2,NULL, &b, NULL);
55         pthread_create(&t3,NULL, &c, NULL);
56         pthread_create(&t4,NULL, &d, NULL);
57
58         pthread_join(t1,NULL);
59         pthread_join(t2,NULL);
60         pthread_join(t3,NULL);
61         pthread_join(t4,NULL);
62         printf("Main thread is finished\n");
63
64         /*
65          * This condition should not be hit if the execution is SC */
66         bool sc = (r1 == 1 && r2 == 0 && r3 == 1 && r4 == 0);
67         printf("r1 = %d, r2 = %d, r3 = %d and r4 = %d\n", r1, r2, r3, r4);
68         MODEL_ASSERT(!sc);
69
70         return 0;
71 }