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