From a71984093ab814a741a1548e5c075e1e94721360 Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Mon, 4 Feb 2019 13:12:55 +0200 Subject: [PATCH] Add bool atomics --- platform/mbed_critical.c | 3 +++ platform/mbed_critical.h | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/platform/mbed_critical.c b/platform/mbed_critical.c index 9ac527cda2..aebebe3280 100644 --- a/platform/mbed_critical.c +++ b/platform/mbed_critical.c @@ -100,6 +100,9 @@ void core_util_critical_section_exit(void) } } +/* Inline bool implementations in the header use uint8_t versions to manipulate the bool */ +MBED_STATIC_ASSERT(sizeof(bool) == sizeof(uint8_t), "Surely bool is a byte"); + #if MBED_EXCLUSIVE_ACCESS /* Supress __ldrex and __strex deprecated warnings - "#3731-D: intrinsic is deprecated" */ diff --git a/platform/mbed_critical.h b/platform/mbed_critical.h index 69759685dd..c80b73d057 100644 --- a/platform/mbed_critical.h +++ b/platform/mbed_critical.h @@ -211,6 +211,12 @@ bool core_util_atomic_cas_u32(volatile uint32_t *ptr, uint32_t *expectedCurrentV /** \copydoc core_util_atomic_cas_u8 */ bool core_util_atomic_cas_u64(volatile uint64_t *ptr, uint64_t *expectedCurrentValue, uint64_t desiredValue); +/** \copydoc core_util_atomic_cas_u8 */ +MBED_FORCEINLINE bool core_util_atomic_cas_bool(volatile bool *ptr, bool *expectedCurrentValue, bool desiredValue) +{ + return (bool)core_util_atomic_cas_u8((volatile uint8_t *)ptr, (uint8_t *)expectedCurrentValue, desiredValue); +} + /** \copydoc core_util_atomic_cas_u8 */ bool core_util_atomic_cas_ptr(void *volatile *ptr, void **expectedCurrentValue, void *desiredValue); @@ -257,6 +263,18 @@ MBED_FORCEINLINE uint32_t core_util_atomic_load_u32(const volatile uint32_t *val */ uint64_t core_util_atomic_load_u64(const volatile uint64_t *valuePtr); +/** + * Atomic load. + * @param valuePtr Target memory location. + * @return The loaded value. + */ +MBED_FORCEINLINE bool core_util_atomic_load_bool(const volatile bool *valuePtr) +{ + bool value = *valuePtr; + MBED_BARRIER(); + return value; +} + /** * Atomic load. * @param valuePtr Target memory location. @@ -312,6 +330,18 @@ MBED_FORCEINLINE void core_util_atomic_store_u32(volatile uint32_t *valuePtr, ui */ void core_util_atomic_store_u64(volatile uint64_t *valuePtr, uint64_t desiredValue); +/** + * Atomic store. + * @param valuePtr Target memory location. + * @param desiredValue The value to store. + */ +MBED_FORCEINLINE void core_util_atomic_store_bool(volatile bool *valuePtr, bool desiredValue) +{ + MBED_BARRIER(); + *valuePtr = desiredValue; + MBED_BARRIER(); +} + /** * Atomic store. * @param valuePtr Target memory location.