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