edeae43122e7f8b5a46dc78671abf5763e895f8b
[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  * @todo (12/11/12) this example currently doesn't run properly; it never sees
10  * the behavior where 'flag == 2'.
11  */
12 #include <stdio.h>
13 #include <threads.h>
14 #include <atomic>
15
16 #include "librace.h"
17
18 std::atomic_int x;
19 std::atomic_int y;
20
21 static void a(void *obj)
22 {
23         int flag = x.load(std::memory_order_acquire);
24         printf("flag: %d\n", flag);
25         if (flag == 2)
26                 printf("Load: %d\n", y.load(std::memory_order_relaxed));
27 }
28
29 static void b(void *obj)
30 {
31         printf("fetch_add: %d\n", x.fetch_add(1, std::memory_order_relaxed));
32 }
33
34 static void c(void *obj)
35 {
36         y.store(3, std::memory_order_relaxed);
37         x.store(1, std::memory_order_release);
38 }
39
40 int user_main(int argc, char **argv)
41 {
42         thrd_t t1, t2, t3;
43
44         std::atomic_init(&x, 0);
45
46         printf("Thread %d: creating 2 threads\n", thrd_current());
47         thrd_create(&t1, (thrd_start_t)&a, NULL);
48         thrd_create(&t2, (thrd_start_t)&b, NULL);
49         thrd_create(&t3, (thrd_start_t)&c, NULL);
50
51         thrd_join(t1);
52         thrd_join(t2);
53         thrd_join(t3);
54         printf("Thread %d is finished\n", thrd_current());
55
56         return 0;
57 }