diff --git a/hal/api/critical.h b/hal/api/critical.h index c08617db6e5..cca96fa9746 100644 --- a/hal/api/critical.h +++ b/hal/api/critical.h @@ -18,6 +18,8 @@ #ifndef __MBED_UTIL_CRITICAL_H__ #define __MBED_UTIL_CRITICAL_H__ +#include + #ifdef __cplusplus extern "C" { #endif @@ -48,6 +50,213 @@ void core_util_critical_section_enter(); */ void core_util_critical_section_exit(); +/** + * Atomic compare and set. It compares the contents of a memory location to a + * given value and, only if they are the same, modifies the contents of that + * memory location to a given new value. This is done as a single atomic + * operation. The atomicity guarantees that the new value is calculated based on + * up-to-date information; if the value had been updated by another thread in + * the meantime, the write would fail due to a mismatched expectedCurrentValue. + * + * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect + * you to the article on compare-and swap]. + * + * @param ptr The target memory location. + * @param[in,out] expectedCurrentValue A pointer to some location holding the + * expected current value of the data being set atomically. + * The computed 'desiredValue' should be a function of this current value. + * @Note: This is an in-out parameter. In the + * failure case of atomic_cas (where the + * destination isn't set), the pointee of expectedCurrentValue is + * updated with the current value. + * @param[in] desiredValue The new value computed based on '*expectedCurrentValue'. + * + * @return true if the memory location was atomically + * updated with the desired value (after verifying + * that it contained the expectedCurrentValue), + * false otherwise. In the failure case, + * exepctedCurrentValue is updated with the new + * value of the target memory location. + * + * pseudocode: + * function cas(p : pointer to int, old : pointer to int, new : int) returns bool { + * if *p != *old { + * *old = *p + * return false + * } + * *p = new + * return true + * } + * + * @Note: In the failure case (where the destination isn't set), the value + * pointed to by expectedCurrentValue is still updated with the current value. + * This property helps writing concise code for the following incr: + * + * function incr(p : pointer to int, a : int) returns int { + * done = false + * *value = *p // This fetch operation need not be atomic. + * while not done { + * done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success + * } + * return value + a + * } + */ +bool core_util_atomic_cas_u8(uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue); + +/** + * Atomic compare and set. It compares the contents of a memory location to a + * given value and, only if they are the same, modifies the contents of that + * memory location to a given new value. This is done as a single atomic + * operation. The atomicity guarantees that the new value is calculated based on + * up-to-date information; if the value had been updated by another thread in + * the meantime, the write would fail due to a mismatched expectedCurrentValue. + * + * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect + * you to the article on compare-and swap]. + * + * @param ptr The target memory location. + * @param[in,out] expectedCurrentValue A pointer to some location holding the + * expected current value of the data being set atomically. + * The computed 'desiredValue' should be a function of this current value. + * @Note: This is an in-out parameter. In the + * failure case of atomic_cas (where the + * destination isn't set), the pointee of expectedCurrentValue is + * updated with the current value. + * @param[in] desiredValue The new value computed based on '*expectedCurrentValue'. + * + * @return true if the memory location was atomically + * updated with the desired value (after verifying + * that it contained the expectedCurrentValue), + * false otherwise. In the failure case, + * exepctedCurrentValue is updated with the new + * value of the target memory location. + * + * pseudocode: + * function cas(p : pointer to int, old : pointer to int, new : int) returns bool { + * if *p != *old { + * *old = *p + * return false + * } + * *p = new + * return true + * } + * + * @Note: In the failure case (where the destination isn't set), the value + * pointed to by expectedCurrentValue is still updated with the current value. + * This property helps writing concise code for the following incr: + * + * function incr(p : pointer to int, a : int) returns int { + * done = false + * *value = *p // This fetch operation need not be atomic. + * while not done { + * done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success + * } + * return value + a + * } + */ +bool core_util_atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue); + +/** + * Atomic compare and set. It compares the contents of a memory location to a + * given value and, only if they are the same, modifies the contents of that + * memory location to a given new value. This is done as a single atomic + * operation. The atomicity guarantees that the new value is calculated based on + * up-to-date information; if the value had been updated by another thread in + * the meantime, the write would fail due to a mismatched expectedCurrentValue. + * + * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect + * you to the article on compare-and swap]. + * + * @param ptr The target memory location. + * @param[in,out] expectedCurrentValue A pointer to some location holding the + * expected current value of the data being set atomically. + * The computed 'desiredValue' should be a function of this current value. + * @Note: This is an in-out parameter. In the + * failure case of atomic_cas (where the + * destination isn't set), the pointee of expectedCurrentValue is + * updated with the current value. + * @param[in] desiredValue The new value computed based on '*expectedCurrentValue'. + * + * @return true if the memory location was atomically + * updated with the desired value (after verifying + * that it contained the expectedCurrentValue), + * false otherwise. In the failure case, + * exepctedCurrentValue is updated with the new + * value of the target memory location. + * + * pseudocode: + * function cas(p : pointer to int, old : pointer to int, new : int) returns bool { + * if *p != *old { + * *old = *p + * return false + * } + * *p = new + * return true + * } + * + * @Note: In the failure case (where the destination isn't set), the value + * pointed to by expectedCurrentValue is still updated with the current value. + * This property helps writing concise code for the following incr: + * + * function incr(p : pointer to int, a : int) returns int { + * done = false + * *value = *p // This fetch operation need not be atomic. + * while not done { + * done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success + * } + * return value + a + * } + */ +bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue); + +/** + * Atomic increment. + * @param valuePtr Target memory location being incremented. + * @param delta The amount being incremented. + * @return The new incremented value. + */ +uint8_t core_util_atomic_incr_u8(uint8_t * valuePtr, uint8_t delta); + +/** + * Atomic increment. + * @param valuePtr Target memory location being incremented. + * @param delta The amount being incremented. + * @return The new incremented value. + */ +uint16_t core_util_atomic_incr_u16(uint16_t * valuePtr, uint16_t delta); + +/** + * Atomic increment. + * @param valuePtr Target memory location being incremented. + * @param delta The amount being incremented. + * @return The new incremented value. + */ +uint32_t core_util_atomic_incr_u32(uint32_t * valuePtr, uint32_t delta); + +/** + * Atomic decrement. + * @param valuePtr Target memory location being decremented. + * @param delta The amount being decremented. + * @return The new decremented value. + */ +uint8_t core_util_atomic_decr_u8(uint8_t * valuePtr, uint8_t delta); + +/** + * Atomic decrement. + * @param valuePtr Target memory location being decremented. + * @param delta The amount being decremented. + * @return The new decremented value. + */ +uint16_t core_util_atomic_decr_u16(uint16_t * valuePtr, uint16_t delta); + +/** + * Atomic decrement. + * @param valuePtr Target memory location being decremented. + * @param delta The amount being decremented. + * @return The new decremented value. + */ +uint32_t core_util_atomic_decr_u32(uint32_t * valuePtr, uint32_t delta); + #ifdef __cplusplus } // extern "C" #endif diff --git a/hal/common/critical.c b/hal/common/critical.c index 5084b12b446..93c261ff0d6 100644 --- a/hal/common/critical.c +++ b/hal/common/critical.c @@ -24,6 +24,8 @@ // Module include #include "critical.h" +#define EXCLUSIVE_ACCESS (!defined (__CORTEX_M0) && !defined (__CORTEX_M0PLUS)) + static volatile uint32_t interrupt_enable_counter = 0; static volatile uint32_t critical_primask = 0; @@ -66,3 +68,215 @@ void core_util_critical_section_exit() } } } + +#if EXCLUSIVE_ACCESS + +bool core_util_atomic_cas_u8(uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue) +{ + uint8_t currentValue = __LDREXB((volatile uint8_t*)ptr); + if (currentValue != *expectedCurrentValue) { + *expectedCurrentValue = currentValue; + __CLREX(); + return false; + } + + return !__STREXB(desiredValue, (volatile uint8_t*)ptr); +} + +bool core_util_atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue) +{ + uint16_t currentValue = __LDREXH((volatile uint16_t*)ptr); + if (currentValue != *expectedCurrentValue) { + *expectedCurrentValue = currentValue; + __CLREX(); + return false; + } + + return !__STREXH(desiredValue, (volatile uint16_t*)ptr); +} + + +bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue) +{ + uint32_t currentValue = __LDREXW((volatile uint32_t*)ptr); + if (currentValue != *expectedCurrentValue) { + *expectedCurrentValue = currentValue; + __CLREX(); + return false; + } + + return !__STREXW(desiredValue, (volatile uint32_t*)ptr); +} + +uint8_t core_util_atomic_incr_u8(uint8_t * valuePtr, uint8_t delta) +{ + uint8_t newValue; + do { + newValue = __LDREXB((volatile uint8_t*)valuePtr) + delta; + } while (__STREXB(newValue, (volatile uint8_t*)valuePtr)); + return newValue; +} + +uint16_t core_util_atomic_incr_u16(uint16_t * valuePtr, uint16_t delta) +{ + uint16_t newValue; + do { + newValue = __LDREXH((volatile uint16_t*)valuePtr) + delta; + } while (__STREXH(newValue, (volatile uint16_t*)valuePtr)); + return newValue; +} + +uint32_t core_util_atomic_incr_u32(uint32_t * valuePtr, uint32_t delta) +{ + uint32_t newValue; + do { + newValue = __LDREXW((volatile uint32_t*)valuePtr) + delta; + } while (__STREXW(newValue, (volatile uint32_t*)valuePtr)); + return newValue; +} + + +uint8_t core_util_atomic_decr_u8(uint8_t * valuePtr, uint8_t delta) +{ + uint8_t newValue; + do { + newValue = __LDREXB((volatile uint8_t*)valuePtr) - delta; + } while (__STREXB(newValue, (volatile uint8_t*)valuePtr)); + return newValue; +} + +uint16_t core_util_atomic_decr_u16(uint16_t * valuePtr, uint16_t delta) +{ + uint16_t newValue; + do { + newValue = __LDREXH((volatile uint16_t*)valuePtr) - delta; + } while (__STREXH(newValue, (volatile uint16_t*)valuePtr)); + return newValue; +} + +uint32_t core_util_atomic_decr_u32(uint32_t * valuePtr, uint32_t delta) +{ + uint32_t newValue; + do { + newValue = __LDREXW((volatile uint32_t*)valuePtr) - delta; + } while (__STREXW(newValue, (volatile uint32_t*)valuePtr)); + return newValue; +} + +#else + +bool core_util_atomic_cas_u8(uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue) +{ + bool success; + uint8_t currentValue; + core_util_critical_section_enter(); + currentValue = *ptr; + if (currentValue == *expectedCurrentValue) { + *ptr = desiredValue; + success = true; + } else { + *expectedCurrentValue = currentValue; + success = false; + } + core_util_critical_section_exit(); + return success; +} + +bool core_util_atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue) +{ + bool success; + uint16_t currentValue; + core_util_critical_section_enter(); + currentValue = *ptr; + if (currentValue == *expectedCurrentValue) { + *ptr = desiredValue; + success = true; + } else { + *expectedCurrentValue = currentValue; + success = false; + } + core_util_critical_section_exit(); + return success; +} + + +bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue) +{ + bool success; + uint32_t currentValue; + core_util_critical_section_enter(); + currentValue = *ptr; + if (currentValue == *expectedCurrentValue) { + *ptr = desiredValue; + success = true; + } else { + *expectedCurrentValue = currentValue; + success = false; + } + core_util_critical_section_exit(); + return success; +} + +uint8_t core_util_atomic_incr_u8(uint8_t * valuePtr, uint8_t delta) +{ + uint8_t newValue; + core_util_critical_section_enter(); + newValue = *valuePtr + delta; + *valuePtr = newValue; + core_util_critical_section_exit(); + return newValue; +} + +uint16_t core_util_atomic_incr_u16(uint16_t * valuePtr, uint16_t delta) +{ + uint16_t newValue; + core_util_critical_section_enter(); + newValue = *valuePtr + delta; + *valuePtr = newValue; + core_util_critical_section_exit(); + return newValue; +} + +uint32_t core_util_atomic_incr_u32(uint32_t * valuePtr, uint32_t delta) +{ + uint32_t newValue; + core_util_critical_section_enter(); + newValue = *valuePtr + delta; + *valuePtr = newValue; + core_util_critical_section_exit(); + return newValue; +} + + +uint8_t core_util_atomic_decr_u8(uint8_t * valuePtr, uint8_t delta) +{ + uint8_t newValue; + core_util_critical_section_enter(); + newValue = *valuePtr - delta; + *valuePtr = newValue; + core_util_critical_section_exit(); + return newValue; +} + +uint16_t core_util_atomic_decr_u16(uint16_t * valuePtr, uint16_t delta) +{ + uint16_t newValue; + core_util_critical_section_enter(); + newValue = *valuePtr - delta; + *valuePtr = newValue; + core_util_critical_section_exit(); + return newValue; +} + +uint32_t core_util_atomic_decr_u32(uint32_t * valuePtr, uint32_t delta) +{ + uint32_t newValue; + core_util_critical_section_enter(); + newValue = *valuePtr - delta; + *valuePtr = newValue; + core_util_critical_section_exit(); + return newValue; +} + +#endif +