README.md: add linux locks example
[model-checker.git] / test / fences2.c
1 #include <stdio.h>
2 #include <threads.h>
3 #include <stdatomic.h>
4
5 #include "librace.h"
6 #include "model-assert.h"
7
8 atomic_int x;
9 atomic_int y;
10
11 static void a(void *obj)
12 {
13         atomic_store_explicit(&x, 1, memory_order_relaxed);
14         atomic_thread_fence(memory_order_release);
15         atomic_store_explicit(&x, 2, memory_order_relaxed);
16 }
17
18 static void b(void *obj)
19 {
20         int r1, r2;
21         r1 = atomic_load_explicit(&x, memory_order_relaxed);
22         atomic_thread_fence(memory_order_acquire);
23         r2 = atomic_load_explicit(&x, memory_order_relaxed);
24
25         printf("FENCES: r1 = %d, r2 = %d\n", r1, r2);
26         if (r1 == 2)
27                 MODEL_ASSERT(r2 != 1);
28 }
29
30 int user_main(int argc, char **argv)
31 {
32         thrd_t t1, t2;
33
34         atomic_init(&x, 0);
35         atomic_init(&y, 0);
36
37         printf("Main thread: creating 2 threads\n");
38         thrd_create(&t1, (thrd_start_t)&a, NULL);
39         thrd_create(&t2, (thrd_start_t)&b, NULL);
40
41         thrd_join(t1);
42         thrd_join(t2);
43         printf("Main thread is finishing\n");
44
45         return 0;
46 }