diff --git a/arch/arm/include/kernel_arch_data.h b/arch/arm/include/kernel_arch_data.h index ec344d74b17..3f570984bb4 100644 --- a/arch/arm/include/kernel_arch_data.h +++ b/arch/arm/include/kernel_arch_data.h @@ -34,7 +34,7 @@ extern "C" { #include #include #include -#include +#include #endif #ifndef _ASMLANGUAGE diff --git a/drivers/adc/adc_context.h b/drivers/adc/adc_context.h index 6815def3e2b..bde1062cae9 100644 --- a/drivers/adc/adc_context.h +++ b/drivers/adc/adc_context.h @@ -9,7 +9,7 @@ #define ZEPHYR_DRIVERS_ADC_ADC_CONTEXT_H_ #include -#include +#include #ifdef __cplusplus extern "C" { diff --git a/drivers/can/can_mcux_flexcan.c b/drivers/can/can_mcux_flexcan.c index c2e85db5612..9cbeff62515 100644 --- a/drivers/can/can_mcux_flexcan.c +++ b/drivers/can/can_mcux_flexcan.c @@ -5,7 +5,7 @@ */ #include -#include +#include #include #include #include diff --git a/drivers/clock_control/nrf_power_clock.c b/drivers/clock_control/nrf_power_clock.c index 3a5f0238331..592d064826f 100644 --- a/drivers/clock_control/nrf_power_clock.c +++ b/drivers/clock_control/nrf_power_clock.c @@ -7,7 +7,7 @@ #include #include -#include +#include #include #include #include diff --git a/drivers/console/uart_console.c b/drivers/console/uart_console.c index a16cd97abd6..e0f62458281 100644 --- a/drivers/console/uart_console.c +++ b/drivers/console/uart_console.c @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include #ifdef CONFIG_UART_CONSOLE_MCUMGR #include "mgmt/serial.h" diff --git a/drivers/entropy/entropy_nrf5.c b/drivers/entropy/entropy_nrf5.c index fe1e87f2a7e..61026cf4e1b 100644 --- a/drivers/entropy/entropy_nrf5.c +++ b/drivers/entropy/entropy_nrf5.c @@ -6,7 +6,7 @@ */ #include -#include +#include #include #include "nrf_rng.h" diff --git a/drivers/ieee802154/ieee802154_cc1200.h b/drivers/ieee802154/ieee802154_cc1200.h index b3ad092ff24..697d7c43352 100644 --- a/drivers/ieee802154/ieee802154_cc1200.h +++ b/drivers/ieee802154/ieee802154_cc1200.h @@ -10,7 +10,7 @@ #define ZEPHYR_DRIVERS_IEEE802154_IEEE802154_CC1200_H_ #include -#include +#include #include #include diff --git a/drivers/ieee802154/ieee802154_cc2520.h b/drivers/ieee802154/ieee802154_cc2520.h index cdf05b53982..6f68f0b7b39 100644 --- a/drivers/ieee802154/ieee802154_cc2520.h +++ b/drivers/ieee802154/ieee802154_cc2520.h @@ -10,7 +10,7 @@ #define ZEPHYR_DRIVERS_IEEE802154_IEEE802154_CC2520_H_ #include -#include +#include #include #include diff --git a/drivers/ieee802154/ieee802154_mcr20a.h b/drivers/ieee802154/ieee802154_mcr20a.h index a936b279664..226bdbb24ed 100644 --- a/drivers/ieee802154/ieee802154_mcr20a.h +++ b/drivers/ieee802154/ieee802154_mcr20a.h @@ -11,7 +11,7 @@ #define ZEPHYR_DRIVERS_IEEE802154_IEEE802154_MCR20A_H_ #include -#include +#include #include /* Runtime context structure diff --git a/ext/hal/nordic/nrfx_glue.h b/ext/hal/nordic/nrfx_glue.h index f33f00dcdba..57df42e6045 100644 --- a/ext/hal/nordic/nrfx_glue.h +++ b/ext/hal/nordic/nrfx_glue.h @@ -151,7 +151,7 @@ extern "C" { //------------------------------------------------------------------------------ -#include +#include /** @brief Atomic 32-bit unsigned type. */ #define nrfx_atomic_t atomic_t diff --git a/include/atomic.h b/include/atomic.h index 1596d60e91c..63124006017 100644 --- a/include/atomic.h +++ b/include/atomic.h @@ -1,470 +1,15 @@ -/* atomic operations */ - /* - * Copyright (c) 1997-2015, Wind River Systems, Inc. + * Copyright (c) 2019 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ - #ifndef ZEPHYR_INCLUDE_ATOMIC_H_ #define ZEPHYR_INCLUDE_ATOMIC_H_ -#include -#include - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -typedef int atomic_t; -typedef atomic_t atomic_val_t; - -/** - * @defgroup atomic_apis Atomic Services APIs - * @ingroup kernel_apis - * @{ - */ - -/** - * @brief Atomic compare-and-set. - * - * This routine performs an atomic compare-and-set on @a target. If the current - * value of @a target equals @a old_value, @a target is set to @a new_value. - * If the current value of @a target does not equal @a old_value, @a target - * is left unchanged. - * - * @param target Address of atomic variable. - * @param old_value Original value to compare against. - * @param new_value New value to store. - * @return true if @a new_value is written, false otherwise. - */ -#ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN -static inline bool atomic_cas(atomic_t *target, atomic_val_t old_value, - atomic_val_t new_value) -{ - return __atomic_compare_exchange_n(target, &old_value, new_value, - 0, __ATOMIC_SEQ_CST, - __ATOMIC_SEQ_CST); -} -#elif defined(CONFIG_ATOMIC_OPERATIONS_C) -__syscall int atomic_cas(atomic_t *target, atomic_val_t old_value, - atomic_val_t new_value); - -#else -extern int atomic_cas(atomic_t *target, atomic_val_t old_value, - atomic_val_t new_value); -#endif - -/** - * - * @brief Atomic addition. - * - * This routine performs an atomic addition on @a target. - * - * @param target Address of atomic variable. - * @param value Value to add. - * - * @return Previous value of @a target. - */ -#ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN -static inline atomic_val_t atomic_add(atomic_t *target, atomic_val_t value) -{ - return __atomic_fetch_add(target, value, __ATOMIC_SEQ_CST); -} -#elif defined(CONFIG_ATOMIC_OPERATIONS_C) -__syscall atomic_val_t atomic_add(atomic_t *target, atomic_val_t value); -#else -extern atomic_val_t atomic_add(atomic_t *target, atomic_val_t value); -#endif - -/** - * - * @brief Atomic subtraction. - * - * This routine performs an atomic subtraction on @a target. - * - * @param target Address of atomic variable. - * @param value Value to subtract. - * - * @return Previous value of @a target. - */ -#ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN -static inline atomic_val_t atomic_sub(atomic_t *target, atomic_val_t value) -{ - return __atomic_fetch_sub(target, value, __ATOMIC_SEQ_CST); -} -#elif defined(CONFIG_ATOMIC_OPERATIONS_C) -__syscall atomic_val_t atomic_sub(atomic_t *target, atomic_val_t value); -#else -extern atomic_val_t atomic_sub(atomic_t *target, atomic_val_t value); -#endif - -/** - * - * @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. - */ -#if defined(CONFIG_ATOMIC_OPERATIONS_BUILTIN) || defined (CONFIG_ATOMIC_OPERATIONS_C) -static inline atomic_val_t atomic_inc(atomic_t *target) -{ - return atomic_add(target, 1); -} -#else -extern atomic_val_t atomic_inc(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. - */ -#if defined(CONFIG_ATOMIC_OPERATIONS_BUILTIN) || defined (CONFIG_ATOMIC_OPERATIONS_C) -static inline atomic_val_t atomic_dec(atomic_t *target) -{ - return atomic_sub(target, 1); -} -#else -extern atomic_val_t atomic_dec(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 atomic_val_t atomic_get(const atomic_t *target) -{ - return __atomic_load_n(target, __ATOMIC_SEQ_CST); -} -#else -extern atomic_val_t atomic_get(const 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 atomic_val_t atomic_set(atomic_t *target, 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); -} -#elif defined(CONFIG_ATOMIC_OPERATIONS_C) -__syscall atomic_val_t atomic_set(atomic_t *target, atomic_val_t value); -#else -extern atomic_val_t atomic_set(atomic_t *target, atomic_val_t value); -#endif - -/** - * - * @brief Atomic clear. - * - * This routine atomically sets @a target to zero and returns its previous - * value. (Hence, it is equivalent to atomic_set(target, 0).) - * - * @param target Address of atomic variable. - * - * @return Previous value of @a target. - */ -#if defined(CONFIG_ATOMIC_OPERATIONS_BUILTIN) || defined (CONFIG_ATOMIC_OPERATIONS_C) -static inline atomic_val_t atomic_clear(atomic_t *target) -{ - return atomic_set(target, 0); -} -#else -extern atomic_val_t atomic_clear(atomic_t *target); -#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 atomic_val_t atomic_or(atomic_t *target, atomic_val_t value) -{ - return __atomic_fetch_or(target, value, __ATOMIC_SEQ_CST); -} -#elif defined(CONFIG_ATOMIC_OPERATIONS_C) -__syscall atomic_val_t atomic_or(atomic_t *target, atomic_val_t value); - -#else -extern atomic_val_t atomic_or(atomic_t *target, atomic_val_t value); -#endif - -/** - * - * @brief Atomic bitwise exclusive OR (XOR). - * - * This routine atomically sets @a target to the bitwise exclusive OR (XOR) of - * @a target and @a value. - * - * @param target Address of atomic variable. - * @param value Value to XOR - * - * @return Previous value of @a target. - */ -#ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN -static inline atomic_val_t atomic_xor(atomic_t *target, atomic_val_t value) -{ - return __atomic_fetch_xor(target, value, __ATOMIC_SEQ_CST); -} -#elif defined(CONFIG_ATOMIC_OPERATIONS_C) -__syscall atomic_val_t atomic_xor(atomic_t *target, atomic_val_t value); -#else -extern atomic_val_t atomic_xor(atomic_t *target, 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 atomic_val_t atomic_and(atomic_t *target, atomic_val_t value) -{ - return __atomic_fetch_and(target, value, __ATOMIC_SEQ_CST); -} -#elif defined(CONFIG_ATOMIC_OPERATIONS_C) -__syscall atomic_val_t atomic_and(atomic_t *target, atomic_val_t value); -#else -extern atomic_val_t atomic_and(atomic_t *target, atomic_val_t value); -#endif - -/** - * - * @brief Atomic bitwise NAND. - * - * This routine atomically sets @a target to the bitwise NAND of @a target - * and @a value. (This operation is equivalent to target = ~(target & value).) - * - * @param target Address of atomic variable. - * @param value Value to NAND. - * - * @return Previous value of @a target. - */ -#ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN -static inline atomic_val_t atomic_nand(atomic_t *target, atomic_val_t value) -{ - return __atomic_fetch_nand(target, value, __ATOMIC_SEQ_CST); -} -#elif defined(CONFIG_ATOMIC_OPERATIONS_C) -__syscall atomic_val_t atomic_nand(atomic_t *target, atomic_val_t value); -#else -extern atomic_val_t atomic_nand(atomic_t *target, atomic_val_t value); -#endif - - -/** - * @brief Initialize an atomic variable. - * - * This macro can be used to initialize an atomic variable. For example, - * @code atomic_t my_var = ATOMIC_INIT(75); @endcode - * - * @param i Value to assign to atomic variable. - */ -#define ATOMIC_INIT(i) (i) - -/** - * @cond INTERNAL_HIDDEN - */ - -#define ATOMIC_BITS (sizeof(atomic_val_t) * 8) -#define ATOMIC_MASK(bit) (1 << ((u32_t)(bit) & (ATOMIC_BITS - 1))) -#define ATOMIC_ELEM(addr, bit) ((addr) + ((bit) / 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 ATOMIC_DEFINE(name, num_bits) \ - atomic_t name[1 + ((num_bits) - 1) / 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 true if the bit was set, false if it wasn't. - */ -static inline bool atomic_test_bit(const atomic_t *target, int bit) -{ - atomic_val_t val = atomic_get(ATOMIC_ELEM(target, bit)); - - return (1 & (val >> (bit & (ATOMIC_BITS - 1)))) != 0; -} - -/** - * @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 true if the bit was set, false if it wasn't. - */ -static inline bool atomic_test_and_clear_bit(atomic_t *target, int bit) -{ - atomic_val_t mask = ATOMIC_MASK(bit); - atomic_val_t old; - - old = atomic_and(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 true if the bit was set, false if it wasn't. - */ -static inline bool atomic_test_and_set_bit(atomic_t *target, int bit) -{ - atomic_val_t mask = ATOMIC_MASK(bit); - atomic_val_t old; - - old = atomic_or(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 atomic_clear_bit(atomic_t *target, int bit) -{ - atomic_val_t mask = ATOMIC_MASK(bit); - - (void)atomic_and(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 atomic_set_bit(atomic_t *target, int bit) -{ - atomic_val_t mask = ATOMIC_MASK(bit); - - (void)atomic_or(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 atomic_set_bit_to(atomic_t *target, int bit, bool val) -{ - atomic_val_t mask = ATOMIC_MASK(bit); - - if (val) { - (void)atomic_or(ATOMIC_ELEM(target, bit), mask); - } else { - (void)atomic_and(ATOMIC_ELEM(target, bit), ~mask); - } -} - -#ifdef CONFIG_ATOMIC_OPERATIONS_C -#include +#ifndef CONFIG_COMPAT_INCLUDES +#warning "This header file has moved, include instead." #endif -/** - * @} - */ -#ifdef __cplusplus -} -#endif +#include #endif /* ZEPHYR_INCLUDE_ATOMIC_H_ */ diff --git a/include/bluetooth/l2cap.h b/include/bluetooth/l2cap.h index 07a441c1027..859399d2849 100644 --- a/include/bluetooth/l2cap.h +++ b/include/bluetooth/l2cap.h @@ -21,7 +21,7 @@ extern "C" { #endif -#include +#include #include #include #include diff --git a/include/kernel_includes.h b/include/kernel_includes.h index 3240d390166..0d4c4fcc6e9 100644 --- a/include/kernel_includes.h +++ b/include/kernel_includes.h @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/include/logging/log_msg.h b/include/logging/log_msg.h index 9ea31e639f3..eceb809a242 100644 --- a/include/logging/log_msg.h +++ b/include/logging/log_msg.h @@ -7,7 +7,7 @@ #define ZEPHYR_INCLUDE_LOGGING_LOG_MSG_H_ #include -#include +#include #include #include diff --git a/include/misc/mutex.h b/include/misc/mutex.h index d099d07df29..f2c26c6642e 100644 --- a/include/misc/mutex.h +++ b/include/misc/mutex.h @@ -17,7 +17,7 @@ */ #ifdef CONFIG_USERSPACE -#include +#include #include struct sys_mutex { diff --git a/include/net/ethernet.h b/include/net/ethernet.h index 2bc41da1342..b02b324279b 100644 --- a/include/net/ethernet.h +++ b/include/net/ethernet.h @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include diff --git a/include/net/net_context.h b/include/net/net_context.h index 6d982547cb7..88490c85c3c 100644 --- a/include/net/net_context.h +++ b/include/net/net_context.h @@ -21,7 +21,7 @@ */ #include -#include +#include #include #include diff --git a/include/shell/shell_log_backend.h b/include/shell/shell_log_backend.h index 01fdb1ed081..aada0d75749 100644 --- a/include/shell/shell_log_backend.h +++ b/include/shell/shell_log_backend.h @@ -10,7 +10,7 @@ #include #include #include -#include +#include #ifdef __cplusplus extern "C" { #endif diff --git a/include/shell/shell_uart.h b/include/shell/shell_uart.h index fc9edfb8d34..8748ec0088d 100644 --- a/include/shell/shell_uart.h +++ b/include/shell/shell_uart.h @@ -9,7 +9,7 @@ #include #include -#include +#include #ifdef CONFIG_MCUMGR_SMP_SHELL #include "mgmt/smp_shell.h" #endif diff --git a/include/spinlock.h b/include/spinlock.h index 33875d1f378..74e67ebb3a1 100644 --- a/include/spinlock.h +++ b/include/spinlock.h @@ -6,7 +6,7 @@ #ifndef ZEPHYR_INCLUDE_SPINLOCK_H_ #define ZEPHYR_INCLUDE_SPINLOCK_H_ -#include +#include /* These stubs aren't provided by the mocking framework, and I can't * find a proper place to put them as mocking seems not to have a diff --git a/include/sys/atomic.h b/include/sys/atomic.h new file mode 100644 index 00000000000..f625410c478 --- /dev/null +++ b/include/sys/atomic.h @@ -0,0 +1,470 @@ +/* atomic operations */ + +/* + * Copyright (c) 1997-2015, Wind River Systems, Inc. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_INCLUDE_SYS_ATOMIC_H_ +#define ZEPHYR_INCLUDE_SYS_ATOMIC_H_ + +#include +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef int atomic_t; +typedef atomic_t atomic_val_t; + +/** + * @defgroup atomic_apis Atomic Services APIs + * @ingroup kernel_apis + * @{ + */ + +/** + * @brief Atomic compare-and-set. + * + * This routine performs an atomic compare-and-set on @a target. If the current + * value of @a target equals @a old_value, @a target is set to @a new_value. + * If the current value of @a target does not equal @a old_value, @a target + * is left unchanged. + * + * @param target Address of atomic variable. + * @param old_value Original value to compare against. + * @param new_value New value to store. + * @return true if @a new_value is written, false otherwise. + */ +#ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN +static inline bool atomic_cas(atomic_t *target, atomic_val_t old_value, + atomic_val_t new_value) +{ + return __atomic_compare_exchange_n(target, &old_value, new_value, + 0, __ATOMIC_SEQ_CST, + __ATOMIC_SEQ_CST); +} +#elif defined(CONFIG_ATOMIC_OPERATIONS_C) +__syscall int atomic_cas(atomic_t *target, atomic_val_t old_value, + atomic_val_t new_value); + +#else +extern int atomic_cas(atomic_t *target, atomic_val_t old_value, + atomic_val_t new_value); +#endif + +/** + * + * @brief Atomic addition. + * + * This routine performs an atomic addition on @a target. + * + * @param target Address of atomic variable. + * @param value Value to add. + * + * @return Previous value of @a target. + */ +#ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN +static inline atomic_val_t atomic_add(atomic_t *target, atomic_val_t value) +{ + return __atomic_fetch_add(target, value, __ATOMIC_SEQ_CST); +} +#elif defined(CONFIG_ATOMIC_OPERATIONS_C) +__syscall atomic_val_t atomic_add(atomic_t *target, atomic_val_t value); +#else +extern atomic_val_t atomic_add(atomic_t *target, atomic_val_t value); +#endif + +/** + * + * @brief Atomic subtraction. + * + * This routine performs an atomic subtraction on @a target. + * + * @param target Address of atomic variable. + * @param value Value to subtract. + * + * @return Previous value of @a target. + */ +#ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN +static inline atomic_val_t atomic_sub(atomic_t *target, atomic_val_t value) +{ + return __atomic_fetch_sub(target, value, __ATOMIC_SEQ_CST); +} +#elif defined(CONFIG_ATOMIC_OPERATIONS_C) +__syscall atomic_val_t atomic_sub(atomic_t *target, atomic_val_t value); +#else +extern atomic_val_t atomic_sub(atomic_t *target, atomic_val_t value); +#endif + +/** + * + * @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. + */ +#if defined(CONFIG_ATOMIC_OPERATIONS_BUILTIN) || defined (CONFIG_ATOMIC_OPERATIONS_C) +static inline atomic_val_t atomic_inc(atomic_t *target) +{ + return atomic_add(target, 1); +} +#else +extern atomic_val_t atomic_inc(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. + */ +#if defined(CONFIG_ATOMIC_OPERATIONS_BUILTIN) || defined (CONFIG_ATOMIC_OPERATIONS_C) +static inline atomic_val_t atomic_dec(atomic_t *target) +{ + return atomic_sub(target, 1); +} +#else +extern atomic_val_t atomic_dec(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 atomic_val_t atomic_get(const atomic_t *target) +{ + return __atomic_load_n(target, __ATOMIC_SEQ_CST); +} +#else +extern atomic_val_t atomic_get(const 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 atomic_val_t atomic_set(atomic_t *target, 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); +} +#elif defined(CONFIG_ATOMIC_OPERATIONS_C) +__syscall atomic_val_t atomic_set(atomic_t *target, atomic_val_t value); +#else +extern atomic_val_t atomic_set(atomic_t *target, atomic_val_t value); +#endif + +/** + * + * @brief Atomic clear. + * + * This routine atomically sets @a target to zero and returns its previous + * value. (Hence, it is equivalent to atomic_set(target, 0).) + * + * @param target Address of atomic variable. + * + * @return Previous value of @a target. + */ +#if defined(CONFIG_ATOMIC_OPERATIONS_BUILTIN) || defined (CONFIG_ATOMIC_OPERATIONS_C) +static inline atomic_val_t atomic_clear(atomic_t *target) +{ + return atomic_set(target, 0); +} +#else +extern atomic_val_t atomic_clear(atomic_t *target); +#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 atomic_val_t atomic_or(atomic_t *target, atomic_val_t value) +{ + return __atomic_fetch_or(target, value, __ATOMIC_SEQ_CST); +} +#elif defined(CONFIG_ATOMIC_OPERATIONS_C) +__syscall atomic_val_t atomic_or(atomic_t *target, atomic_val_t value); + +#else +extern atomic_val_t atomic_or(atomic_t *target, atomic_val_t value); +#endif + +/** + * + * @brief Atomic bitwise exclusive OR (XOR). + * + * This routine atomically sets @a target to the bitwise exclusive OR (XOR) of + * @a target and @a value. + * + * @param target Address of atomic variable. + * @param value Value to XOR + * + * @return Previous value of @a target. + */ +#ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN +static inline atomic_val_t atomic_xor(atomic_t *target, atomic_val_t value) +{ + return __atomic_fetch_xor(target, value, __ATOMIC_SEQ_CST); +} +#elif defined(CONFIG_ATOMIC_OPERATIONS_C) +__syscall atomic_val_t atomic_xor(atomic_t *target, atomic_val_t value); +#else +extern atomic_val_t atomic_xor(atomic_t *target, 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 atomic_val_t atomic_and(atomic_t *target, atomic_val_t value) +{ + return __atomic_fetch_and(target, value, __ATOMIC_SEQ_CST); +} +#elif defined(CONFIG_ATOMIC_OPERATIONS_C) +__syscall atomic_val_t atomic_and(atomic_t *target, atomic_val_t value); +#else +extern atomic_val_t atomic_and(atomic_t *target, atomic_val_t value); +#endif + +/** + * + * @brief Atomic bitwise NAND. + * + * This routine atomically sets @a target to the bitwise NAND of @a target + * and @a value. (This operation is equivalent to target = ~(target & value).) + * + * @param target Address of atomic variable. + * @param value Value to NAND. + * + * @return Previous value of @a target. + */ +#ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN +static inline atomic_val_t atomic_nand(atomic_t *target, atomic_val_t value) +{ + return __atomic_fetch_nand(target, value, __ATOMIC_SEQ_CST); +} +#elif defined(CONFIG_ATOMIC_OPERATIONS_C) +__syscall atomic_val_t atomic_nand(atomic_t *target, atomic_val_t value); +#else +extern atomic_val_t atomic_nand(atomic_t *target, atomic_val_t value); +#endif + + +/** + * @brief Initialize an atomic variable. + * + * This macro can be used to initialize an atomic variable. For example, + * @code atomic_t my_var = ATOMIC_INIT(75); @endcode + * + * @param i Value to assign to atomic variable. + */ +#define ATOMIC_INIT(i) (i) + +/** + * @cond INTERNAL_HIDDEN + */ + +#define ATOMIC_BITS (sizeof(atomic_val_t) * 8) +#define ATOMIC_MASK(bit) (1 << ((u32_t)(bit) & (ATOMIC_BITS - 1))) +#define ATOMIC_ELEM(addr, bit) ((addr) + ((bit) / 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 ATOMIC_DEFINE(name, num_bits) \ + atomic_t name[1 + ((num_bits) - 1) / 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 true if the bit was set, false if it wasn't. + */ +static inline bool atomic_test_bit(const atomic_t *target, int bit) +{ + atomic_val_t val = atomic_get(ATOMIC_ELEM(target, bit)); + + return (1 & (val >> (bit & (ATOMIC_BITS - 1)))) != 0; +} + +/** + * @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 true if the bit was set, false if it wasn't. + */ +static inline bool atomic_test_and_clear_bit(atomic_t *target, int bit) +{ + atomic_val_t mask = ATOMIC_MASK(bit); + atomic_val_t old; + + old = atomic_and(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 true if the bit was set, false if it wasn't. + */ +static inline bool atomic_test_and_set_bit(atomic_t *target, int bit) +{ + atomic_val_t mask = ATOMIC_MASK(bit); + atomic_val_t old; + + old = atomic_or(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 atomic_clear_bit(atomic_t *target, int bit) +{ + atomic_val_t mask = ATOMIC_MASK(bit); + + (void)atomic_and(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 atomic_set_bit(atomic_t *target, int bit) +{ + atomic_val_t mask = ATOMIC_MASK(bit); + + (void)atomic_or(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 atomic_set_bit_to(atomic_t *target, int bit, bool val) +{ + atomic_val_t mask = ATOMIC_MASK(bit); + + if (val) { + (void)atomic_or(ATOMIC_ELEM(target, bit), mask); + } else { + (void)atomic_and(ATOMIC_ELEM(target, bit), ~mask); + } +} + +#ifdef CONFIG_ATOMIC_OPERATIONS_C +#include +#endif +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_SYS_ATOMIC_H_ */ diff --git a/kernel/atomic_c.c b/kernel/atomic_c.c index 954388ad209..f24b3ae0150 100644 --- a/kernel/atomic_c.c +++ b/kernel/atomic_c.c @@ -18,7 +18,7 @@ * (originally from x86's atomic.c) */ -#include +#include #include #include #include diff --git a/kernel/device.c b/kernel/device.c index 03dc2868c33..604dfd56488 100644 --- a/kernel/device.c +++ b/kernel/device.c @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include extern struct device __device_init_start[]; diff --git a/kernel/include/kernel_structs.h b/kernel/include/kernel_structs.h index 1095538cc1c..a1c73d75375 100644 --- a/kernel/include/kernel_structs.h +++ b/kernel/include/kernel_structs.h @@ -10,7 +10,7 @@ #include #if !defined(_ASMLANGUAGE) -#include +#include #include #include #include diff --git a/kernel/thread.c b/kernel/thread.c index 6a7aaa1fce7..90d0ade87b9 100644 --- a/kernel/thread.c +++ b/kernel/thread.c @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/lib/cmsis_rtos_v1/cmsis_thread.c b/lib/cmsis_rtos_v1/cmsis_thread.c index 2b50ea82e90..3972010f20c 100644 --- a/lib/cmsis_rtos_v1/cmsis_thread.c +++ b/lib/cmsis_rtos_v1/cmsis_thread.c @@ -5,7 +5,7 @@ */ #include -#include +#include #include #define TOTAL_CMSIS_THREAD_PRIORITIES (osPriorityRealtime - osPriorityIdle + 1) diff --git a/lib/cmsis_rtos_v2/thread.c b/lib/cmsis_rtos_v2/thread.c index 19cce685e03..7441e348f4a 100644 --- a/lib/cmsis_rtos_v2/thread.c +++ b/lib/cmsis_rtos_v2/thread.c @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include "wrapper.h" diff --git a/lib/posix/mqueue.c b/lib/posix/mqueue.c index 155077194d4..47bee1bbc4a 100644 --- a/lib/posix/mqueue.c +++ b/lib/posix/mqueue.c @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include diff --git a/lib/posix/pthread.c b/lib/posix/pthread.c index a2138bbccd3..4d3fdb16c24 100644 --- a/lib/posix/pthread.c +++ b/lib/posix/pthread.c @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include #include diff --git a/subsys/bluetooth/common/rpa.c b/subsys/bluetooth/common/rpa.c index c96fd62557d..0e97c52d4e1 100644 --- a/subsys/bluetooth/common/rpa.c +++ b/subsys/bluetooth/common/rpa.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/subsys/bluetooth/controller/hci/hci.c b/subsys/bluetooth/controller/hci/hci.c index c52098006b8..3b5ec441e20 100644 --- a/subsys/bluetooth/controller/hci/hci.c +++ b/subsys/bluetooth/controller/hci/hci.c @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/subsys/bluetooth/controller/hci/hci_driver.c b/subsys/bluetooth/controller/hci/hci_driver.c index 6670f8df66e..05ee9eb3684 100644 --- a/subsys/bluetooth/controller/hci/hci_driver.c +++ b/subsys/bluetooth/controller/hci/hci_driver.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include diff --git a/subsys/bluetooth/host/a2dp.c b/subsys/bluetooth/host/a2dp.c index 70fec4ff76a..c45b5a4e476 100644 --- a/subsys/bluetooth/host/a2dp.c +++ b/subsys/bluetooth/host/a2dp.c @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/subsys/bluetooth/host/att.c b/subsys/bluetooth/host/att.c index 7d4f339e89d..c00b8a92d1e 100644 --- a/subsys/bluetooth/host/att.c +++ b/subsys/bluetooth/host/att.c @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include diff --git a/subsys/bluetooth/host/avdtp.c b/subsys/bluetooth/host/avdtp.c index 0d354ad92f3..db20944c8cc 100644 --- a/subsys/bluetooth/host/avdtp.c +++ b/subsys/bluetooth/host/avdtp.c @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include diff --git a/subsys/bluetooth/host/conn.c b/subsys/bluetooth/host/conn.c index 57c7562a3c9..95c545d08a2 100644 --- a/subsys/bluetooth/host/conn.c +++ b/subsys/bluetooth/host/conn.c @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/subsys/bluetooth/host/gatt.c b/subsys/bluetooth/host/gatt.c index 49528eadfb1..a959c3ab7be 100644 --- a/subsys/bluetooth/host/gatt.c +++ b/subsys/bluetooth/host/gatt.c @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include diff --git a/subsys/bluetooth/host/hci_core.c b/subsys/bluetooth/host/hci_core.c index 60320166a73..32e56ca66e2 100644 --- a/subsys/bluetooth/host/hci_core.c +++ b/subsys/bluetooth/host/hci_core.c @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/subsys/bluetooth/host/hci_ecc.c b/subsys/bluetooth/host/hci_ecc.c index 683c06412da..51e8a667f59 100644 --- a/subsys/bluetooth/host/hci_ecc.c +++ b/subsys/bluetooth/host/hci_ecc.c @@ -10,7 +10,7 @@ */ #include -#include +#include #include #include #include diff --git a/subsys/bluetooth/host/hci_raw.c b/subsys/bluetooth/host/hci_raw.c index 6a7394627ed..f81d4e8ed3a 100644 --- a/subsys/bluetooth/host/hci_raw.c +++ b/subsys/bluetooth/host/hci_raw.c @@ -7,7 +7,7 @@ */ #include -#include +#include #include #include diff --git a/subsys/bluetooth/host/hfp_hf.c b/subsys/bluetooth/host/hfp_hf.c index d3b9c84dfb8..6e23280233f 100644 --- a/subsys/bluetooth/host/hfp_hf.c +++ b/subsys/bluetooth/host/hfp_hf.c @@ -7,7 +7,7 @@ */ #include #include -#include +#include #include #include #include diff --git a/subsys/bluetooth/host/keys.c b/subsys/bluetooth/host/keys.c index 7719693ae37..15f23851cf7 100644 --- a/subsys/bluetooth/host/keys.c +++ b/subsys/bluetooth/host/keys.c @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include diff --git a/subsys/bluetooth/host/keys_br.c b/subsys/bluetooth/host/keys_br.c index bb4108bdbb2..79470390428 100644 --- a/subsys/bluetooth/host/keys_br.c +++ b/subsys/bluetooth/host/keys_br.c @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include diff --git a/subsys/bluetooth/host/l2cap.c b/subsys/bluetooth/host/l2cap.c index fd3e932a1c3..c3ab76393bd 100644 --- a/subsys/bluetooth/host/l2cap.c +++ b/subsys/bluetooth/host/l2cap.c @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include diff --git a/subsys/bluetooth/host/l2cap_br.c b/subsys/bluetooth/host/l2cap_br.c index 582c6cd04f7..e37b6f494a5 100644 --- a/subsys/bluetooth/host/l2cap_br.c +++ b/subsys/bluetooth/host/l2cap_br.c @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include diff --git a/subsys/bluetooth/host/mesh/net.c b/subsys/bluetooth/host/mesh/net.c index 638d594d226..085aaeb39da 100644 --- a/subsys/bluetooth/host/mesh/net.c +++ b/subsys/bluetooth/host/mesh/net.c @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include diff --git a/subsys/bluetooth/host/mesh/prov.c b/subsys/bluetooth/host/mesh/prov.c index 03fb61432f7..f56b45561fc 100644 --- a/subsys/bluetooth/host/mesh/prov.c +++ b/subsys/bluetooth/host/mesh/prov.c @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include diff --git a/subsys/bluetooth/host/rfcomm.c b/subsys/bluetooth/host/rfcomm.c index 70b8be18a59..c44ee3bdd6d 100644 --- a/subsys/bluetooth/host/rfcomm.c +++ b/subsys/bluetooth/host/rfcomm.c @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/subsys/bluetooth/host/smp.c b/subsys/bluetooth/host/smp.c index e84ef02bea2..c1b110736f5 100644 --- a/subsys/bluetooth/host/smp.c +++ b/subsys/bluetooth/host/smp.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/subsys/bluetooth/host/smp_null.c b/subsys/bluetooth/host/smp_null.c index a2b74ef70a0..a054ba071ae 100644 --- a/subsys/bluetooth/host/smp_null.c +++ b/subsys/bluetooth/host/smp_null.c @@ -11,7 +11,7 @@ #include #include -#include +#include #include #include diff --git a/subsys/logging/log_core.c b/subsys/logging/log_core.c index c58df406d45..d694731991e 100644 --- a/subsys/logging/log_core.c +++ b/subsys/logging/log_core.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include LOG_MODULE_REGISTER(log); diff --git a/subsys/power/pm_ctrl.c b/subsys/power/pm_ctrl.c index 7bafbd459ff..1f81f026428 100644 --- a/subsys/power/pm_ctrl.c +++ b/subsys/power/pm_ctrl.c @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include "policy/pm_policy.h" #define LOG_LEVEL CONFIG_SYS_PM_LOG_LEVEL /* From power module Kconfig */ diff --git a/subsys/random/rand32_entropy_device.c b/subsys/random/rand32_entropy_device.c index 45c7b6f6a14..f1416c0443b 100644 --- a/subsys/random/rand32_entropy_device.c +++ b/subsys/random/rand32_entropy_device.c @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include +#include #include #include diff --git a/subsys/random/rand32_timer.c b/subsys/random/rand32_timer.c index 00c78a6540e..76bd228288c 100644 --- a/subsys/random/rand32_timer.c +++ b/subsys/random/rand32_timer.c @@ -17,7 +17,7 @@ #include #include #include -#include +#include #if defined(__GNUC__) diff --git a/subsys/shell/shell.c b/subsys/shell/shell.c index b4e0cee90b3..03070efadc9 100644 --- a/subsys/shell/shell.c +++ b/subsys/shell/shell.c @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include #include "shell_ops.h" diff --git a/tests/bluetooth/tester/src/gap.c b/tests/bluetooth/tester/src/gap.c index afe7747eb3a..2ab37589014 100644 --- a/tests/bluetooth/tester/src/gap.c +++ b/tests/bluetooth/tester/src/gap.c @@ -6,7 +6,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include +#include #include #include diff --git a/tests/kernel/common/src/atomic.c b/tests/kernel/common/src/atomic.c index d2aa3f2e213..a4e475b06fd 100644 --- a/tests/kernel/common/src/atomic.c +++ b/tests/kernel/common/src/atomic.c @@ -5,7 +5,7 @@ */ #include -#include +#include /** * @addtogroup kernel_common_tests diff --git a/tests/kernel/mem_pool/mem_pool_threadsafe/src/main.c b/tests/kernel/mem_pool/mem_pool_threadsafe/src/main.c index 9f59df3b670..0b5bbbc697b 100644 --- a/tests/kernel/mem_pool/mem_pool_threadsafe/src/main.c +++ b/tests/kernel/mem_pool/mem_pool_threadsafe/src/main.c @@ -5,7 +5,7 @@ */ #include -#include +#include #define THREAD_NUM 4 #define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE) #define POOL_NUM 2 diff --git a/tests/kernel/mem_slab/mslab_threadsafe/src/test_mslab_threadsafe.c b/tests/kernel/mem_slab/mslab_threadsafe/src/test_mslab_threadsafe.c index 822998e0054..2145413d689 100644 --- a/tests/kernel/mem_slab/mslab_threadsafe/src/test_mslab_threadsafe.c +++ b/tests/kernel/mem_slab/mslab_threadsafe/src/test_mslab_threadsafe.c @@ -5,7 +5,7 @@ */ #include -#include +#include #define LOOP 10 #define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE) #define THREAD_NUM 4