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