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