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