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