5 #include "libinterface.h"
7 // Sequence for reader consistency check
9 // It needs to be atomic to avoid data races
10 /*atomic_*/ int _data;
19 int old_seq = load_32(&_seq); // acquire
21 if (old_seq % 2 == 1) {
24 res = load_32(&_data);
25 int seq = load_32(&_seq);
35 int seqlock_write(int new_data) {
36 int old_seq = load_32(&_seq);
39 if (old_seq % 2 == 1) {
42 int new_seq = old_seq + 1;
43 int cas_value = rmw_32(CAS, &_seq, old_seq, new_seq);
45 if (cas_value == old_seq) {
47 store_32(&_data, new_data);
48 rmw_32(ADD, &_seq, /* dummy */0, 1);
57 static void a(void *obj) {
59 for (int i = 0; i < PROBLEMSIZE; i++) {
60 q = seqlock_write(30);
65 static void b(void *obj) {
67 for (int i = 0; i < PROBLEMSIZE; i++) {
73 static void c(void *obj) {
75 for (int i = 0; i < PROBLEMSIZE; i++) {
81 int user_main(int argc, char **argv) {
82 thrd_t t1; thrd_t t2; thrd_t t3;
85 thrd_create(&t1, (thrd_start_t)&a, NULL);
86 thrd_create(&t2, (thrd_start_t)&b, NULL);
87 thrd_create(&t3, (thrd_start_t)&c, NULL);