mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
188:bcfe06ba3d64
mbed library release version 165

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 160:d5399cc887bb 1
<> 160:d5399cc887bb 2 /*
<> 160:d5399cc887bb 3 * Copyright (c) 2015-2016, ARM Limited, All Rights Reserved
<> 160:d5399cc887bb 4 * SPDX-License-Identifier: Apache-2.0
<> 160:d5399cc887bb 5 *
<> 160:d5399cc887bb 6 * Licensed under the Apache License, Version 2.0 (the "License"); you may
<> 160:d5399cc887bb 7 * not use this file except in compliance with the License.
<> 160:d5399cc887bb 8 * You may obtain a copy of the License at
<> 160:d5399cc887bb 9 *
<> 160:d5399cc887bb 10 * http://www.apache.org/licenses/LICENSE-2.0
<> 160:d5399cc887bb 11 *
<> 160:d5399cc887bb 12 * Unless required by applicable law or agreed to in writing, software
<> 160:d5399cc887bb 13 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
<> 160:d5399cc887bb 14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 160:d5399cc887bb 15 * See the License for the specific language governing permissions and
<> 160:d5399cc887bb 16 * limitations under the License.
<> 160:d5399cc887bb 17 */
<> 160:d5399cc887bb 18
<> 160:d5399cc887bb 19 #ifndef __MBED_UTIL_CRITICAL_H__
<> 160:d5399cc887bb 20 #define __MBED_UTIL_CRITICAL_H__
<> 160:d5399cc887bb 21
<> 160:d5399cc887bb 22 #include <stdbool.h>
<> 160:d5399cc887bb 23 #include <stdint.h>
<> 160:d5399cc887bb 24 #include <stddef.h>
<> 160:d5399cc887bb 25
<> 160:d5399cc887bb 26 #ifdef __cplusplus
<> 160:d5399cc887bb 27 extern "C" {
<> 160:d5399cc887bb 28 #endif
<> 160:d5399cc887bb 29
AnnaBridge 178:79309dc6340a 30 /** \addtogroup platform */
AnnaBridge 178:79309dc6340a 31 /** @{*/
AnnaBridge 178:79309dc6340a 32 /**
AnnaBridge 178:79309dc6340a 33 * \defgroup platform_critical critical section function
AnnaBridge 178:79309dc6340a 34 * @{
AnnaBridge 178:79309dc6340a 35 */
<> 160:d5399cc887bb 36
<> 160:d5399cc887bb 37 /** Determine the current interrupts enabled state
<> 160:d5399cc887bb 38 *
<> 160:d5399cc887bb 39 * This function can be called to determine whether or not interrupts are currently enabled.
AnnaBridge 167:e84263d55307 40 * @note
<> 160:d5399cc887bb 41 * NOTE:
AnnaBridge 188:bcfe06ba3d64 42 * This function works for both cortex-A and cortex-M, although the underlying implementation
<> 160:d5399cc887bb 43 * differs.
<> 160:d5399cc887bb 44 * @return true if interrupts are enabled, false otherwise
<> 160:d5399cc887bb 45 */
<> 160:d5399cc887bb 46 bool core_util_are_interrupts_enabled(void);
<> 160:d5399cc887bb 47
AnnaBridge 167:e84263d55307 48 /** Determine if this code is executing from an interrupt
AnnaBridge 167:e84263d55307 49 *
AnnaBridge 167:e84263d55307 50 * This function can be called to determine if the code is running on interrupt context.
AnnaBridge 167:e84263d55307 51 * @note
AnnaBridge 167:e84263d55307 52 * NOTE:
AnnaBridge 188:bcfe06ba3d64 53 * This function works for both cortex-A and cortex-M, although the underlying implementation
AnnaBridge 167:e84263d55307 54 * differs.
AnnaBridge 167:e84263d55307 55 * @return true if in an isr, false otherwise
AnnaBridge 167:e84263d55307 56 */
AnnaBridge 167:e84263d55307 57 bool core_util_is_isr_active(void);
AnnaBridge 167:e84263d55307 58
<> 160:d5399cc887bb 59 /** Mark the start of a critical section
<> 160:d5399cc887bb 60 *
<> 160:d5399cc887bb 61 * This function should be called to mark the start of a critical section of code.
AnnaBridge 167:e84263d55307 62 * @note
<> 160:d5399cc887bb 63 * NOTES:
<> 160:d5399cc887bb 64 * 1) The use of this style of critical section is targetted at C based implementations.
<> 160:d5399cc887bb 65 * 2) These critical sections can be nested.
<> 160:d5399cc887bb 66 * 3) The interrupt enable state on entry to the first critical section (of a nested set, or single
<> 160:d5399cc887bb 67 * section) will be preserved on exit from the section.
<> 160:d5399cc887bb 68 * 4) This implementation will currently only work on code running in privileged mode.
<> 160:d5399cc887bb 69 */
<> 160:d5399cc887bb 70 void core_util_critical_section_enter(void);
<> 160:d5399cc887bb 71
<> 160:d5399cc887bb 72 /** Mark the end of a critical section
<> 160:d5399cc887bb 73 *
<> 160:d5399cc887bb 74 * This function should be called to mark the end of a critical section of code.
AnnaBridge 167:e84263d55307 75 * @note
<> 160:d5399cc887bb 76 * NOTES:
<> 160:d5399cc887bb 77 * 1) The use of this style of critical section is targetted at C based implementations.
<> 160:d5399cc887bb 78 * 2) These critical sections can be nested.
<> 160:d5399cc887bb 79 * 3) The interrupt enable state on entry to the first critical section (of a nested set, or single
<> 160:d5399cc887bb 80 * section) will be preserved on exit from the section.
<> 160:d5399cc887bb 81 * 4) This implementation will currently only work on code running in privileged mode.
<> 160:d5399cc887bb 82 */
<> 160:d5399cc887bb 83 void core_util_critical_section_exit(void);
<> 160:d5399cc887bb 84
<> 160:d5399cc887bb 85 /**
AnnaBridge 184:08ed48f1de7f 86 * Determine if we are currently in a critical section
AnnaBridge 184:08ed48f1de7f 87 *
AnnaBridge 184:08ed48f1de7f 88 * @return true if in a critical section, false otherwise.
AnnaBridge 184:08ed48f1de7f 89 */
AnnaBridge 184:08ed48f1de7f 90 bool core_util_in_critical_section(void);
AnnaBridge 184:08ed48f1de7f 91
AnnaBridge 184:08ed48f1de7f 92 /**
AnnaBridge 189:f392fc9709a3 93 * A lock-free, primitive atomic flag.
AnnaBridge 189:f392fc9709a3 94 *
AnnaBridge 189:f392fc9709a3 95 * Emulate C11's atomic_flag. The flag is initially in an indeterminate state
AnnaBridge 189:f392fc9709a3 96 * unless explicitly initialized with CORE_UTIL_ATOMIC_FLAG_INIT.
AnnaBridge 189:f392fc9709a3 97 */
AnnaBridge 189:f392fc9709a3 98 typedef struct core_util_atomic_flag {
AnnaBridge 189:f392fc9709a3 99 uint8_t _flag;
AnnaBridge 189:f392fc9709a3 100 } core_util_atomic_flag;
AnnaBridge 189:f392fc9709a3 101
AnnaBridge 189:f392fc9709a3 102 /**
AnnaBridge 189:f392fc9709a3 103 * Initializer for a core_util_atomic_flag.
AnnaBridge 189:f392fc9709a3 104 *
AnnaBridge 189:f392fc9709a3 105 * Example:
AnnaBridge 189:f392fc9709a3 106 * ~~~
AnnaBridge 189:f392fc9709a3 107 * core_util_atomic_flag in_progress = CORE_UTIL_ATOMIC_FLAG_INIT;
AnnaBridge 189:f392fc9709a3 108 * ~~~
AnnaBridge 189:f392fc9709a3 109 */
AnnaBridge 189:f392fc9709a3 110 #define CORE_UTIL_ATOMIC_FLAG_INIT { 0 }
AnnaBridge 189:f392fc9709a3 111
AnnaBridge 189:f392fc9709a3 112 /**
AnnaBridge 189:f392fc9709a3 113 * Atomic test and set.
AnnaBridge 189:f392fc9709a3 114 *
AnnaBridge 189:f392fc9709a3 115 * Atomically tests then sets the flag to true, returning the previous value.
AnnaBridge 189:f392fc9709a3 116 *
AnnaBridge 189:f392fc9709a3 117 * @param flagPtr Target flag being tested and set.
AnnaBridge 189:f392fc9709a3 118 * @return The previous value.
AnnaBridge 189:f392fc9709a3 119 */
AnnaBridge 189:f392fc9709a3 120 bool core_util_atomic_flag_test_and_set(volatile core_util_atomic_flag *flagPtr);
AnnaBridge 189:f392fc9709a3 121
AnnaBridge 189:f392fc9709a3 122 /**
AnnaBridge 189:f392fc9709a3 123 * Atomic clear.
AnnaBridge 189:f392fc9709a3 124 *
AnnaBridge 189:f392fc9709a3 125 * @param flagPtr Target flag being cleared.
AnnaBridge 189:f392fc9709a3 126 */
AnnaBridge 189:f392fc9709a3 127 void core_util_atomic_flag_clear(volatile core_util_atomic_flag *flagPtr);
AnnaBridge 189:f392fc9709a3 128
AnnaBridge 189:f392fc9709a3 129 /**
<> 160:d5399cc887bb 130 * Atomic compare and set. It compares the contents of a memory location to a
<> 160:d5399cc887bb 131 * given value and, only if they are the same, modifies the contents of that
<> 160:d5399cc887bb 132 * memory location to a given new value. This is done as a single atomic
<> 160:d5399cc887bb 133 * operation. The atomicity guarantees that the new value is calculated based on
<> 160:d5399cc887bb 134 * up-to-date information; if the value had been updated by another thread in
<> 160:d5399cc887bb 135 * the meantime, the write would fail due to a mismatched expectedCurrentValue.
<> 160:d5399cc887bb 136 *
<> 160:d5399cc887bb 137 * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
<> 160:d5399cc887bb 138 * you to the article on compare-and swap].
<> 160:d5399cc887bb 139 *
<> 160:d5399cc887bb 140 * @param ptr The target memory location.
<> 160:d5399cc887bb 141 * @param[in,out] expectedCurrentValue A pointer to some location holding the
<> 160:d5399cc887bb 142 * expected current value of the data being set atomically.
<> 160:d5399cc887bb 143 * The computed 'desiredValue' should be a function of this current value.
AnnaBridge 167:e84263d55307 144 * @note: This is an in-out parameter. In the
<> 160:d5399cc887bb 145 * failure case of atomic_cas (where the
<> 160:d5399cc887bb 146 * destination isn't set), the pointee of expectedCurrentValue is
<> 160:d5399cc887bb 147 * updated with the current value.
<> 160:d5399cc887bb 148 * @param[in] desiredValue The new value computed based on '*expectedCurrentValue'.
<> 160:d5399cc887bb 149 *
<> 160:d5399cc887bb 150 * @return true if the memory location was atomically
<> 160:d5399cc887bb 151 * updated with the desired value (after verifying
<> 160:d5399cc887bb 152 * that it contained the expectedCurrentValue),
<> 160:d5399cc887bb 153 * false otherwise. In the failure case,
<> 160:d5399cc887bb 154 * exepctedCurrentValue is updated with the new
<> 160:d5399cc887bb 155 * value of the target memory location.
<> 160:d5399cc887bb 156 *
<> 160:d5399cc887bb 157 * pseudocode:
<> 160:d5399cc887bb 158 * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
<> 160:d5399cc887bb 159 * if *p != *old {
<> 160:d5399cc887bb 160 * *old = *p
<> 160:d5399cc887bb 161 * return false
<> 160:d5399cc887bb 162 * }
<> 160:d5399cc887bb 163 * *p = new
<> 160:d5399cc887bb 164 * return true
<> 160:d5399cc887bb 165 * }
<> 160:d5399cc887bb 166 *
AnnaBridge 167:e84263d55307 167 * @note: In the failure case (where the destination isn't set), the value
Anna Bridge 180:96ed750bd169 168 * pointed to by expectedCurrentValue is instead updated with the current value.
<> 160:d5399cc887bb 169 * This property helps writing concise code for the following incr:
<> 160:d5399cc887bb 170 *
<> 160:d5399cc887bb 171 * function incr(p : pointer to int, a : int) returns int {
<> 160:d5399cc887bb 172 * done = false
<> 160:d5399cc887bb 173 * value = *p // This fetch operation need not be atomic.
<> 160:d5399cc887bb 174 * while not done {
<> 160:d5399cc887bb 175 * done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
<> 160:d5399cc887bb 176 * }
<> 160:d5399cc887bb 177 * return value + a
<> 160:d5399cc887bb 178 * }
Anna Bridge 180:96ed750bd169 179 *
Anna Bridge 180:96ed750bd169 180 * @note: This corresponds to the C11 "atomic_compare_exchange_strong" - it
Anna Bridge 180:96ed750bd169 181 * always succeeds if the current value is expected, as per the pseudocode
Anna Bridge 180:96ed750bd169 182 * above; it will not spuriously fail as "atomic_compare_exchange_weak" may.
<> 160:d5399cc887bb 183 */
AnnaBridge 184:08ed48f1de7f 184 bool core_util_atomic_cas_u8(volatile uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue);
<> 160:d5399cc887bb 185
<> 160:d5399cc887bb 186 /**
<> 160:d5399cc887bb 187 * Atomic compare and set. It compares the contents of a memory location to a
<> 160:d5399cc887bb 188 * given value and, only if they are the same, modifies the contents of that
<> 160:d5399cc887bb 189 * memory location to a given new value. This is done as a single atomic
<> 160:d5399cc887bb 190 * operation. The atomicity guarantees that the new value is calculated based on
<> 160:d5399cc887bb 191 * up-to-date information; if the value had been updated by another thread in
<> 160:d5399cc887bb 192 * the meantime, the write would fail due to a mismatched expectedCurrentValue.
<> 160:d5399cc887bb 193 *
<> 160:d5399cc887bb 194 * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
<> 160:d5399cc887bb 195 * you to the article on compare-and swap].
<> 160:d5399cc887bb 196 *
<> 160:d5399cc887bb 197 * @param ptr The target memory location.
<> 160:d5399cc887bb 198 * @param[in,out] expectedCurrentValue A pointer to some location holding the
<> 160:d5399cc887bb 199 * expected current value of the data being set atomically.
<> 160:d5399cc887bb 200 * The computed 'desiredValue' should be a function of this current value.
AnnaBridge 167:e84263d55307 201 * @note: This is an in-out parameter. In the
<> 160:d5399cc887bb 202 * failure case of atomic_cas (where the
<> 160:d5399cc887bb 203 * destination isn't set), the pointee of expectedCurrentValue is
<> 160:d5399cc887bb 204 * updated with the current value.
<> 160:d5399cc887bb 205 * @param[in] desiredValue The new value computed based on '*expectedCurrentValue'.
<> 160:d5399cc887bb 206 *
<> 160:d5399cc887bb 207 * @return true if the memory location was atomically
<> 160:d5399cc887bb 208 * updated with the desired value (after verifying
<> 160:d5399cc887bb 209 * that it contained the expectedCurrentValue),
<> 160:d5399cc887bb 210 * false otherwise. In the failure case,
<> 160:d5399cc887bb 211 * exepctedCurrentValue is updated with the new
<> 160:d5399cc887bb 212 * value of the target memory location.
<> 160:d5399cc887bb 213 *
<> 160:d5399cc887bb 214 * pseudocode:
<> 160:d5399cc887bb 215 * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
<> 160:d5399cc887bb 216 * if *p != *old {
<> 160:d5399cc887bb 217 * *old = *p
<> 160:d5399cc887bb 218 * return false
<> 160:d5399cc887bb 219 * }
<> 160:d5399cc887bb 220 * *p = new
<> 160:d5399cc887bb 221 * return true
<> 160:d5399cc887bb 222 * }
<> 160:d5399cc887bb 223 *
AnnaBridge 167:e84263d55307 224 * @note: In the failure case (where the destination isn't set), the value
Anna Bridge 180:96ed750bd169 225 * pointed to by expectedCurrentValue is instead updated with the current value.
<> 160:d5399cc887bb 226 * This property helps writing concise code for the following incr:
<> 160:d5399cc887bb 227 *
<> 160:d5399cc887bb 228 * function incr(p : pointer to int, a : int) returns int {
<> 160:d5399cc887bb 229 * done = false
<> 160:d5399cc887bb 230 * value = *p // This fetch operation need not be atomic.
<> 160:d5399cc887bb 231 * while not done {
<> 160:d5399cc887bb 232 * done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
<> 160:d5399cc887bb 233 * }
<> 160:d5399cc887bb 234 * return value + a
<> 160:d5399cc887bb 235 * }
Anna Bridge 180:96ed750bd169 236 *
Anna Bridge 180:96ed750bd169 237 * @note: This corresponds to the C11 "atomic_compare_exchange_strong" - it
Anna Bridge 180:96ed750bd169 238 * always succeeds if the current value is expected, as per the pseudocode
Anna Bridge 180:96ed750bd169 239 * above; it will not spuriously fail as "atomic_compare_exchange_weak" may.
<> 160:d5399cc887bb 240 */
AnnaBridge 184:08ed48f1de7f 241 bool core_util_atomic_cas_u16(volatile uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue);
<> 160:d5399cc887bb 242
<> 160:d5399cc887bb 243 /**
<> 160:d5399cc887bb 244 * Atomic compare and set. It compares the contents of a memory location to a
<> 160:d5399cc887bb 245 * given value and, only if they are the same, modifies the contents of that
<> 160:d5399cc887bb 246 * memory location to a given new value. This is done as a single atomic
<> 160:d5399cc887bb 247 * operation. The atomicity guarantees that the new value is calculated based on
<> 160:d5399cc887bb 248 * up-to-date information; if the value had been updated by another thread in
<> 160:d5399cc887bb 249 * the meantime, the write would fail due to a mismatched expectedCurrentValue.
<> 160:d5399cc887bb 250 *
<> 160:d5399cc887bb 251 * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
<> 160:d5399cc887bb 252 * you to the article on compare-and swap].
<> 160:d5399cc887bb 253 *
<> 160:d5399cc887bb 254 * @param ptr The target memory location.
<> 160:d5399cc887bb 255 * @param[in,out] expectedCurrentValue A pointer to some location holding the
<> 160:d5399cc887bb 256 * expected current value of the data being set atomically.
<> 160:d5399cc887bb 257 * The computed 'desiredValue' should be a function of this current value.
AnnaBridge 167:e84263d55307 258 * @note: This is an in-out parameter. In the
<> 160:d5399cc887bb 259 * failure case of atomic_cas (where the
<> 160:d5399cc887bb 260 * destination isn't set), the pointee of expectedCurrentValue is
<> 160:d5399cc887bb 261 * updated with the current value.
<> 160:d5399cc887bb 262 * @param[in] desiredValue The new value computed based on '*expectedCurrentValue'.
<> 160:d5399cc887bb 263 *
<> 160:d5399cc887bb 264 * @return true if the memory location was atomically
<> 160:d5399cc887bb 265 * updated with the desired value (after verifying
<> 160:d5399cc887bb 266 * that it contained the expectedCurrentValue),
<> 160:d5399cc887bb 267 * false otherwise. In the failure case,
<> 160:d5399cc887bb 268 * exepctedCurrentValue is updated with the new
<> 160:d5399cc887bb 269 * value of the target memory location.
<> 160:d5399cc887bb 270 *
<> 160:d5399cc887bb 271 * pseudocode:
<> 160:d5399cc887bb 272 * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
<> 160:d5399cc887bb 273 * if *p != *old {
<> 160:d5399cc887bb 274 * *old = *p
<> 160:d5399cc887bb 275 * return false
<> 160:d5399cc887bb 276 * }
<> 160:d5399cc887bb 277 * *p = new
<> 160:d5399cc887bb 278 * return true
<> 160:d5399cc887bb 279 * }
<> 160:d5399cc887bb 280 *
AnnaBridge 167:e84263d55307 281 * @note: In the failure case (where the destination isn't set), the value
Anna Bridge 180:96ed750bd169 282 * pointed to by expectedCurrentValue is instead updated with the current value.
<> 160:d5399cc887bb 283 * This property helps writing concise code for the following incr:
<> 160:d5399cc887bb 284 *
<> 160:d5399cc887bb 285 * function incr(p : pointer to int, a : int) returns int {
<> 160:d5399cc887bb 286 * done = false
<> 160:d5399cc887bb 287 * value = *p // This fetch operation need not be atomic.
<> 160:d5399cc887bb 288 * while not done {
<> 160:d5399cc887bb 289 * done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
<> 160:d5399cc887bb 290 * }
<> 160:d5399cc887bb 291 * return value + a
Anna Bridge 180:96ed750bd169 292 *
Anna Bridge 180:96ed750bd169 293 * @note: This corresponds to the C11 "atomic_compare_exchange_strong" - it
Anna Bridge 180:96ed750bd169 294 * always succeeds if the current value is expected, as per the pseudocode
Anna Bridge 180:96ed750bd169 295 * above; it will not spuriously fail as "atomic_compare_exchange_weak" may.
<> 160:d5399cc887bb 296 * }
<> 160:d5399cc887bb 297 */
AnnaBridge 184:08ed48f1de7f 298 bool core_util_atomic_cas_u32(volatile uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue);
<> 160:d5399cc887bb 299
<> 160:d5399cc887bb 300 /**
<> 160:d5399cc887bb 301 * Atomic compare and set. It compares the contents of a memory location to a
<> 160:d5399cc887bb 302 * given value and, only if they are the same, modifies the contents of that
<> 160:d5399cc887bb 303 * memory location to a given new value. This is done as a single atomic
<> 160:d5399cc887bb 304 * operation. The atomicity guarantees that the new value is calculated based on
<> 160:d5399cc887bb 305 * up-to-date information; if the value had been updated by another thread in
<> 160:d5399cc887bb 306 * the meantime, the write would fail due to a mismatched expectedCurrentValue.
<> 160:d5399cc887bb 307 *
<> 160:d5399cc887bb 308 * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
<> 160:d5399cc887bb 309 * you to the article on compare-and swap].
<> 160:d5399cc887bb 310 *
<> 160:d5399cc887bb 311 * @param ptr The target memory location.
<> 160:d5399cc887bb 312 * @param[in,out] expectedCurrentValue A pointer to some location holding the
<> 160:d5399cc887bb 313 * expected current value of the data being set atomically.
<> 160:d5399cc887bb 314 * The computed 'desiredValue' should be a function of this current value.
AnnaBridge 167:e84263d55307 315 * @note: This is an in-out parameter. In the
<> 160:d5399cc887bb 316 * failure case of atomic_cas (where the
<> 160:d5399cc887bb 317 * destination isn't set), the pointee of expectedCurrentValue is
<> 160:d5399cc887bb 318 * updated with the current value.
<> 160:d5399cc887bb 319 * @param[in] desiredValue The new value computed based on '*expectedCurrentValue'.
<> 160:d5399cc887bb 320 *
<> 160:d5399cc887bb 321 * @return true if the memory location was atomically
<> 160:d5399cc887bb 322 * updated with the desired value (after verifying
<> 160:d5399cc887bb 323 * that it contained the expectedCurrentValue),
<> 160:d5399cc887bb 324 * false otherwise. In the failure case,
<> 160:d5399cc887bb 325 * exepctedCurrentValue is updated with the new
<> 160:d5399cc887bb 326 * value of the target memory location.
<> 160:d5399cc887bb 327 *
<> 160:d5399cc887bb 328 * pseudocode:
<> 160:d5399cc887bb 329 * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
<> 160:d5399cc887bb 330 * if *p != *old {
<> 160:d5399cc887bb 331 * *old = *p
<> 160:d5399cc887bb 332 * return false
<> 160:d5399cc887bb 333 * }
<> 160:d5399cc887bb 334 * *p = new
<> 160:d5399cc887bb 335 * return true
<> 160:d5399cc887bb 336 * }
<> 160:d5399cc887bb 337 *
AnnaBridge 167:e84263d55307 338 * @note: In the failure case (where the destination isn't set), the value
Anna Bridge 180:96ed750bd169 339 * pointed to by expectedCurrentValue is instead updated with the current value.
<> 160:d5399cc887bb 340 * This property helps writing concise code for the following incr:
<> 160:d5399cc887bb 341 *
<> 160:d5399cc887bb 342 * function incr(p : pointer to int, a : int) returns int {
<> 160:d5399cc887bb 343 * done = false
<> 160:d5399cc887bb 344 * value = *p // This fetch operation need not be atomic.
<> 160:d5399cc887bb 345 * while not done {
<> 160:d5399cc887bb 346 * done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
<> 160:d5399cc887bb 347 * }
<> 160:d5399cc887bb 348 * return value + a
<> 160:d5399cc887bb 349 * }
Anna Bridge 180:96ed750bd169 350 *
Anna Bridge 180:96ed750bd169 351 * @note: This corresponds to the C11 "atomic_compare_exchange_strong" - it
Anna Bridge 180:96ed750bd169 352 * always succeeds if the current value is expected, as per the pseudocode
Anna Bridge 180:96ed750bd169 353 * above; it will not spuriously fail as "atomic_compare_exchange_weak" may.
<> 160:d5399cc887bb 354 */
AnnaBridge 187:0387e8f68319 355 bool core_util_atomic_cas_ptr(void *volatile *ptr, void **expectedCurrentValue, void *desiredValue);
<> 160:d5399cc887bb 356
<> 160:d5399cc887bb 357 /**
<> 160:d5399cc887bb 358 * Atomic increment.
<> 160:d5399cc887bb 359 * @param valuePtr Target memory location being incremented.
<> 160:d5399cc887bb 360 * @param delta The amount being incremented.
<> 160:d5399cc887bb 361 * @return The new incremented value.
<> 160:d5399cc887bb 362 */
AnnaBridge 184:08ed48f1de7f 363 uint8_t core_util_atomic_incr_u8(volatile uint8_t *valuePtr, uint8_t delta);
<> 160:d5399cc887bb 364
<> 160:d5399cc887bb 365 /**
<> 160:d5399cc887bb 366 * Atomic increment.
<> 160:d5399cc887bb 367 * @param valuePtr Target memory location being incremented.
<> 160:d5399cc887bb 368 * @param delta The amount being incremented.
<> 160:d5399cc887bb 369 * @return The new incremented value.
<> 160:d5399cc887bb 370 */
AnnaBridge 184:08ed48f1de7f 371 uint16_t core_util_atomic_incr_u16(volatile uint16_t *valuePtr, uint16_t delta);
<> 160:d5399cc887bb 372
<> 160:d5399cc887bb 373 /**
<> 160:d5399cc887bb 374 * Atomic increment.
<> 160:d5399cc887bb 375 * @param valuePtr Target memory location being incremented.
<> 160:d5399cc887bb 376 * @param delta The amount being incremented.
<> 160:d5399cc887bb 377 * @return The new incremented value.
<> 160:d5399cc887bb 378 */
AnnaBridge 184:08ed48f1de7f 379 uint32_t core_util_atomic_incr_u32(volatile uint32_t *valuePtr, uint32_t delta);
<> 160:d5399cc887bb 380
<> 160:d5399cc887bb 381 /**
<> 160:d5399cc887bb 382 * Atomic increment.
<> 160:d5399cc887bb 383 * @param valuePtr Target memory location being incremented.
<> 160:d5399cc887bb 384 * @param delta The amount being incremented in bytes.
<> 160:d5399cc887bb 385 * @return The new incremented value.
<> 160:d5399cc887bb 386 *
<> 160:d5399cc887bb 387 * @note The type of the pointer argument is not taken into account
<> 160:d5399cc887bb 388 * and the pointer is incremented by bytes.
<> 160:d5399cc887bb 389 */
AnnaBridge 187:0387e8f68319 390 void *core_util_atomic_incr_ptr(void *volatile *valuePtr, ptrdiff_t delta);
<> 160:d5399cc887bb 391
<> 160:d5399cc887bb 392 /**
<> 160:d5399cc887bb 393 * Atomic decrement.
<> 160:d5399cc887bb 394 * @param valuePtr Target memory location being decremented.
<> 160:d5399cc887bb 395 * @param delta The amount being decremented.
<> 160:d5399cc887bb 396 * @return The new decremented value.
<> 160:d5399cc887bb 397 */
AnnaBridge 184:08ed48f1de7f 398 uint8_t core_util_atomic_decr_u8(volatile uint8_t *valuePtr, uint8_t delta);
<> 160:d5399cc887bb 399
<> 160:d5399cc887bb 400 /**
<> 160:d5399cc887bb 401 * Atomic decrement.
<> 160:d5399cc887bb 402 * @param valuePtr Target memory location being decremented.
<> 160:d5399cc887bb 403 * @param delta The amount being decremented.
<> 160:d5399cc887bb 404 * @return The new decremented value.
<> 160:d5399cc887bb 405 */
AnnaBridge 184:08ed48f1de7f 406 uint16_t core_util_atomic_decr_u16(volatile uint16_t *valuePtr, uint16_t delta);
<> 160:d5399cc887bb 407
<> 160:d5399cc887bb 408 /**
<> 160:d5399cc887bb 409 * Atomic decrement.
<> 160:d5399cc887bb 410 * @param valuePtr Target memory location being decremented.
<> 160:d5399cc887bb 411 * @param delta The amount being decremented.
<> 160:d5399cc887bb 412 * @return The new decremented value.
<> 160:d5399cc887bb 413 */
AnnaBridge 184:08ed48f1de7f 414 uint32_t core_util_atomic_decr_u32(volatile uint32_t *valuePtr, uint32_t delta);
<> 160:d5399cc887bb 415
<> 160:d5399cc887bb 416 /**
<> 160:d5399cc887bb 417 * Atomic decrement.
<> 160:d5399cc887bb 418 * @param valuePtr Target memory location being decremented.
<> 160:d5399cc887bb 419 * @param delta The amount being decremented in bytes.
<> 160:d5399cc887bb 420 * @return The new decremented value.
<> 160:d5399cc887bb 421 *
<> 160:d5399cc887bb 422 * @note The type of the pointer argument is not taken into account
<> 160:d5399cc887bb 423 * and the pointer is decremented by bytes
<> 160:d5399cc887bb 424 */
AnnaBridge 187:0387e8f68319 425 void *core_util_atomic_decr_ptr(void *volatile *valuePtr, ptrdiff_t delta);
<> 160:d5399cc887bb 426
<> 160:d5399cc887bb 427 #ifdef __cplusplus
<> 160:d5399cc887bb 428 } // extern "C"
<> 160:d5399cc887bb 429 #endif
AnnaBridge 178:79309dc6340a 430 /**@}*/
<> 160:d5399cc887bb 431
AnnaBridge 178:79309dc6340a 432 /**@}*/
<> 160:d5399cc887bb 433
<> 160:d5399cc887bb 434 #endif // __MBED_UTIL_CRITICAL_H__
<> 160:d5399cc887bb 435
AnnaBridge 178:79309dc6340a 436
AnnaBridge 178:79309dc6340a 437