C · 375 bytes Raw Blame History
1 #include <stdatomic.h>
2
3 _Atomic unsigned char g8;
4
5 unsigned char load_then_store8(unsigned char x) {
6 unsigned char y = atomic_load_explicit(&g8, memory_order_acquire);
7 atomic_store_explicit(&g8, (unsigned char)(x + y), memory_order_release);
8 return y;
9 }
10
11 unsigned char swap8(unsigned char x) {
12 return atomic_exchange_explicit(&g8, x, memory_order_acq_rel);
13 }