mirror of https://github.com/ARMmbed/mbed-os.git
Merge pull request #2054 from geky/critical-cas-ptr
Added cas instrinsics for pointer valuespull/2051/head
commit
c55c07a890
|
@ -19,6 +19,8 @@
|
||||||
#define __MBED_UTIL_CRITICAL_H__
|
#define __MBED_UTIL_CRITICAL_H__
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
@ -47,7 +49,7 @@ bool core_util_are_interrupts_enabled(void);
|
||||||
* section) will be preserved on exit from the section.
|
* section) will be preserved on exit from the section.
|
||||||
* 4) This implementation will currently only work on code running in privileged mode.
|
* 4) This implementation will currently only work on code running in privileged mode.
|
||||||
*/
|
*/
|
||||||
void core_util_critical_section_enter();
|
void core_util_critical_section_enter(void);
|
||||||
|
|
||||||
/** Mark the end of a critical section
|
/** Mark the end of a critical section
|
||||||
*
|
*
|
||||||
|
@ -60,7 +62,7 @@ void core_util_critical_section_enter();
|
||||||
* section) will be preserved on exit from the section.
|
* section) will be preserved on exit from the section.
|
||||||
* 4) This implementation will currently only work on code running in privileged mode.
|
* 4) This implementation will currently only work on code running in privileged mode.
|
||||||
*/
|
*/
|
||||||
void core_util_critical_section_exit();
|
void core_util_critical_section_exit(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Atomic compare and set. It compares the contents of a memory location to a
|
* Atomic compare and set. It compares the contents of a memory location to a
|
||||||
|
@ -106,7 +108,7 @@ void core_util_critical_section_exit();
|
||||||
*
|
*
|
||||||
* function incr(p : pointer to int, a : int) returns int {
|
* function incr(p : pointer to int, a : int) returns int {
|
||||||
* done = false
|
* done = false
|
||||||
* *value = *p // This fetch operation need not be atomic.
|
* value = *p // This fetch operation need not be atomic.
|
||||||
* while not done {
|
* while not done {
|
||||||
* done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
|
* done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
|
||||||
* }
|
* }
|
||||||
|
@ -159,7 +161,7 @@ bool core_util_atomic_cas_u8(uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_
|
||||||
*
|
*
|
||||||
* function incr(p : pointer to int, a : int) returns int {
|
* function incr(p : pointer to int, a : int) returns int {
|
||||||
* done = false
|
* done = false
|
||||||
* *value = *p // This fetch operation need not be atomic.
|
* value = *p // This fetch operation need not be atomic.
|
||||||
* while not done {
|
* while not done {
|
||||||
* done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
|
* done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
|
||||||
* }
|
* }
|
||||||
|
@ -212,7 +214,7 @@ bool core_util_atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue, uin
|
||||||
*
|
*
|
||||||
* function incr(p : pointer to int, a : int) returns int {
|
* function incr(p : pointer to int, a : int) returns int {
|
||||||
* done = false
|
* done = false
|
||||||
* *value = *p // This fetch operation need not be atomic.
|
* value = *p // This fetch operation need not be atomic.
|
||||||
* while not done {
|
* while not done {
|
||||||
* done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
|
* done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
|
||||||
* }
|
* }
|
||||||
|
@ -221,6 +223,59 @@ bool core_util_atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue, uin
|
||||||
*/
|
*/
|
||||||
bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue);
|
bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_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_ptr(void **ptr, void **expectedCurrentValue, void *desiredValue);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Atomic increment.
|
* Atomic increment.
|
||||||
* @param valuePtr Target memory location being incremented.
|
* @param valuePtr Target memory location being incremented.
|
||||||
|
@ -245,6 +300,17 @@ uint16_t core_util_atomic_incr_u16(uint16_t * valuePtr, uint16_t delta);
|
||||||
*/
|
*/
|
||||||
uint32_t core_util_atomic_incr_u32(uint32_t *valuePtr, uint32_t delta);
|
uint32_t core_util_atomic_incr_u32(uint32_t *valuePtr, uint32_t delta);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Atomic increment.
|
||||||
|
* @param valuePtr Target memory location being incremented.
|
||||||
|
* @param delta The amount being incremented in bytes.
|
||||||
|
* @return The new incremented value.
|
||||||
|
*
|
||||||
|
* @note The type of the pointer argument is not taken into account
|
||||||
|
* and the pointer is incremented by bytes.
|
||||||
|
*/
|
||||||
|
void *core_util_atomic_incr_ptr(void **valuePtr, ptrdiff_t delta);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Atomic decrement.
|
* Atomic decrement.
|
||||||
* @param valuePtr Target memory location being decremented.
|
* @param valuePtr Target memory location being decremented.
|
||||||
|
@ -269,6 +335,17 @@ uint16_t core_util_atomic_decr_u16(uint16_t * valuePtr, uint16_t delta);
|
||||||
*/
|
*/
|
||||||
uint32_t core_util_atomic_decr_u32(uint32_t *valuePtr, uint32_t delta);
|
uint32_t core_util_atomic_decr_u32(uint32_t *valuePtr, uint32_t delta);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Atomic decrement.
|
||||||
|
* @param valuePtr Target memory location being decremented.
|
||||||
|
* @param delta The amount being decremented in bytes.
|
||||||
|
* @return The new decremented value.
|
||||||
|
*
|
||||||
|
* @note The type of the pointer argument is not taken into account
|
||||||
|
* and the pointer is decremented by bytes
|
||||||
|
*/
|
||||||
|
void *core_util_atomic_decr_ptr(void **valuePtr, ptrdiff_t delta);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
} // extern "C"
|
} // extern "C"
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -15,15 +15,11 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define __STDC_LIMIT_MACROS
|
#include "critical.h"
|
||||||
#include <stdint.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include "cmsis.h"
|
#include "cmsis.h"
|
||||||
#include "mbed_assert.h"
|
#include "mbed_assert.h"
|
||||||
|
|
||||||
// Module include
|
|
||||||
#include "critical.h"
|
|
||||||
|
|
||||||
#define EXCLUSIVE_ACCESS (!defined (__CORTEX_M0) && !defined (__CORTEX_M0PLUS))
|
#define EXCLUSIVE_ACCESS (!defined (__CORTEX_M0) && !defined (__CORTEX_M0PLUS))
|
||||||
|
|
||||||
static volatile uint32_t interrupt_enable_counter = 0;
|
static volatile uint32_t interrupt_enable_counter = 0;
|
||||||
|
@ -38,7 +34,7 @@ bool core_util_are_interrupts_enabled(void)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void core_util_critical_section_enter()
|
void core_util_critical_section_enter(void)
|
||||||
{
|
{
|
||||||
bool interrupts_disabled = !core_util_are_interrupts_enabled();
|
bool interrupts_disabled = !core_util_are_interrupts_enabled();
|
||||||
__disable_irq();
|
__disable_irq();
|
||||||
|
@ -63,7 +59,7 @@ void core_util_critical_section_enter()
|
||||||
interrupt_enable_counter++;
|
interrupt_enable_counter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void core_util_critical_section_exit()
|
void core_util_critical_section_exit(void)
|
||||||
{
|
{
|
||||||
/* If critical_section_enter has not previously been called, do nothing */
|
/* If critical_section_enter has not previously been called, do nothing */
|
||||||
if (interrupt_enable_counter) {
|
if (interrupt_enable_counter) {
|
||||||
|
@ -236,6 +232,13 @@ bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uin
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool core_util_atomic_cas_ptr(void **ptr, void **expectedCurrentValue, void *desiredValue) {
|
||||||
|
return core_util_atomic_cas_u32(
|
||||||
|
(uintptr_t *)ptr,
|
||||||
|
(uintptr_t *)expectedCurrentValue,
|
||||||
|
(uintptr_t)desiredValue);
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t core_util_atomic_incr_u8(uint8_t *valuePtr, uint8_t delta)
|
uint8_t core_util_atomic_incr_u8(uint8_t *valuePtr, uint8_t delta)
|
||||||
{
|
{
|
||||||
uint8_t newValue;
|
uint8_t newValue;
|
||||||
|
@ -266,6 +269,10 @@ uint32_t core_util_atomic_incr_u32(uint32_t * valuePtr, uint32_t delta)
|
||||||
return newValue;
|
return newValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *core_util_atomic_incr_ptr(void **valuePtr, ptrdiff_t delta) {
|
||||||
|
return core_util_atomic_incr((uintptr_t)valuePtr, (uintptr_t)delta);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
uint8_t core_util_atomic_decr_u8(uint8_t *valuePtr, uint8_t delta)
|
uint8_t core_util_atomic_decr_u8(uint8_t *valuePtr, uint8_t delta)
|
||||||
{
|
{
|
||||||
|
@ -297,5 +304,9 @@ uint32_t core_util_atomic_decr_u32(uint32_t * valuePtr, uint32_t delta)
|
||||||
return newValue;
|
return newValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *core_util_atomic_decr_ptr(void **valuePtr, ptrdiff_t delta) {
|
||||||
|
return core_util_atomic_decr((uintptr_t)valuePtr, (uintptr_t)delta);
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue