Add example from java showing legit satisfaction cycle
[cdsspec-compiler.git] / test / sctest.c
1 #include <stdio.h>
2 #include <threads.h>
3 #include <stdatomic.h>
4
5 #include "librace.h"
6
7 atomic_int x;
8 atomic_int y;
9 atomic_int z;
10
11 static int r1, r2, r3;
12
13 static void a(void *obj)
14 {
15         atomic_store_explicit(&z, 1, memory_order_relaxed);
16 }
17
18 static void b(void *obj)
19 {
20         atomic_store_explicit(&x, 1, memory_order_relaxed);
21         atomic_store_explicit(&y, 1, memory_order_relaxed);
22         r1=atomic_load_explicit(&z, memory_order_relaxed);
23 }
24 static void c(void *obj)
25 {
26         atomic_store_explicit(&z, 2, memory_order_relaxed);
27         atomic_store_explicit(&x, 2, memory_order_relaxed);
28         r2=atomic_load_explicit(&y, memory_order_relaxed);
29 }
30
31 static void d(void *obj)
32 {
33         atomic_store_explicit(&z, 3, memory_order_relaxed);
34         atomic_store_explicit(&y, 2, memory_order_relaxed);
35         r3=atomic_load_explicit(&x, memory_order_relaxed);
36 }
37
38 int user_main(int argc, char **argv)
39 {
40         thrd_t t1, t2,t3, t4;
41
42         atomic_init(&x, 0);
43         atomic_init(&y, 0);
44         atomic_init(&z, 0);
45
46         thrd_create(&t1, (thrd_start_t)&a, NULL);
47         thrd_create(&t2, (thrd_start_t)&b, NULL);
48         thrd_create(&t3, (thrd_start_t)&c, NULL);
49         thrd_create(&t4, (thrd_start_t)&d, NULL);
50
51         thrd_join(t1);
52         thrd_join(t2);
53         thrd_join(t3);
54         thrd_join(t4);
55
56         /* Check and/or print r1, r2, r3? */
57
58         return 0;
59 }