| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- /* atomic operations */
- /*
- * SPDX-FileCopyrightText: 1997-2015 Wind River Systems, Inc.
- *
- * SPDX-License-Identifier: Apache-2.0
- */
- #ifndef _BLE_MESH_ATOMIC_H_
- #define _BLE_MESH_ATOMIC_H_
- #include "mesh_types.h"
- #ifdef __cplusplus
- extern "C" {
- #endif
- typedef bt_mesh_atomic_t bt_mesh_atomic_val_t;
- /**
- * @defgroup atomic_apis Atomic Services APIs
- * @ingroup kernel_apis
- * @{
- */
- /**
- *
- * @brief Atomic increment.
- *
- * This routine performs an atomic increment by 1 on @a target.
- *
- * @param target Address of atomic variable.
- *
- * @return Previous value of @a target.
- */
- #ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN
- static inline bt_mesh_atomic_val_t bt_mesh_atomic_inc(bt_mesh_atomic_t *target)
- {
- return bt_mesh_atomic_add(target, 1);
- }
- #else
- extern bt_mesh_atomic_val_t bt_mesh_atomic_inc(bt_mesh_atomic_t *target);
- #endif
- /**
- *
- * @brief Atomic decrement.
- *
- * This routine performs an atomic decrement by 1 on @a target.
- *
- * @param target Address of atomic variable.
- *
- * @return Previous value of @a target.
- */
- #ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN
- static inline bt_mesh_atomic_val_t bt_mesh_atomic_dec(bt_mesh_atomic_t *target)
- {
- return bt_mesh_atomic_sub(target, 1);
- }
- #else
- extern bt_mesh_atomic_val_t bt_mesh_atomic_dec(bt_mesh_atomic_t *target);
- #endif
- /**
- *
- * @brief Atomic get.
- *
- * This routine performs an atomic read on @a target.
- *
- * @param target Address of atomic variable.
- *
- * @return Value of @a target.
- */
- #ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN
- static inline bt_mesh_atomic_val_t bt_mesh_atomic_get(const bt_mesh_atomic_t *target)
- {
- return __atomic_load_n(target, __ATOMIC_SEQ_CST);
- }
- #else
- extern bt_mesh_atomic_val_t bt_mesh_atomic_get(const bt_mesh_atomic_t *target);
- #endif
- /**
- *
- * @brief Atomic get-and-set.
- *
- * This routine atomically sets @a target to @a value and returns
- * the previous value of @a target.
- *
- * @param target Address of atomic variable.
- * @param value Value to write to @a target.
- *
- * @return Previous value of @a target.
- */
- #ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN
- static inline bt_mesh_atomic_val_t bt_mesh_atomic_set(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t value)
- {
- /* This builtin, as described by Intel, is not a traditional
- * test-and-set operation, but rather an atomic exchange operation. It
- * writes value into *ptr, and returns the previous contents of *ptr.
- */
- return __atomic_exchange_n(target, value, __ATOMIC_SEQ_CST);
- }
- #else
- extern bt_mesh_atomic_val_t bt_mesh_atomic_set(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t value);
- #endif
- /**
- *
- * @brief Atomic bitwise inclusive OR.
- *
- * This routine atomically sets @a target to the bitwise inclusive OR of
- * @a target and @a value.
- *
- * @param target Address of atomic variable.
- * @param value Value to OR.
- *
- * @return Previous value of @a target.
- */
- #ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN
- static inline bt_mesh_atomic_val_t bt_mesh_atomic_or(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t value)
- {
- return __atomic_fetch_or(target, value, __ATOMIC_SEQ_CST);
- }
- #else
- extern bt_mesh_atomic_val_t bt_mesh_atomic_or(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t value);
- #endif
- /**
- *
- * @brief Atomic bitwise AND.
- *
- * This routine atomically sets @a target to the bitwise AND of @a target
- * and @a value.
- *
- * @param target Address of atomic variable.
- * @param value Value to AND.
- *
- * @return Previous value of @a target.
- */
- #ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN
- static inline bt_mesh_atomic_val_t bt_mesh_atomic_and(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t value)
- {
- return __atomic_fetch_and(target, value, __ATOMIC_SEQ_CST);
- }
- #else
- extern bt_mesh_atomic_val_t bt_mesh_atomic_and(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t value);
- #endif
- /**
- * @cond INTERNAL_HIDDEN
- */
- #define BLE_MESH_ATOMIC_BITS (sizeof(bt_mesh_atomic_val_t) * 8)
- #define BLE_MESH_ATOMIC_MASK(bit) (1 << ((bit) & (BLE_MESH_ATOMIC_BITS - 1)))
- #define BLE_MESH_ATOMIC_ELEM(addr, bit) ((addr) + ((bit) / BLE_MESH_ATOMIC_BITS))
- /**
- * INTERNAL_HIDDEN @endcond
- */
- /**
- * @brief Define an array of atomic variables.
- *
- * This macro defines an array of atomic variables containing at least
- * @a num_bits bits.
- *
- * @note
- * If used from file scope, the bits of the array are initialized to zero;
- * if used from within a function, the bits are left uninitialized.
- *
- * @param name Name of array of atomic variables.
- * @param num_bits Number of bits needed.
- */
- #define BLE_MESH_ATOMIC_DEFINE(name, num_bits) \
- bt_mesh_atomic_t name[1 + ((num_bits) - 1) / BLE_MESH_ATOMIC_BITS]
- /**
- * @brief Atomically test a bit.
- *
- * This routine tests whether bit number @a bit of @a target is set or not.
- * The target may be a single atomic variable or an array of them.
- *
- * @param target Address of atomic variable or array.
- * @param bit Bit number (starting from 0).
- *
- * @return 1 if the bit was set, 0 if it wasn't.
- */
- static inline int bt_mesh_atomic_test_bit(const bt_mesh_atomic_t *target, int bit)
- {
- bt_mesh_atomic_val_t val = bt_mesh_atomic_get(BLE_MESH_ATOMIC_ELEM(target, bit));
- return (1 & (val >> (bit & (BLE_MESH_ATOMIC_BITS - 1))));
- }
- /**
- * @brief Atomically test and clear a bit.
- *
- * Atomically clear bit number @a bit of @a target and return its old value.
- * The target may be a single atomic variable or an array of them.
- *
- * @param target Address of atomic variable or array.
- * @param bit Bit number (starting from 0).
- *
- * @return 1 if the bit was set, 0 if it wasn't.
- */
- static inline int bt_mesh_atomic_test_and_clear_bit(bt_mesh_atomic_t *target, int bit)
- {
- bt_mesh_atomic_val_t mask = BLE_MESH_ATOMIC_MASK(bit);
- bt_mesh_atomic_val_t old;
- old = bt_mesh_atomic_and(BLE_MESH_ATOMIC_ELEM(target, bit), ~mask);
- return (old & mask) != 0;
- }
- /**
- * @brief Atomically set a bit.
- *
- * Atomically set bit number @a bit of @a target and return its old value.
- * The target may be a single atomic variable or an array of them.
- *
- * @param target Address of atomic variable or array.
- * @param bit Bit number (starting from 0).
- *
- * @return 1 if the bit was set, 0 if it wasn't.
- */
- static inline int bt_mesh_atomic_test_and_set_bit(bt_mesh_atomic_t *target, int bit)
- {
- bt_mesh_atomic_val_t mask = BLE_MESH_ATOMIC_MASK(bit);
- bt_mesh_atomic_val_t old;
- old = bt_mesh_atomic_or(BLE_MESH_ATOMIC_ELEM(target, bit), mask);
- return (old & mask) != 0;
- }
- /**
- * @brief Atomically clear a bit.
- *
- * Atomically clear bit number @a bit of @a target.
- * The target may be a single atomic variable or an array of them.
- *
- * @param target Address of atomic variable or array.
- * @param bit Bit number (starting from 0).
- *
- * @return N/A
- */
- static inline void bt_mesh_atomic_clear_bit(bt_mesh_atomic_t *target, int bit)
- {
- bt_mesh_atomic_val_t mask = BLE_MESH_ATOMIC_MASK(bit);
- (void)bt_mesh_atomic_and(BLE_MESH_ATOMIC_ELEM(target, bit), ~mask);
- }
- /**
- * @brief Atomically set a bit.
- *
- * Atomically set bit number @a bit of @a target.
- * The target may be a single atomic variable or an array of them.
- *
- * @param target Address of atomic variable or array.
- * @param bit Bit number (starting from 0).
- *
- * @return N/A
- */
- static inline void bt_mesh_atomic_set_bit(bt_mesh_atomic_t *target, int bit)
- {
- bt_mesh_atomic_val_t mask = BLE_MESH_ATOMIC_MASK(bit);
- (void)bt_mesh_atomic_or(BLE_MESH_ATOMIC_ELEM(target, bit), mask);
- }
- /**
- * @brief Atomically set a bit to a given value.
- *
- * Atomically set bit number @a bit of @a target to value @a val.
- * The target may be a single atomic variable or an array of them.
- *
- * @param target Address of atomic variable or array.
- * @param bit Bit number (starting from 0).
- * @param val true for 1, false for 0.
- *
- * @return N/A
- */
- static inline void bt_mesh_atomic_set_bit_to(bt_mesh_atomic_t *target, int bit, bool val)
- {
- bt_mesh_atomic_val_t mask = BLE_MESH_ATOMIC_MASK(bit);
- if (val) {
- (void)bt_mesh_atomic_or(BLE_MESH_ATOMIC_ELEM(target, bit), mask);
- } else {
- (void)bt_mesh_atomic_and(BLE_MESH_ATOMIC_ELEM(target, bit), ~mask);
- }
- }
- /**
- * @}
- */
- #ifdef __cplusplus
- }
- #endif
- #endif /* _BLE_MESH_ATOMIC_H_ */
|