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