d74dcca642ba74e4ec44564aeb3118de0c396483
[model-checker.git] / test / pending-release.c
1 /*
2  * This test performs some relaxes, release, acquire opeations on a single
3  * atomic variable. It is designed for creating a difficult set of pending
4  * release sequences to resolve at the end of an execution. However, it
5  * utilizes 6 threads, so it blows up into a lot of executions quickly.
6  */
7
8 #include <stdio.h>
9
10 #include <threads.h>
11 #include "librace.h"
12 #include "stdatomic.h"
13
14 atomic_int x;
15 int var = 0;
16
17 static void a(void *obj)
18 {
19         store_32(&var, 1);
20         atomic_store_explicit(&x, *((int *)obj), memory_order_release);
21         atomic_store_explicit(&x, *((int *)obj) + 1, memory_order_relaxed);
22 }
23
24 static void b2(void *obj)
25 {
26         int r = atomic_load_explicit(&x, memory_order_acquire);
27         printf("r = %u\n", r);
28         store_32(&var, 3);
29 }
30
31 static void b1(void *obj)
32 {
33         thrd_t t3, t4;
34         int i = 7;
35         int r = atomic_load_explicit(&x, memory_order_acquire);
36         printf("r = %u\n", r);
37         store_32(&var, 2);
38         thrd_create(&t3, (thrd_start_t)&a, &i);
39         thrd_create(&t4, (thrd_start_t)&b2, NULL);
40         thrd_join(t3);
41         thrd_join(t4);
42 }
43
44 static void c(void *obj)
45 {
46         atomic_store_explicit(&x, 22, memory_order_relaxed);
47 }
48
49 int user_main(int argc, char **argv)
50 {
51         thrd_t t1, t2, t5;
52         int i = 4;
53
54         atomic_init(&x, 0);
55
56         thrd_create(&t1, (thrd_start_t)&a, &i);
57         thrd_create(&t2, (thrd_start_t)&b1, NULL);
58         thrd_create(&t5, (thrd_start_t)&c, NULL);
59
60         thrd_join(t1);
61         thrd_join(t2);
62         thrd_join(t5);
63
64         return 0;
65 }