|
6 | 6 |
|
7 | 7 | #include <stdint.h>
|
8 | 8 | #include <zephyr/logging/log.h>
|
| 9 | +#include <zephyr/sys/atomic.h> |
9 | 10 | #include <nrf_error.h>
|
10 | 11 | #include <ble_gap.h>
|
11 | 12 | #include <ble_err.h>
|
|
20 | 21 |
|
21 | 22 | LOG_MODULE_REGISTER(gatt_cache_manager, CONFIG_PEER_MANAGER_LOG_LEVEL);
|
22 | 23 |
|
| 24 | +#define NRF_MTX_LOCKED 1 |
| 25 | +#define NRF_MTX_UNLOCKED 0 |
| 26 | + |
| 27 | +typedef atomic_t nrf_mtx_t; |
| 28 | + |
| 29 | +/** |
| 30 | + * @brief Initialize mutex. |
| 31 | + * |
| 32 | + * This function _must_ be called before nrf_mtx_trylock() and nrf_mtx_unlock() functions. |
| 33 | + * |
| 34 | + * @param[in, out] p_mtx The mutex to be initialized. |
| 35 | + */ |
| 36 | +__STATIC_INLINE void nrf_mtx_init(nrf_mtx_t *p_mtx) |
| 37 | +{ |
| 38 | + ASSERT(p_mtx != NULL); |
| 39 | + |
| 40 | + atomic_set(p_mtx, NRF_MTX_UNLOCKED); |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * @brief Try to lock a mutex. |
| 45 | + * |
| 46 | + * If the mutex is already held by another context, this function will return immediately. |
| 47 | + * |
| 48 | + * @param[in, out] p_mtx The mutex to be locked. |
| 49 | + * @return true if lock was acquired, false if not |
| 50 | + */ |
| 51 | +__STATIC_INLINE bool nrf_mtx_trylock(nrf_mtx_t *p_mtx) |
| 52 | +{ |
| 53 | + ASSERT(p_mtx != NULL); |
| 54 | + |
| 55 | + return atomic_cas(p_mtx, NRF_MTX_UNLOCKED, NRF_MTX_LOCKED); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * @brief Unlock a mutex. |
| 60 | + * |
| 61 | + * This function _must_ only be called when holding the lock. Unlocking a mutex which you do not |
| 62 | + * hold will give undefined behavior. |
| 63 | + * |
| 64 | + * @note Unlock must happen from the same context as the one used to lock the mutex. |
| 65 | + * |
| 66 | + * @param[in, out] p_mtx The mutex to be unlocked. |
| 67 | + */ |
| 68 | +__STATIC_INLINE void nrf_mtx_unlock(nrf_mtx_t *p_mtx) |
| 69 | +{ |
| 70 | + ASSERT(p_mtx != NULL); |
| 71 | + ASSERT(*p_mtx == NRF_MTX_LOCKED); |
| 72 | + |
| 73 | + atomic_set(p_mtx, NRF_MTX_UNLOCKED); |
| 74 | +} |
| 75 | + |
23 | 76 | /* The number of registered event handlers. */
|
24 | 77 | #define GCM_EVENT_HANDLERS_CNT ARRAY_SIZE(m_evt_handlers)
|
25 | 78 |
|
@@ -72,7 +125,8 @@ static int m_flag_car_handle_queried;
|
72 | 125 | static int m_flag_car_value_queried;
|
73 | 126 |
|
74 | 127 | #ifdef CONFIG_PM_SERVICE_CHANGED_ENABLED
|
75 |
| -STATIC_ASSERT(CONFIG_PM_SERVICE_CHANGED_ENABLED || !NRF_SDH_BLE_SERVICE_CHANGED, |
| 128 | +BUILD_ASSERT(IS_ENABLED(CONFIG_PM_SERVICE_CHANGED_ENABLED) || |
| 129 | + !IS_ENABLED(CONFIG_NRF_SDH_BLE_SERVICE_CHANGED), |
76 | 130 | "CONFIG_PM_SERVICE_CHANGED_ENABLED should be enabled "
|
77 | 131 | "if NRF_SDH_BLE_SERVICE_CHANGED is enabled.");
|
78 | 132 | #else
|
|
0 commit comments