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