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