diff --git a/src/atomic.h b/src/atomic.h index fc2d1265a22a44ac581a455540b8afed10e8a77d..8ba1b0e692b08806d18eb453d9211a4829a8943a 100644 --- a/src/atomic.h +++ b/src/atomic.h @@ -38,6 +38,29 @@ #define atomic_cas(v, o, n) __sync_val_compare_and_swap(v, o, n) #define atomic_swap(v, n) __sync_lock_test_and_set(v, n) +/** + * @brief Atomic min operation on ints. + * + * 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_min( + volatile int *const address, const int y) { + + int *int_ptr = (int *)address; + + int test_val, old_val, new_val; + old_val = *address; + + do { + test_val = old_val; + new_val = min(old_val, y); + old_val = atomic_cas(int_ptr, test_val, new_val); + } while (test_val != old_val); +} + /** * @brief Atomic min operation on floats. * @@ -69,29 +92,6 @@ __attribute__((always_inline)) INLINE static void atomic_min_f( } while (test_val.as_int != old_val.as_int); } -/** - * @brief Atomic min operation on ints. - * - * 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_min( - volatile int *address, int y) { - - int *int_ptr = (int *)address; - - int test_val, old_val, new_val; - old_val = *address; - - do { - test_val = old_val; - new_val = min(old_val, y); - old_val = atomic_cas(int_ptr, test_val, new_val); - } while (test_val != old_val); -} - /** * @brief Atomic min operation on doubles. * @@ -145,6 +145,29 @@ __attribute__((always_inline)) INLINE static void atomic_max_c( } while (test_val != old_val); } +/** + * @brief Atomic max operation on ints. + * + * 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( + volatile int *const address, const int y) { + + int *int_ptr = (int *)address; + + int test_val, old_val, new_val; + old_val = *address; + + do { + test_val = old_val; + new_val = max(old_val, y); + old_val = atomic_cas(int_ptr, test_val, new_val); + } while (test_val != old_val); +} + /** * @brief Atomic max operation on floats. *