changes
[model-checker-benchmarks.git] / seqlock / seqlock.c
1 #include <stdatomic.h>
2 #include <threads.h>
3
4 typedef struct seqlock {
5         // Sequence for reader consistency check
6         atomic_int _seq;
7         // It needs to be atomic to avoid data races
8         atomic_int _data;
9
10         seqlock() {
11                 atomic_init(&_seq, 0);
12                 atomic_init(&_data, 0);
13         }
14
15         int read() {
16                 while (true) {
17                         int old_seq = _seq.load(memory_order_acquire); // acquire
18                         if (old_seq % 2 == 1) continue;
19
20                         int res = _data.load(memory_order_acquire); // acquire
21                         if (_seq.load(memory_order_relaxed) == old_seq) { // relaxed
22                                 return res;
23                         }
24                 }
25         }
26         
27         void write(int new_data) {
28                 while (true) {
29                         // This might be a relaxed too
30                         int old_seq = _seq.load(memory_order_acquire); // acquire
31                         if (old_seq % 2 == 1)
32                                 continue; // Retry
33
34                         // Should be relaxed!!! 
35                         if (_seq.compare_exchange_strong(old_seq, old_seq + 1,
36                                 memory_order_relaxed, memory_order_relaxed)) // relaxed 
37                                 break;
38                 }
39
40                 // Update the data
41                 _data.store(new_data, memory_order_release); // release
42
43                 _seq.fetch_add(1, memory_order_release); // release
44         }
45
46 } seqlock_t;
47
48
49 seqlock_t *lock;
50
51 static void a(void *obj) {
52         lock->write(3);
53 }
54
55 static void b(void *obj) {
56         lock->write(2);
57 }
58
59 static void c(void *obj) {
60         int r1 = lock->read();
61 }
62
63 int user_main(int argc, char **argv) {
64         thrd_t t1, t2, t3, t4;
65         lock = new seqlock_t();
66
67         thrd_create(&t1, (thrd_start_t)&a, NULL);
68         thrd_create(&t2, (thrd_start_t)&b, NULL);
69         thrd_create(&t3, (thrd_start_t)&c, NULL);
70
71         thrd_join(t1);
72         thrd_join(t2);
73         thrd_join(t3);
74         return 0;
75 }