4 #include "libinterface.h"
6 // Sequence for reader consistency check
8 // It needs to be atomic to avoid data races
18 int old_seq = load_32(&_seq); // acquire
20 if (old_seq % 2 == 1) {
23 res = load_32(&_data);
24 int seq = load_32(&_seq);
34 int seqlock_write(int new_data) {
35 int old_seq = load_32(&_seq);
38 if (old_seq % 2 == 1) {
41 int new_seq = old_seq + 1;
42 int cas_value = rmw_32(CAS, &_seq, old_seq, new_seq);
44 if (cas_value == old_seq) {
46 store_32(&_data, new_data);
47 rmw_32(ADD, &_seq, /* dummy */0, 1);
56 static void a(void *obj) {
58 for (int i = 0; i < PROBLEMSIZE; i++) {
59 q = seqlock_write(30);
64 static void b(void *obj) {
66 for (int i = 0; i < PROBLEMSIZE; i++) {
72 static void c(void *obj) {
74 for (int i = 0; i < PROBLEMSIZE; i++) {
80 int user_main(int argc, char **argv) {
81 thrd_t t1; thrd_t t2; thrd_t t3;
84 thrd_create(&t1, (thrd_start_t)&a, NULL);
85 thrd_create(&t2, (thrd_start_t)&b, NULL);
86 thrd_create(&t3, (thrd_start_t)&c, NULL);