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