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