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