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