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