--- /dev/null
+Chase-Lev (buggy) 65 24 0.0021 0.13 68 3.2*10^-5
+Chase-Lev (correct) 49 1 0.0016 0.06 75 3.2*10^-5
+SPSC (buggy) 10 2 0.0002 0.03 26 1.7*10^-5
+SPSC (correct) 15 0 0.0004 0.03 29 2.7*10^-5
+Barrier 7 0 0.0002 0.01 23 2.9*10^-5
+Dekker 2313 0 0.0793 10.19 52 3.4*10^-5
+MCS lock 12609 0 0.3409 4.85 65 2.7*10^-5
+MPMC queue 11306
+M&S queue 114
+Linux RW lock 1348
+Seqlock 9124 0 0.4895 3.818 38 5.3*10^-5
--- /dev/null
+#include <stdatomic.h>
+#include <threads.h>
+
+typedef struct seqlock {
+ // Sequence for reader consistency check
+ atomic_int _seq;
+ // It needs to be atomic to avoid data races
+ atomic_int _data;
+
+ seqlock() {
+ atomic_init(&_seq, 0);
+ atomic_init(&_data, 0);
+ }
+
+ int read() {
+ while (true) {
+ int old_seq = _seq.load(memory_order_acquire); // acquire
+ if (old_seq % 2 == 1) continue;
+
+ int res = _data.load(memory_order_acquire); // acquire
+ if (_seq.load(memory_order_relaxed) == old_seq) { // relaxed
+ return res;
+ }
+ }
+ }
+
+ void write(int new_data) {
+ while (true) {
+ // This might be a relaxed too
+ int old_seq = _seq.load(memory_order_acquire); // acquire
+ if (old_seq % 2 == 1)
+ continue; // Retry
+
+ // Should be relaxed!!!
+ if (_seq.compare_exchange_strong(old_seq, old_seq + 1,
+ memory_order_relaxed, memory_order_relaxed)) // relaxed
+ break;
+ }
+
+ // Update the data
+ _data.store(new_data, memory_order_release); // release
+
+ _seq.fetch_add(1, memory_order_release); // release
+ }
+
+} seqlock_t;
+
+
+seqlock_t *lock;
+
+static void a(void *obj) {
+ lock->write(3);
+}
+
+static void b(void *obj) {
+ lock->write(2);
+}
+
+static void c(void *obj) {
+ int r1 = lock->read();
+}
+
+int user_main(int argc, char **argv) {
+ thrd_t t1, t2, t3, t4;
+ lock = new seqlock_t();
+
+ thrd_create(&t1, (thrd_start_t)&a, NULL);
+ thrd_create(&t2, (thrd_start_t)&b, NULL);
+ thrd_create(&t3, (thrd_start_t)&c, NULL);
+
+ thrd_join(t1);
+ thrd_join(t2);
+ thrd_join(t3);
+ return 0;
+}