759a4fd8eaabf1f502bc22d9dddc66f8ec55e40b
[cdsspec-compiler.git] / test / uninit.cc
1 /**
2  * @file uninit.cc
3  * @brief Uninitialized loads test
4  *
5  * This is a test of the "uninitialized loads" code. While we don't explicitly
6  * initialize y, this example's synchronization pattern should guarantee we
7  * never see it uninitialized.
8  */
9 #include <stdio.h>
10 #include <threads.h>
11 #include <atomic>
12
13 #include "librace.h"
14
15 std::atomic_int x;
16 std::atomic_int y;
17
18 static void a(void *obj)
19 {
20         int flag = x.load(std::memory_order_acquire);
21         printf("flag: %d\n", flag);
22         if (flag == 2)
23                 printf("Load: %d\n", y.load(std::memory_order_relaxed));
24 }
25
26 static void b(void *obj)
27 {
28         printf("fetch_add: %d\n", x.fetch_add(1, std::memory_order_relaxed));
29 }
30
31 static void c(void *obj)
32 {
33         y.store(3, std::memory_order_relaxed);
34         x.store(1, std::memory_order_release);
35 }
36
37 int user_main(int argc, char **argv)
38 {
39         thrd_t t1, t2, t3;
40
41         std::atomic_init(&x, 0);
42
43         printf("Main thread: creating 2 threads\n");
44         thrd_create(&t1, (thrd_start_t)&a, NULL);
45         thrd_create(&t2, (thrd_start_t)&b, NULL);
46         thrd_create(&t3, (thrd_start_t)&c, NULL);
47
48         thrd_join(t1);
49         thrd_join(t2);
50         thrd_join(t3);
51         printf("Main thread is finished\n");
52
53         return 0;
54 }