diff options
author | Daniel Salzman <daniel.salzman@nic.cz> | 2024-12-11 17:31:39 +0100 |
---|---|---|
committer | Daniel Salzman <daniel.salzman@nic.cz> | 2024-12-11 17:36:20 +0100 |
commit | 853fffc0e0822c544a84657e330036ec1c72d71d (patch) | |
tree | c008ffce354d10faad63556e1350c1883ed0f1c1 | |
parent | contrib/atomic: replace a dangerous fallback with a spin-lock protected version (diff) | |
download | knot-853fffc0e0822c544a84657e330036ec1c72d71d.tar.xz knot-853fffc0e0822c544a84657e330036ec1c72d71d.zip |
contrib/atomic: use volatile for the fallback implementation
-rw-r--r-- | src/contrib/atomic.h | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/src/contrib/atomic.h b/src/contrib/atomic.h index 355930edc..8a408b992 100644 --- a/src/contrib/atomic.h +++ b/src/contrib/atomic.h @@ -69,7 +69,7 @@ #define ATOMIC_SET(dst, val) ({ \ knot_spin_lock((knot_spin_t *)&(dst).lock); \ - (dst).value = (val); \ + (dst).value.vol = (val); \ knot_spin_unlock((knot_spin_t *)&(dst).lock); \ }) @@ -84,34 +84,37 @@ #define ATOMIC_GET(src) ({ \ knot_spin_lock((knot_spin_t *)&(src).lock); \ - typeof((src).value) _z = (src).value; \ + typeof((src).value.non_vol) _z = (typeof((src).value.non_vol))(src).value.vol; \ knot_spin_unlock((knot_spin_t *)&(src).lock); \ _z; \ }) #define ATOMIC_ADD(dst, val) ({ \ knot_spin_lock((knot_spin_t *)&(dst).lock); \ - (dst).value += (val); \ + (dst).value.vol += (val); \ knot_spin_unlock((knot_spin_t *)&(dst).lock); \ }) #define ATOMIC_SUB(dst, val) ({ \ knot_spin_lock((knot_spin_t *)&(dst).lock); \ - (dst).value -= (val); \ + (dst).value.vol -= (val); \ knot_spin_unlock((knot_spin_t *)&(dst).lock); \ }) #define ATOMIC_XCHG(dst, val) ({ \ knot_spin_lock((knot_spin_t *)&(dst).lock); \ - typeof((dst).value) _z = (dst).value; \ - (dst).value = (val); \ + typeof((dst).value.non_vol) _z = (typeof((dst).value.non_vol))(dst).value.vol; \ + (dst).value.vol = (val); \ knot_spin_unlock((knot_spin_t *)&(dst).lock); \ _z; \ }) #define ATOMIC_T(x) struct { \ knot_spin_t lock; \ - x value; \ + union { \ + volatile x vol; \ + x non_vol; \ + } value; \ } typedef ATOMIC_T(uint16_t) knot_atomic_uint16_t; |