From 001c818b94e155e64d73c8e8df7bde28a6fe20bd Mon Sep 17 00:00:00 2001 From: Matthieu Schaller <schaller@strw.leidenuniv.nl> Date: Wed, 25 Mar 2020 14:11:20 +0100 Subject: [PATCH] Added an atomic max for chars --- src/atomic.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/atomic.h b/src/atomic.h index 8a790a7f6c..0916497efc 100644 --- a/src/atomic.h +++ b/src/atomic.h @@ -121,6 +121,27 @@ __attribute__((always_inline)) INLINE static void atomic_min_d( } while (test_val.as_long_long != old_val.as_long_long); } +/** + * @brief Atomic max operation on chars. + * + * This is a text-book implementation based on an atomic CAS. + * + * @param address The address to update. + * @param y The value to update the address with. + */ +__attribute__((always_inline)) INLINE static void atomic_max_c( + volatile char *const address, const char y) { + + char test_val, old_val, new_val; + old_val = *address; + + do { + test_val = old_val; + new_val = max(old_val, y); + old_val = atomic_cas(address, test_val, new_val); + } while (test_val != old_val); +} + /** * @brief Atomic max operation on floats. * -- GitLab