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