mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
platform/mbed_critical.h@187:fa51feb62426, 2018-10-09 (annotated)
- Committer:
- pmcorreia
- Date:
- Tue Oct 09 14:42:37 2018 +0000
- Revision:
- 187:fa51feb62426
- Parent:
- 184:08ed48f1de7f
Updated version to work with F446RE
Who changed what in which revision?
User | Revision | Line number | New 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: |
<> | 160:d5399cc887bb | 42 | * This function works for both cortex-A and cortex-M, although the underlyng 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 | 167:e84263d55307 | 53 | * This function works for both cortex-A and cortex-M, although the underlyng 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 | /** |
<> | 160:d5399cc887bb | 93 | * Atomic compare and set. It compares the contents of a memory location to a |
<> | 160:d5399cc887bb | 94 | * given value and, only if they are the same, modifies the contents of that |
<> | 160:d5399cc887bb | 95 | * memory location to a given new value. This is done as a single atomic |
<> | 160:d5399cc887bb | 96 | * operation. The atomicity guarantees that the new value is calculated based on |
<> | 160:d5399cc887bb | 97 | * up-to-date information; if the value had been updated by another thread in |
<> | 160:d5399cc887bb | 98 | * the meantime, the write would fail due to a mismatched expectedCurrentValue. |
<> | 160:d5399cc887bb | 99 | * |
<> | 160:d5399cc887bb | 100 | * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect |
<> | 160:d5399cc887bb | 101 | * you to the article on compare-and swap]. |
<> | 160:d5399cc887bb | 102 | * |
<> | 160:d5399cc887bb | 103 | * @param ptr The target memory location. |
<> | 160:d5399cc887bb | 104 | * @param[in,out] expectedCurrentValue A pointer to some location holding the |
<> | 160:d5399cc887bb | 105 | * expected current value of the data being set atomically. |
<> | 160:d5399cc887bb | 106 | * The computed 'desiredValue' should be a function of this current value. |
AnnaBridge | 167:e84263d55307 | 107 | * @note: This is an in-out parameter. In the |
<> | 160:d5399cc887bb | 108 | * failure case of atomic_cas (where the |
<> | 160:d5399cc887bb | 109 | * destination isn't set), the pointee of expectedCurrentValue is |
<> | 160:d5399cc887bb | 110 | * updated with the current value. |
<> | 160:d5399cc887bb | 111 | * @param[in] desiredValue The new value computed based on '*expectedCurrentValue'. |
<> | 160:d5399cc887bb | 112 | * |
<> | 160:d5399cc887bb | 113 | * @return true if the memory location was atomically |
<> | 160:d5399cc887bb | 114 | * updated with the desired value (after verifying |
<> | 160:d5399cc887bb | 115 | * that it contained the expectedCurrentValue), |
<> | 160:d5399cc887bb | 116 | * false otherwise. In the failure case, |
<> | 160:d5399cc887bb | 117 | * exepctedCurrentValue is updated with the new |
<> | 160:d5399cc887bb | 118 | * value of the target memory location. |
<> | 160:d5399cc887bb | 119 | * |
<> | 160:d5399cc887bb | 120 | * pseudocode: |
<> | 160:d5399cc887bb | 121 | * function cas(p : pointer to int, old : pointer to int, new : int) returns bool { |
<> | 160:d5399cc887bb | 122 | * if *p != *old { |
<> | 160:d5399cc887bb | 123 | * *old = *p |
<> | 160:d5399cc887bb | 124 | * return false |
<> | 160:d5399cc887bb | 125 | * } |
<> | 160:d5399cc887bb | 126 | * *p = new |
<> | 160:d5399cc887bb | 127 | * return true |
<> | 160:d5399cc887bb | 128 | * } |
<> | 160:d5399cc887bb | 129 | * |
AnnaBridge | 167:e84263d55307 | 130 | * @note: In the failure case (where the destination isn't set), the value |
Anna Bridge |
180:96ed750bd169 | 131 | * pointed to by expectedCurrentValue is instead updated with the current value. |
<> | 160:d5399cc887bb | 132 | * This property helps writing concise code for the following incr: |
<> | 160:d5399cc887bb | 133 | * |
<> | 160:d5399cc887bb | 134 | * function incr(p : pointer to int, a : int) returns int { |
<> | 160:d5399cc887bb | 135 | * done = false |
<> | 160:d5399cc887bb | 136 | * value = *p // This fetch operation need not be atomic. |
<> | 160:d5399cc887bb | 137 | * while not done { |
<> | 160:d5399cc887bb | 138 | * done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success |
<> | 160:d5399cc887bb | 139 | * } |
<> | 160:d5399cc887bb | 140 | * return value + a |
<> | 160:d5399cc887bb | 141 | * } |
Anna Bridge |
180:96ed750bd169 | 142 | * |
Anna Bridge |
180:96ed750bd169 | 143 | * @note: This corresponds to the C11 "atomic_compare_exchange_strong" - it |
Anna Bridge |
180:96ed750bd169 | 144 | * always succeeds if the current value is expected, as per the pseudocode |
Anna Bridge |
180:96ed750bd169 | 145 | * above; it will not spuriously fail as "atomic_compare_exchange_weak" may. |
<> | 160:d5399cc887bb | 146 | */ |
AnnaBridge | 184:08ed48f1de7f | 147 | bool core_util_atomic_cas_u8(volatile uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue); |
<> | 160:d5399cc887bb | 148 | |
<> | 160:d5399cc887bb | 149 | /** |
<> | 160:d5399cc887bb | 150 | * Atomic compare and set. It compares the contents of a memory location to a |
<> | 160:d5399cc887bb | 151 | * given value and, only if they are the same, modifies the contents of that |
<> | 160:d5399cc887bb | 152 | * memory location to a given new value. This is done as a single atomic |
<> | 160:d5399cc887bb | 153 | * operation. The atomicity guarantees that the new value is calculated based on |
<> | 160:d5399cc887bb | 154 | * up-to-date information; if the value had been updated by another thread in |
<> | 160:d5399cc887bb | 155 | * the meantime, the write would fail due to a mismatched expectedCurrentValue. |
<> | 160:d5399cc887bb | 156 | * |
<> | 160:d5399cc887bb | 157 | * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect |
<> | 160:d5399cc887bb | 158 | * you to the article on compare-and swap]. |
<> | 160:d5399cc887bb | 159 | * |
<> | 160:d5399cc887bb | 160 | * @param ptr The target memory location. |
<> | 160:d5399cc887bb | 161 | * @param[in,out] expectedCurrentValue A pointer to some location holding the |
<> | 160:d5399cc887bb | 162 | * expected current value of the data being set atomically. |
<> | 160:d5399cc887bb | 163 | * The computed 'desiredValue' should be a function of this current value. |
AnnaBridge | 167:e84263d55307 | 164 | * @note: This is an in-out parameter. In the |
<> | 160:d5399cc887bb | 165 | * failure case of atomic_cas (where the |
<> | 160:d5399cc887bb | 166 | * destination isn't set), the pointee of expectedCurrentValue is |
<> | 160:d5399cc887bb | 167 | * updated with the current value. |
<> | 160:d5399cc887bb | 168 | * @param[in] desiredValue The new value computed based on '*expectedCurrentValue'. |
<> | 160:d5399cc887bb | 169 | * |
<> | 160:d5399cc887bb | 170 | * @return true if the memory location was atomically |
<> | 160:d5399cc887bb | 171 | * updated with the desired value (after verifying |
<> | 160:d5399cc887bb | 172 | * that it contained the expectedCurrentValue), |
<> | 160:d5399cc887bb | 173 | * false otherwise. In the failure case, |
<> | 160:d5399cc887bb | 174 | * exepctedCurrentValue is updated with the new |
<> | 160:d5399cc887bb | 175 | * value of the target memory location. |
<> | 160:d5399cc887bb | 176 | * |
<> | 160:d5399cc887bb | 177 | * pseudocode: |
<> | 160:d5399cc887bb | 178 | * function cas(p : pointer to int, old : pointer to int, new : int) returns bool { |
<> | 160:d5399cc887bb | 179 | * if *p != *old { |
<> | 160:d5399cc887bb | 180 | * *old = *p |
<> | 160:d5399cc887bb | 181 | * return false |
<> | 160:d5399cc887bb | 182 | * } |
<> | 160:d5399cc887bb | 183 | * *p = new |
<> | 160:d5399cc887bb | 184 | * return true |
<> | 160:d5399cc887bb | 185 | * } |
<> | 160:d5399cc887bb | 186 | * |
AnnaBridge | 167:e84263d55307 | 187 | * @note: In the failure case (where the destination isn't set), the value |
Anna Bridge |
180:96ed750bd169 | 188 | * pointed to by expectedCurrentValue is instead updated with the current value. |
<> | 160:d5399cc887bb | 189 | * This property helps writing concise code for the following incr: |
<> | 160:d5399cc887bb | 190 | * |
<> | 160:d5399cc887bb | 191 | * function incr(p : pointer to int, a : int) returns int { |
<> | 160:d5399cc887bb | 192 | * done = false |
<> | 160:d5399cc887bb | 193 | * value = *p // This fetch operation need not be atomic. |
<> | 160:d5399cc887bb | 194 | * while not done { |
<> | 160:d5399cc887bb | 195 | * done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success |
<> | 160:d5399cc887bb | 196 | * } |
<> | 160:d5399cc887bb | 197 | * return value + a |
<> | 160:d5399cc887bb | 198 | * } |
Anna Bridge |
180:96ed750bd169 | 199 | * |
Anna Bridge |
180:96ed750bd169 | 200 | * @note: This corresponds to the C11 "atomic_compare_exchange_strong" - it |
Anna Bridge |
180:96ed750bd169 | 201 | * always succeeds if the current value is expected, as per the pseudocode |
Anna Bridge |
180:96ed750bd169 | 202 | * above; it will not spuriously fail as "atomic_compare_exchange_weak" may. |
<> | 160:d5399cc887bb | 203 | */ |
AnnaBridge | 184:08ed48f1de7f | 204 | bool core_util_atomic_cas_u16(volatile uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue); |
<> | 160:d5399cc887bb | 205 | |
<> | 160:d5399cc887bb | 206 | /** |
<> | 160:d5399cc887bb | 207 | * Atomic compare and set. It compares the contents of a memory location to a |
<> | 160:d5399cc887bb | 208 | * given value and, only if they are the same, modifies the contents of that |
<> | 160:d5399cc887bb | 209 | * memory location to a given new value. This is done as a single atomic |
<> | 160:d5399cc887bb | 210 | * operation. The atomicity guarantees that the new value is calculated based on |
<> | 160:d5399cc887bb | 211 | * up-to-date information; if the value had been updated by another thread in |
<> | 160:d5399cc887bb | 212 | * the meantime, the write would fail due to a mismatched expectedCurrentValue. |
<> | 160:d5399cc887bb | 213 | * |
<> | 160:d5399cc887bb | 214 | * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect |
<> | 160:d5399cc887bb | 215 | * you to the article on compare-and swap]. |
<> | 160:d5399cc887bb | 216 | * |
<> | 160:d5399cc887bb | 217 | * @param ptr The target memory location. |
<> | 160:d5399cc887bb | 218 | * @param[in,out] expectedCurrentValue A pointer to some location holding the |
<> | 160:d5399cc887bb | 219 | * expected current value of the data being set atomically. |
<> | 160:d5399cc887bb | 220 | * The computed 'desiredValue' should be a function of this current value. |
AnnaBridge | 167:e84263d55307 | 221 | * @note: This is an in-out parameter. In the |
<> | 160:d5399cc887bb | 222 | * failure case of atomic_cas (where the |
<> | 160:d5399cc887bb | 223 | * destination isn't set), the pointee of expectedCurrentValue is |
<> | 160:d5399cc887bb | 224 | * updated with the current value. |
<> | 160:d5399cc887bb | 225 | * @param[in] desiredValue The new value computed based on '*expectedCurrentValue'. |
<> | 160:d5399cc887bb | 226 | * |
<> | 160:d5399cc887bb | 227 | * @return true if the memory location was atomically |
<> | 160:d5399cc887bb | 228 | * updated with the desired value (after verifying |
<> | 160:d5399cc887bb | 229 | * that it contained the expectedCurrentValue), |
<> | 160:d5399cc887bb | 230 | * false otherwise. In the failure case, |
<> | 160:d5399cc887bb | 231 | * exepctedCurrentValue is updated with the new |
<> | 160:d5399cc887bb | 232 | * value of the target memory location. |
<> | 160:d5399cc887bb | 233 | * |
<> | 160:d5399cc887bb | 234 | * pseudocode: |
<> | 160:d5399cc887bb | 235 | * function cas(p : pointer to int, old : pointer to int, new : int) returns bool { |
<> | 160:d5399cc887bb | 236 | * if *p != *old { |
<> | 160:d5399cc887bb | 237 | * *old = *p |
<> | 160:d5399cc887bb | 238 | * return false |
<> | 160:d5399cc887bb | 239 | * } |
<> | 160:d5399cc887bb | 240 | * *p = new |
<> | 160:d5399cc887bb | 241 | * return true |
<> | 160:d5399cc887bb | 242 | * } |
<> | 160:d5399cc887bb | 243 | * |
AnnaBridge | 167:e84263d55307 | 244 | * @note: In the failure case (where the destination isn't set), the value |
Anna Bridge |
180:96ed750bd169 | 245 | * pointed to by expectedCurrentValue is instead updated with the current value. |
<> | 160:d5399cc887bb | 246 | * This property helps writing concise code for the following incr: |
<> | 160:d5399cc887bb | 247 | * |
<> | 160:d5399cc887bb | 248 | * function incr(p : pointer to int, a : int) returns int { |
<> | 160:d5399cc887bb | 249 | * done = false |
<> | 160:d5399cc887bb | 250 | * value = *p // This fetch operation need not be atomic. |
<> | 160:d5399cc887bb | 251 | * while not done { |
<> | 160:d5399cc887bb | 252 | * done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success |
<> | 160:d5399cc887bb | 253 | * } |
<> | 160:d5399cc887bb | 254 | * return value + a |
Anna Bridge |
180:96ed750bd169 | 255 | * |
Anna Bridge |
180:96ed750bd169 | 256 | * @note: This corresponds to the C11 "atomic_compare_exchange_strong" - it |
Anna Bridge |
180:96ed750bd169 | 257 | * always succeeds if the current value is expected, as per the pseudocode |
Anna Bridge |
180:96ed750bd169 | 258 | * above; it will not spuriously fail as "atomic_compare_exchange_weak" may. |
<> | 160:d5399cc887bb | 259 | * } |
<> | 160:d5399cc887bb | 260 | */ |
AnnaBridge | 184:08ed48f1de7f | 261 | bool core_util_atomic_cas_u32(volatile uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue); |
<> | 160:d5399cc887bb | 262 | |
<> | 160:d5399cc887bb | 263 | /** |
<> | 160:d5399cc887bb | 264 | * Atomic compare and set. It compares the contents of a memory location to a |
<> | 160:d5399cc887bb | 265 | * given value and, only if they are the same, modifies the contents of that |
<> | 160:d5399cc887bb | 266 | * memory location to a given new value. This is done as a single atomic |
<> | 160:d5399cc887bb | 267 | * operation. The atomicity guarantees that the new value is calculated based on |
<> | 160:d5399cc887bb | 268 | * up-to-date information; if the value had been updated by another thread in |
<> | 160:d5399cc887bb | 269 | * the meantime, the write would fail due to a mismatched expectedCurrentValue. |
<> | 160:d5399cc887bb | 270 | * |
<> | 160:d5399cc887bb | 271 | * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect |
<> | 160:d5399cc887bb | 272 | * you to the article on compare-and swap]. |
<> | 160:d5399cc887bb | 273 | * |
<> | 160:d5399cc887bb | 274 | * @param ptr The target memory location. |
<> | 160:d5399cc887bb | 275 | * @param[in,out] expectedCurrentValue A pointer to some location holding the |
<> | 160:d5399cc887bb | 276 | * expected current value of the data being set atomically. |
<> | 160:d5399cc887bb | 277 | * The computed 'desiredValue' should be a function of this current value. |
AnnaBridge | 167:e84263d55307 | 278 | * @note: This is an in-out parameter. In the |
<> | 160:d5399cc887bb | 279 | * failure case of atomic_cas (where the |
<> | 160:d5399cc887bb | 280 | * destination isn't set), the pointee of expectedCurrentValue is |
<> | 160:d5399cc887bb | 281 | * updated with the current value. |
<> | 160:d5399cc887bb | 282 | * @param[in] desiredValue The new value computed based on '*expectedCurrentValue'. |
<> | 160:d5399cc887bb | 283 | * |
<> | 160:d5399cc887bb | 284 | * @return true if the memory location was atomically |
<> | 160:d5399cc887bb | 285 | * updated with the desired value (after verifying |
<> | 160:d5399cc887bb | 286 | * that it contained the expectedCurrentValue), |
<> | 160:d5399cc887bb | 287 | * false otherwise. In the failure case, |
<> | 160:d5399cc887bb | 288 | * exepctedCurrentValue is updated with the new |
<> | 160:d5399cc887bb | 289 | * value of the target memory location. |
<> | 160:d5399cc887bb | 290 | * |
<> | 160:d5399cc887bb | 291 | * pseudocode: |
<> | 160:d5399cc887bb | 292 | * function cas(p : pointer to int, old : pointer to int, new : int) returns bool { |
<> | 160:d5399cc887bb | 293 | * if *p != *old { |
<> | 160:d5399cc887bb | 294 | * *old = *p |
<> | 160:d5399cc887bb | 295 | * return false |
<> | 160:d5399cc887bb | 296 | * } |
<> | 160:d5399cc887bb | 297 | * *p = new |
<> | 160:d5399cc887bb | 298 | * return true |
<> | 160:d5399cc887bb | 299 | * } |
<> | 160:d5399cc887bb | 300 | * |
AnnaBridge | 167:e84263d55307 | 301 | * @note: In the failure case (where the destination isn't set), the value |
Anna Bridge |
180:96ed750bd169 | 302 | * pointed to by expectedCurrentValue is instead updated with the current value. |
<> | 160:d5399cc887bb | 303 | * This property helps writing concise code for the following incr: |
<> | 160:d5399cc887bb | 304 | * |
<> | 160:d5399cc887bb | 305 | * function incr(p : pointer to int, a : int) returns int { |
<> | 160:d5399cc887bb | 306 | * done = false |
<> | 160:d5399cc887bb | 307 | * value = *p // This fetch operation need not be atomic. |
<> | 160:d5399cc887bb | 308 | * while not done { |
<> | 160:d5399cc887bb | 309 | * done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success |
<> | 160:d5399cc887bb | 310 | * } |
<> | 160:d5399cc887bb | 311 | * return value + a |
<> | 160:d5399cc887bb | 312 | * } |
Anna Bridge |
180:96ed750bd169 | 313 | * |
Anna Bridge |
180:96ed750bd169 | 314 | * @note: This corresponds to the C11 "atomic_compare_exchange_strong" - it |
Anna Bridge |
180:96ed750bd169 | 315 | * always succeeds if the current value is expected, as per the pseudocode |
Anna Bridge |
180:96ed750bd169 | 316 | * above; it will not spuriously fail as "atomic_compare_exchange_weak" may. |
<> | 160:d5399cc887bb | 317 | */ |
AnnaBridge | 184:08ed48f1de7f | 318 | bool core_util_atomic_cas_ptr(void * volatile *ptr, void **expectedCurrentValue, void *desiredValue); |
<> | 160:d5399cc887bb | 319 | |
<> | 160:d5399cc887bb | 320 | /** |
<> | 160:d5399cc887bb | 321 | * Atomic increment. |
<> | 160:d5399cc887bb | 322 | * @param valuePtr Target memory location being incremented. |
<> | 160:d5399cc887bb | 323 | * @param delta The amount being incremented. |
<> | 160:d5399cc887bb | 324 | * @return The new incremented value. |
<> | 160:d5399cc887bb | 325 | */ |
AnnaBridge | 184:08ed48f1de7f | 326 | uint8_t core_util_atomic_incr_u8(volatile uint8_t *valuePtr, uint8_t delta); |
<> | 160:d5399cc887bb | 327 | |
<> | 160:d5399cc887bb | 328 | /** |
<> | 160:d5399cc887bb | 329 | * Atomic increment. |
<> | 160:d5399cc887bb | 330 | * @param valuePtr Target memory location being incremented. |
<> | 160:d5399cc887bb | 331 | * @param delta The amount being incremented. |
<> | 160:d5399cc887bb | 332 | * @return The new incremented value. |
<> | 160:d5399cc887bb | 333 | */ |
AnnaBridge | 184:08ed48f1de7f | 334 | uint16_t core_util_atomic_incr_u16(volatile uint16_t *valuePtr, uint16_t delta); |
<> | 160:d5399cc887bb | 335 | |
<> | 160:d5399cc887bb | 336 | /** |
<> | 160:d5399cc887bb | 337 | * Atomic increment. |
<> | 160:d5399cc887bb | 338 | * @param valuePtr Target memory location being incremented. |
<> | 160:d5399cc887bb | 339 | * @param delta The amount being incremented. |
<> | 160:d5399cc887bb | 340 | * @return The new incremented value. |
<> | 160:d5399cc887bb | 341 | */ |
AnnaBridge | 184:08ed48f1de7f | 342 | uint32_t core_util_atomic_incr_u32(volatile uint32_t *valuePtr, uint32_t delta); |
<> | 160:d5399cc887bb | 343 | |
<> | 160:d5399cc887bb | 344 | /** |
<> | 160:d5399cc887bb | 345 | * Atomic increment. |
<> | 160:d5399cc887bb | 346 | * @param valuePtr Target memory location being incremented. |
<> | 160:d5399cc887bb | 347 | * @param delta The amount being incremented in bytes. |
<> | 160:d5399cc887bb | 348 | * @return The new incremented value. |
<> | 160:d5399cc887bb | 349 | * |
<> | 160:d5399cc887bb | 350 | * @note The type of the pointer argument is not taken into account |
<> | 160:d5399cc887bb | 351 | * and the pointer is incremented by bytes. |
<> | 160:d5399cc887bb | 352 | */ |
AnnaBridge | 184:08ed48f1de7f | 353 | void *core_util_atomic_incr_ptr(void * volatile *valuePtr, ptrdiff_t delta); |
<> | 160:d5399cc887bb | 354 | |
<> | 160:d5399cc887bb | 355 | /** |
<> | 160:d5399cc887bb | 356 | * Atomic decrement. |
<> | 160:d5399cc887bb | 357 | * @param valuePtr Target memory location being decremented. |
<> | 160:d5399cc887bb | 358 | * @param delta The amount being decremented. |
<> | 160:d5399cc887bb | 359 | * @return The new decremented value. |
<> | 160:d5399cc887bb | 360 | */ |
AnnaBridge | 184:08ed48f1de7f | 361 | uint8_t core_util_atomic_decr_u8(volatile uint8_t *valuePtr, uint8_t delta); |
<> | 160:d5399cc887bb | 362 | |
<> | 160:d5399cc887bb | 363 | /** |
<> | 160:d5399cc887bb | 364 | * Atomic decrement. |
<> | 160:d5399cc887bb | 365 | * @param valuePtr Target memory location being decremented. |
<> | 160:d5399cc887bb | 366 | * @param delta The amount being decremented. |
<> | 160:d5399cc887bb | 367 | * @return The new decremented value. |
<> | 160:d5399cc887bb | 368 | */ |
AnnaBridge | 184:08ed48f1de7f | 369 | uint16_t core_util_atomic_decr_u16(volatile uint16_t *valuePtr, uint16_t delta); |
<> | 160:d5399cc887bb | 370 | |
<> | 160:d5399cc887bb | 371 | /** |
<> | 160:d5399cc887bb | 372 | * Atomic decrement. |
<> | 160:d5399cc887bb | 373 | * @param valuePtr Target memory location being decremented. |
<> | 160:d5399cc887bb | 374 | * @param delta The amount being decremented. |
<> | 160:d5399cc887bb | 375 | * @return The new decremented value. |
<> | 160:d5399cc887bb | 376 | */ |
AnnaBridge | 184:08ed48f1de7f | 377 | uint32_t core_util_atomic_decr_u32(volatile uint32_t *valuePtr, uint32_t delta); |
<> | 160:d5399cc887bb | 378 | |
<> | 160:d5399cc887bb | 379 | /** |
<> | 160:d5399cc887bb | 380 | * Atomic decrement. |
<> | 160:d5399cc887bb | 381 | * @param valuePtr Target memory location being decremented. |
<> | 160:d5399cc887bb | 382 | * @param delta The amount being decremented in bytes. |
<> | 160:d5399cc887bb | 383 | * @return The new decremented value. |
<> | 160:d5399cc887bb | 384 | * |
<> | 160:d5399cc887bb | 385 | * @note The type of the pointer argument is not taken into account |
<> | 160:d5399cc887bb | 386 | * and the pointer is decremented by bytes |
<> | 160:d5399cc887bb | 387 | */ |
AnnaBridge | 184:08ed48f1de7f | 388 | void *core_util_atomic_decr_ptr(void * volatile *valuePtr, ptrdiff_t delta); |
<> | 160:d5399cc887bb | 389 | |
<> | 160:d5399cc887bb | 390 | #ifdef __cplusplus |
<> | 160:d5399cc887bb | 391 | } // extern "C" |
<> | 160:d5399cc887bb | 392 | #endif |
AnnaBridge | 178:79309dc6340a | 393 | /**@}*/ |
<> | 160:d5399cc887bb | 394 | |
AnnaBridge | 178:79309dc6340a | 395 | /**@}*/ |
<> | 160:d5399cc887bb | 396 | |
<> | 160:d5399cc887bb | 397 | #endif // __MBED_UTIL_CRITICAL_H__ |
<> | 160:d5399cc887bb | 398 | |
AnnaBridge | 178:79309dc6340a | 399 | |
AnnaBridge | 178:79309dc6340a | 400 |