Skip to content

Commit c55c07a

Browse files
authored
Merge pull request #2054 from geky/critical-cas-ptr
Added cas instrinsics for pointer values
2 parents 0bc7622 + 9461991 commit c55c07a

File tree

2 files changed

+119
-31
lines changed

2 files changed

+119
-31
lines changed

hal/api/critical.h

Lines changed: 88 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#define __MBED_UTIL_CRITICAL_H__
2020

2121
#include <stdbool.h>
22+
#include <stdint.h>
23+
#include <stddef.h>
2224

2325
#ifdef __cplusplus
2426
extern "C" {
@@ -47,7 +49,7 @@ bool core_util_are_interrupts_enabled(void);
4749
* section) will be preserved on exit from the section.
4850
* 4) This implementation will currently only work on code running in privileged mode.
4951
*/
50-
void core_util_critical_section_enter();
52+
void core_util_critical_section_enter(void);
5153

5254
/** Mark the end of a critical section
5355
*
@@ -60,7 +62,7 @@ void core_util_critical_section_enter();
6062
* section) will be preserved on exit from the section.
6163
* 4) This implementation will currently only work on code running in privileged mode.
6264
*/
63-
void core_util_critical_section_exit();
65+
void core_util_critical_section_exit(void);
6466

6567
/**
6668
* Atomic compare and set. It compares the contents of a memory location to a
@@ -106,7 +108,7 @@ void core_util_critical_section_exit();
106108
*
107109
* function incr(p : pointer to int, a : int) returns int {
108110
* done = false
109-
* *value = *p // This fetch operation need not be atomic.
111+
* value = *p // This fetch operation need not be atomic.
110112
* while not done {
111113
* done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
112114
* }
@@ -159,7 +161,7 @@ bool core_util_atomic_cas_u8(uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_
159161
*
160162
* function incr(p : pointer to int, a : int) returns int {
161163
* done = false
162-
* *value = *p // This fetch operation need not be atomic.
164+
* value = *p // This fetch operation need not be atomic.
163165
* while not done {
164166
* done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
165167
* }
@@ -212,7 +214,7 @@ bool core_util_atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue, uin
212214
*
213215
* function incr(p : pointer to int, a : int) returns int {
214216
* done = false
215-
* *value = *p // This fetch operation need not be atomic.
217+
* value = *p // This fetch operation need not be atomic.
216218
* while not done {
217219
* done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
218220
* }
@@ -221,53 +223,128 @@ bool core_util_atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue, uin
221223
*/
222224
bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue);
223225

226+
/**
227+
* Atomic compare and set. It compares the contents of a memory location to a
228+
* given value and, only if they are the same, modifies the contents of that
229+
* memory location to a given new value. This is done as a single atomic
230+
* operation. The atomicity guarantees that the new value is calculated based on
231+
* up-to-date information; if the value had been updated by another thread in
232+
* the meantime, the write would fail due to a mismatched expectedCurrentValue.
233+
*
234+
* Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
235+
* you to the article on compare-and swap].
236+
*
237+
* @param ptr The target memory location.
238+
* @param[in,out] expectedCurrentValue A pointer to some location holding the
239+
* expected current value of the data being set atomically.
240+
* The computed 'desiredValue' should be a function of this current value.
241+
* @Note: This is an in-out parameter. In the
242+
* failure case of atomic_cas (where the
243+
* destination isn't set), the pointee of expectedCurrentValue is
244+
* updated with the current value.
245+
* @param[in] desiredValue The new value computed based on '*expectedCurrentValue'.
246+
*
247+
* @return true if the memory location was atomically
248+
* updated with the desired value (after verifying
249+
* that it contained the expectedCurrentValue),
250+
* false otherwise. In the failure case,
251+
* exepctedCurrentValue is updated with the new
252+
* value of the target memory location.
253+
*
254+
* pseudocode:
255+
* function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
256+
* if *p != *old {
257+
* *old = *p
258+
* return false
259+
* }
260+
* *p = new
261+
* return true
262+
* }
263+
*
264+
* @Note: In the failure case (where the destination isn't set), the value
265+
* pointed to by expectedCurrentValue is still updated with the current value.
266+
* This property helps writing concise code for the following incr:
267+
*
268+
* function incr(p : pointer to int, a : int) returns int {
269+
* done = false
270+
* value = *p // This fetch operation need not be atomic.
271+
* while not done {
272+
* done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
273+
* }
274+
* return value + a
275+
* }
276+
*/
277+
bool core_util_atomic_cas_ptr(void **ptr, void **expectedCurrentValue, void *desiredValue);
278+
224279
/**
225280
* Atomic increment.
226281
* @param valuePtr Target memory location being incremented.
227282
* @param delta The amount being incremented.
228283
* @return The new incremented value.
229284
*/
230-
uint8_t core_util_atomic_incr_u8(uint8_t * valuePtr, uint8_t delta);
285+
uint8_t core_util_atomic_incr_u8(uint8_t *valuePtr, uint8_t delta);
231286

232287
/**
233288
* Atomic increment.
234289
* @param valuePtr Target memory location being incremented.
235290
* @param delta The amount being incremented.
236291
* @return The new incremented value.
237292
*/
238-
uint16_t core_util_atomic_incr_u16(uint16_t * valuePtr, uint16_t delta);
293+
uint16_t core_util_atomic_incr_u16(uint16_t *valuePtr, uint16_t delta);
239294

240295
/**
241296
* Atomic increment.
242297
* @param valuePtr Target memory location being incremented.
243298
* @param delta The amount being incremented.
244299
* @return The new incremented value.
245300
*/
246-
uint32_t core_util_atomic_incr_u32(uint32_t * valuePtr, uint32_t delta);
301+
uint32_t core_util_atomic_incr_u32(uint32_t *valuePtr, uint32_t delta);
302+
303+
/**
304+
* Atomic increment.
305+
* @param valuePtr Target memory location being incremented.
306+
* @param delta The amount being incremented in bytes.
307+
* @return The new incremented value.
308+
*
309+
* @note The type of the pointer argument is not taken into account
310+
* and the pointer is incremented by bytes.
311+
*/
312+
void *core_util_atomic_incr_ptr(void **valuePtr, ptrdiff_t delta);
247313

248314
/**
249315
* Atomic decrement.
250316
* @param valuePtr Target memory location being decremented.
251317
* @param delta The amount being decremented.
252318
* @return The new decremented value.
253319
*/
254-
uint8_t core_util_atomic_decr_u8(uint8_t * valuePtr, uint8_t delta);
320+
uint8_t core_util_atomic_decr_u8(uint8_t *valuePtr, uint8_t delta);
255321

256322
/**
257323
* Atomic decrement.
258324
* @param valuePtr Target memory location being decremented.
259325
* @param delta The amount being decremented.
260326
* @return The new decremented value.
261327
*/
262-
uint16_t core_util_atomic_decr_u16(uint16_t * valuePtr, uint16_t delta);
328+
uint16_t core_util_atomic_decr_u16(uint16_t *valuePtr, uint16_t delta);
263329

264330
/**
265331
* Atomic decrement.
266332
* @param valuePtr Target memory location being decremented.
267333
* @param delta The amount being decremented.
268334
* @return The new decremented value.
269335
*/
270-
uint32_t core_util_atomic_decr_u32(uint32_t * valuePtr, uint32_t delta);
336+
uint32_t core_util_atomic_decr_u32(uint32_t *valuePtr, uint32_t delta);
337+
338+
/**
339+
* Atomic decrement.
340+
* @param valuePtr Target memory location being decremented.
341+
* @param delta The amount being decremented in bytes.
342+
* @return The new decremented value.
343+
*
344+
* @note The type of the pointer argument is not taken into account
345+
* and the pointer is decremented by bytes
346+
*/
347+
void *core_util_atomic_decr_ptr(void **valuePtr, ptrdiff_t delta);
271348

272349
#ifdef __cplusplus
273350
} // extern "C"

hal/common/critical.c

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@
1515
* limitations under the License.
1616
*/
1717

18-
#define __STDC_LIMIT_MACROS
19-
#include <stdint.h>
20-
#include <stddef.h>
18+
#include "critical.h"
19+
2120
#include "cmsis.h"
2221
#include "mbed_assert.h"
2322

24-
// Module include
25-
#include "critical.h"
26-
2723
#define EXCLUSIVE_ACCESS (!defined (__CORTEX_M0) && !defined (__CORTEX_M0PLUS))
2824

2925
static volatile uint32_t interrupt_enable_counter = 0;
@@ -38,7 +34,7 @@ bool core_util_are_interrupts_enabled(void)
3834
#endif
3935
}
4036

41-
void core_util_critical_section_enter()
37+
void core_util_critical_section_enter(void)
4238
{
4339
bool interrupts_disabled = !core_util_are_interrupts_enabled();
4440
__disable_irq();
@@ -63,7 +59,7 @@ void core_util_critical_section_enter()
6359
interrupt_enable_counter++;
6460
}
6561

66-
void core_util_critical_section_exit()
62+
void core_util_critical_section_exit(void)
6763
{
6864
/* If critical_section_enter has not previously been called, do nothing */
6965
if (interrupt_enable_counter) {
@@ -127,7 +123,7 @@ bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uin
127123
return !__STREXW(desiredValue, (volatile uint32_t*)ptr);
128124
}
129125

130-
uint8_t core_util_atomic_incr_u8(uint8_t * valuePtr, uint8_t delta)
126+
uint8_t core_util_atomic_incr_u8(uint8_t *valuePtr, uint8_t delta)
131127
{
132128
uint8_t newValue;
133129
do {
@@ -136,7 +132,7 @@ uint8_t core_util_atomic_incr_u8(uint8_t * valuePtr, uint8_t delta)
136132
return newValue;
137133
}
138134

139-
uint16_t core_util_atomic_incr_u16(uint16_t * valuePtr, uint16_t delta)
135+
uint16_t core_util_atomic_incr_u16(uint16_t *valuePtr, uint16_t delta)
140136
{
141137
uint16_t newValue;
142138
do {
@@ -145,7 +141,7 @@ uint16_t core_util_atomic_incr_u16(uint16_t * valuePtr, uint16_t delta)
145141
return newValue;
146142
}
147143

148-
uint32_t core_util_atomic_incr_u32(uint32_t * valuePtr, uint32_t delta)
144+
uint32_t core_util_atomic_incr_u32(uint32_t *valuePtr, uint32_t delta)
149145
{
150146
uint32_t newValue;
151147
do {
@@ -155,7 +151,7 @@ uint32_t core_util_atomic_incr_u32(uint32_t * valuePtr, uint32_t delta)
155151
}
156152

157153

158-
uint8_t core_util_atomic_decr_u8(uint8_t * valuePtr, uint8_t delta)
154+
uint8_t core_util_atomic_decr_u8(uint8_t *valuePtr, uint8_t delta)
159155
{
160156
uint8_t newValue;
161157
do {
@@ -164,7 +160,7 @@ uint8_t core_util_atomic_decr_u8(uint8_t * valuePtr, uint8_t delta)
164160
return newValue;
165161
}
166162

167-
uint16_t core_util_atomic_decr_u16(uint16_t * valuePtr, uint16_t delta)
163+
uint16_t core_util_atomic_decr_u16(uint16_t *valuePtr, uint16_t delta)
168164
{
169165
uint16_t newValue;
170166
do {
@@ -173,7 +169,7 @@ uint16_t core_util_atomic_decr_u16(uint16_t * valuePtr, uint16_t delta)
173169
return newValue;
174170
}
175171

176-
uint32_t core_util_atomic_decr_u32(uint32_t * valuePtr, uint32_t delta)
172+
uint32_t core_util_atomic_decr_u32(uint32_t *valuePtr, uint32_t delta)
177173
{
178174
uint32_t newValue;
179175
do {
@@ -236,7 +232,14 @@ bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uin
236232
return success;
237233
}
238234

239-
uint8_t core_util_atomic_incr_u8(uint8_t * valuePtr, uint8_t delta)
235+
bool core_util_atomic_cas_ptr(void **ptr, void **expectedCurrentValue, void *desiredValue) {
236+
return core_util_atomic_cas_u32(
237+
(uintptr_t *)ptr,
238+
(uintptr_t *)expectedCurrentValue,
239+
(uintptr_t)desiredValue);
240+
}
241+
242+
uint8_t core_util_atomic_incr_u8(uint8_t *valuePtr, uint8_t delta)
240243
{
241244
uint8_t newValue;
242245
core_util_critical_section_enter();
@@ -246,7 +249,7 @@ uint8_t core_util_atomic_incr_u8(uint8_t * valuePtr, uint8_t delta)
246249
return newValue;
247250
}
248251

249-
uint16_t core_util_atomic_incr_u16(uint16_t * valuePtr, uint16_t delta)
252+
uint16_t core_util_atomic_incr_u16(uint16_t *valuePtr, uint16_t delta)
250253
{
251254
uint16_t newValue;
252255
core_util_critical_section_enter();
@@ -256,7 +259,7 @@ uint16_t core_util_atomic_incr_u16(uint16_t * valuePtr, uint16_t delta)
256259
return newValue;
257260
}
258261

259-
uint32_t core_util_atomic_incr_u32(uint32_t * valuePtr, uint32_t delta)
262+
uint32_t core_util_atomic_incr_u32(uint32_t *valuePtr, uint32_t delta)
260263
{
261264
uint32_t newValue;
262265
core_util_critical_section_enter();
@@ -266,8 +269,12 @@ uint32_t core_util_atomic_incr_u32(uint32_t * valuePtr, uint32_t delta)
266269
return newValue;
267270
}
268271

272+
void *core_util_atomic_incr_ptr(void **valuePtr, ptrdiff_t delta) {
273+
return core_util_atomic_incr((uintptr_t)valuePtr, (uintptr_t)delta);
274+
}
275+
269276

270-
uint8_t core_util_atomic_decr_u8(uint8_t * valuePtr, uint8_t delta)
277+
uint8_t core_util_atomic_decr_u8(uint8_t *valuePtr, uint8_t delta)
271278
{
272279
uint8_t newValue;
273280
core_util_critical_section_enter();
@@ -277,7 +284,7 @@ uint8_t core_util_atomic_decr_u8(uint8_t * valuePtr, uint8_t delta)
277284
return newValue;
278285
}
279286

280-
uint16_t core_util_atomic_decr_u16(uint16_t * valuePtr, uint16_t delta)
287+
uint16_t core_util_atomic_decr_u16(uint16_t *valuePtr, uint16_t delta)
281288
{
282289
uint16_t newValue;
283290
core_util_critical_section_enter();
@@ -287,7 +294,7 @@ uint16_t core_util_atomic_decr_u16(uint16_t * valuePtr, uint16_t delta)
287294
return newValue;
288295
}
289296

290-
uint32_t core_util_atomic_decr_u32(uint32_t * valuePtr, uint32_t delta)
297+
uint32_t core_util_atomic_decr_u32(uint32_t *valuePtr, uint32_t delta)
291298
{
292299
uint32_t newValue;
293300
core_util_critical_section_enter();
@@ -297,5 +304,9 @@ uint32_t core_util_atomic_decr_u32(uint32_t * valuePtr, uint32_t delta)
297304
return newValue;
298305
}
299306

307+
void *core_util_atomic_decr_ptr(void **valuePtr, ptrdiff_t delta) {
308+
return core_util_atomic_decr((uintptr_t)valuePtr, (uintptr_t)delta);
309+
}
310+
300311
#endif
301312

0 commit comments

Comments
 (0)