Remove C/C++11 header files that we don't really use
[satcheck.git] / clang / test / seqlock_raw_unannotated.c
1 #include <stdio.h>
2 #include <threads.h>
3 #include "libinterface.h"
4
5 /*atomic_*/ int _seq;
6 /*atomic_*/ int _data;
7
8 void seqlock_init() {
9         store_32(&_seq, 0);
10         store_32(&_data, 0);
11 }
12
13 int seqlock_read() {
14         while (true) {
15                 int old_seq = load_32(&_seq);
16
17                 if (old_seq % 2 == 1) {
18                 } else {
19                         int res = load_32(&_data);
20                         int seq = load_32(&_seq);
21                         
22                         if (seq == old_seq) { // relaxed
23                                 return res;
24                         }
25                 }
26         }
27 }
28         
29 void seqlock_write(int new_data) {
30         while (true) {
31                 int old_seq = load_32(&_seq);
32                 if (old_seq % 2 == 1) {
33                 } else {
34                         int new_seq = old_seq + 1;
35                         int cas_value = rmw_32(CAS, &_seq, old_seq, new_seq);
36                         if (old_seq == cas_value) {
37                                 break;
38                         }
39                 }
40         }
41         store_32(&_data, new_data);
42
43         rmw_32(ADD, &_seq, /*dummy */0, 1);
44 }
45
46 static void a(void *obj) {
47         seqlock_write(3);
48 }
49
50 static void b(void *obj) {
51         seqlock_write(2);
52 }
53
54 static void c(void *obj) {
55         int r1 = seqlock_read();
56 }
57
58 int user_main(int argc, char **argv) {
59         thrd_t t1, t2, t3;
60         seqlock_init();
61
62         thrd_create(&t1, (thrd_start_t)&a, NULL);
63         thrd_create(&t2, (thrd_start_t)&b, NULL);
64         thrd_create(&t3, (thrd_start_t)&c, NULL);
65
66         thrd_join(t1);
67         thrd_join(t2);
68         thrd_join(t3);
69         return 0;
70 }