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