6 typedef struct seqlock {
7 // Sequence for reader consistency check
9 // It needs to be atomic to avoid data races
13 atomic_init(&_seq, 0);
14 atomic_init(&_data, 0);
19 int old_seq = _seq.load(wildcard(1)); // acquire
20 if (old_seq % 2 == 1) continue;
22 int res = _data.load(wildcard(2)); // acquire
23 if (_seq.load(wildcard(3)) == old_seq) { // relaxed
29 void write(int new_data) {
31 int old_seq = _seq.load(wildcard(4)); // acquire
35 // Should be relaxed!!!
36 if (_seq.compare_exchange_strong(old_seq, old_seq + 1,
37 wildcard(5), wildcard(6))) // relaxed
42 _data.store(new_data, wildcard(7)); // release
44 _seq.fetch_add(1, wildcard(8)); // release