test: add a "pending release sequences" test
[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 "libthreads.h"
11 #include "librace.h"
12 #include "stdatomic.h"
13
14 atomic_int x;
15
16 static void a(void *obj)
17 {
18         atomic_store_explicit(&x, *((int *)obj), memory_order_release);
19         atomic_store_explicit(&x, *((int *)obj) + 1, memory_order_relaxed);
20 }
21
22 static void b2(void *obj)
23 {
24         int r = atomic_load_explicit(&x, memory_order_acquire);
25         printf("r = %u\n", r);
26 }
27
28 static void b1(void *obj)
29 {
30         thrd_t t3, t4;
31         int i = 7;
32         int r = atomic_load_explicit(&x, memory_order_acquire);
33         printf("r = %u\n", r);
34         thrd_create(&t3, (thrd_start_t)&a, &i);
35         thrd_create(&t4, (thrd_start_t)&b2, NULL);
36         thrd_join(t3);
37         thrd_join(t4);
38 }
39
40 static void c(void *obj)
41 {
42         atomic_store_explicit(&x, 22, memory_order_relaxed);
43 }
44
45 void user_main()
46 {
47         thrd_t t1, t2, t5;
48         int i = 4;
49
50         atomic_init(&x, 0);
51
52         thrd_create(&t1, (thrd_start_t)&a, &i);
53         thrd_create(&t2, (thrd_start_t)&b1, NULL);
54         thrd_create(&t5, (thrd_start_t)&c, NULL);
55
56         thrd_join(t1);
57         thrd_join(t2);
58         thrd_join(t5);
59 }