scanalysis: allocate structures as true members of class (not pointers)
[model-checker.git] / test / insanesync.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <threads.h>
4 #include <stdatomic.h>
5
6 #include "librace.h"
7 #include "model-assert.h"
8
9 atomic_int x;
10 atomic_int y;
11 atomic_intptr_t z;
12 atomic_intptr_t z2;
13
14 /** 
15                 This example illustrates a self-satisfying cycle involving
16                 synchronization.  A failed synchronization creates the store that
17                 causes the synchronization to fail. 
18                 
19                 The C++11 memory model nominally allows t1=0, t2=1, t3=5.
20
21                 This example is insane, we don't support that behavior.
22 */
23
24
25 static void a(void *obj)
26 {
27         atomic_store_explicit(&z, (intptr_t) &y, memory_order_relaxed);
28         int t1=atomic_fetch_add_explicit(&y, 1, memory_order_release);
29         atomic_store_explicit(&z, (intptr_t) &x, memory_order_relaxed);
30         int t2=atomic_fetch_add_explicit(&y, 1, memory_order_release);
31         printf("t1=%d, t2=%d\n", t1, t2);
32 }
33
34
35 static void b(void *obj)
36 {
37         int t3=atomic_fetch_add_explicit(&y, 1, memory_order_acquire);
38         void *ptr = (void *)atomic_load_explicit(&z, memory_order_relaxed);
39         atomic_store_explicit(&z2, (intptr_t)ptr, memory_order_relaxed);
40         printf("t3=%d\n", t3);
41 }
42
43 static void c(void *obj)
44 {
45         void * ptr2=(void *) atomic_load_explicit(&z2, memory_order_relaxed);
46         atomic_store_explicit((atomic_int *)ptr2, 5, memory_order_relaxed);
47 }
48
49 int user_main(int argc, char **argv)
50 {
51         thrd_t t1, t2, t3;
52
53         
54         atomic_init(&x, 0);
55         atomic_init(&y, 0);
56         atomic_init(&z, (intptr_t) &x);
57         atomic_init(&z2, (intptr_t) &x);
58
59         thrd_create(&t1, (thrd_start_t)&a, NULL);
60         thrd_create(&t2, (thrd_start_t)&b, NULL);
61         thrd_create(&t3, (thrd_start_t)&c, NULL);
62         
63         thrd_join(t1);
64         thrd_join(t2);
65         thrd_join(t3);
66         
67
68         return 0;
69 }