Skip to content

Commit cd627d6

Browse files
forx157espressif-bot
authored andcommitted
feat(ble_mesh): add cas operation for bt_mesh_atomic_val_t
1 parent ffe0de9 commit cd627d6

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

components/bt/esp_ble_mesh/common/atomic.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/*
1414
* SPDX-FileCopyrightText: 2016 Intel Corporation
1515
* SPDX-FileCopyrightText: 2011-2014 Wind River Systems, Inc.
16-
* SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
16+
* SPDX-FileContributor: 2018-2024 Espressif Systems (Shanghai) CO LTD
1717
*
1818
* SPDX-License-Identifier: Apache-2.0
1919
*/
@@ -170,4 +170,21 @@ bt_mesh_atomic_val_t bt_mesh_atomic_inc(bt_mesh_atomic_t *target)
170170
return ret;
171171
}
172172

173+
bool bt_mesh_atomic_campare_and_set(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t excepted, bt_mesh_atomic_val_t new_val)
174+
{
175+
bt_mesh_atomic_val_t ret = 0;
176+
177+
bt_mesh_atomic_lock();
178+
179+
ret = *target;
180+
if (*target == excepted) {
181+
*target = new_val;
182+
bt_mesh_atomic_unlock();
183+
return true;
184+
}
185+
186+
bt_mesh_atomic_unlock();
187+
return false;
188+
}
189+
173190
#endif /* #ifndef CONFIG_ATOMIC_OPERATIONS_BUILTIN */

components/bt/esp_ble_mesh/common/include/mesh/atomic.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,33 @@ static inline bt_mesh_atomic_val_t bt_mesh_atomic_and(bt_mesh_atomic_t *target,
147147
extern bt_mesh_atomic_val_t bt_mesh_atomic_and(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t value);
148148
#endif
149149

150+
/**
151+
* @brief Atomic CAS operation.
152+
*
153+
* This compares the contents of @a *target
154+
* with the contents of @a excepted. If equal,
155+
* the operation is a read-modify-write operation
156+
* that writes @a new_val into @a *target and return true.
157+
* If they are not equal, the operation is a read
158+
* and return false.
159+
*
160+
* @param target Address of atomic variable.
161+
* @param excepted Value of excepted.
162+
* @param new_val Write value if compare sunncess.
163+
*
164+
* @return
165+
* - true: write operation succeeded.
166+
* - false: write operation failed.
167+
*/
168+
#ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN
169+
static inline bool bt_mesh_atomic_campare_and_set(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t excepted, bt_mesh_atomic_val_t new_val)
170+
{
171+
return __atomic_compare_exchange_n(target, &excepted, &new_val, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
172+
}
173+
#else
174+
extern bool bt_mesh_atomic_campare_and_set(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t excepted, bt_mesh_atomic_val_t new_val);
175+
#endif
176+
150177
/**
151178
* @cond INTERNAL_HIDDEN
152179
*/

0 commit comments

Comments
 (0)