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