test test test

Dependencies:   mbed

Committer:
mohamedmoawya
Date:
Mon May 25 19:06:11 2020 +0000
Revision:
0:e4c5e6ec922e
snake game tteest

Who changed what in which revision?

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