Marco Zecchini
/
Example_RTOS
Rtos API example
mbed-os/platform/mbed_critical.c@0:9fca2b23d0ba, 2019-02-23 (annotated)
- Committer:
- marcozecchini
- Date:
- Sat Feb 23 12:13:36 2019 +0000
- Revision:
- 0:9fca2b23d0ba
final commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcozecchini | 0:9fca2b23d0ba | 1 | /* |
marcozecchini | 0:9fca2b23d0ba | 2 | * Copyright (c) 2015-2016, ARM Limited, All Rights Reserved |
marcozecchini | 0:9fca2b23d0ba | 3 | * SPDX-License-Identifier: Apache-2.0 |
marcozecchini | 0:9fca2b23d0ba | 4 | * |
marcozecchini | 0:9fca2b23d0ba | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
marcozecchini | 0:9fca2b23d0ba | 6 | * not use this file except in compliance with the License. |
marcozecchini | 0:9fca2b23d0ba | 7 | * You may obtain a copy of the License at |
marcozecchini | 0:9fca2b23d0ba | 8 | * |
marcozecchini | 0:9fca2b23d0ba | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
marcozecchini | 0:9fca2b23d0ba | 10 | * |
marcozecchini | 0:9fca2b23d0ba | 11 | * Unless required by applicable law or agreed to in writing, software |
marcozecchini | 0:9fca2b23d0ba | 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
marcozecchini | 0:9fca2b23d0ba | 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
marcozecchini | 0:9fca2b23d0ba | 14 | * See the License for the specific language governing permissions and |
marcozecchini | 0:9fca2b23d0ba | 15 | * limitations under the License. |
marcozecchini | 0:9fca2b23d0ba | 16 | */ |
marcozecchini | 0:9fca2b23d0ba | 17 | |
marcozecchini | 0:9fca2b23d0ba | 18 | /* Declare __STDC_LIMIT_MACROS so stdint.h defines UINT32_MAX when using C++ */ |
marcozecchini | 0:9fca2b23d0ba | 19 | #define __STDC_LIMIT_MACROS |
marcozecchini | 0:9fca2b23d0ba | 20 | #include "platform/mbed_critical.h" |
marcozecchini | 0:9fca2b23d0ba | 21 | |
marcozecchini | 0:9fca2b23d0ba | 22 | #include "cmsis.h" |
marcozecchini | 0:9fca2b23d0ba | 23 | #include "platform/mbed_assert.h" |
marcozecchini | 0:9fca2b23d0ba | 24 | #include "platform/mbed_toolchain.h" |
marcozecchini | 0:9fca2b23d0ba | 25 | |
marcozecchini | 0:9fca2b23d0ba | 26 | static volatile uint32_t interrupt_enable_counter = 0; |
marcozecchini | 0:9fca2b23d0ba | 27 | static volatile bool critical_interrupts_disabled = false; |
marcozecchini | 0:9fca2b23d0ba | 28 | |
marcozecchini | 0:9fca2b23d0ba | 29 | bool core_util_are_interrupts_enabled(void) |
marcozecchini | 0:9fca2b23d0ba | 30 | { |
marcozecchini | 0:9fca2b23d0ba | 31 | #if defined(__CORTEX_A9) |
marcozecchini | 0:9fca2b23d0ba | 32 | return ((__get_CPSR() & 0x80) == 0); |
marcozecchini | 0:9fca2b23d0ba | 33 | #else |
marcozecchini | 0:9fca2b23d0ba | 34 | return ((__get_PRIMASK() & 0x1) == 0); |
marcozecchini | 0:9fca2b23d0ba | 35 | #endif |
marcozecchini | 0:9fca2b23d0ba | 36 | } |
marcozecchini | 0:9fca2b23d0ba | 37 | |
marcozecchini | 0:9fca2b23d0ba | 38 | bool core_util_is_isr_active(void) |
marcozecchini | 0:9fca2b23d0ba | 39 | { |
marcozecchini | 0:9fca2b23d0ba | 40 | #if defined(__CORTEX_A9) |
marcozecchini | 0:9fca2b23d0ba | 41 | switch(__get_CPSR() & 0x1FU) { |
marcozecchini | 0:9fca2b23d0ba | 42 | case CPSR_M_USR: |
marcozecchini | 0:9fca2b23d0ba | 43 | case CPSR_M_SYS: |
marcozecchini | 0:9fca2b23d0ba | 44 | return false; |
marcozecchini | 0:9fca2b23d0ba | 45 | case CPSR_M_SVC: |
marcozecchini | 0:9fca2b23d0ba | 46 | default: |
marcozecchini | 0:9fca2b23d0ba | 47 | return true; |
marcozecchini | 0:9fca2b23d0ba | 48 | } |
marcozecchini | 0:9fca2b23d0ba | 49 | #else |
marcozecchini | 0:9fca2b23d0ba | 50 | return (__get_IPSR() != 0U); |
marcozecchini | 0:9fca2b23d0ba | 51 | #endif |
marcozecchini | 0:9fca2b23d0ba | 52 | } |
marcozecchini | 0:9fca2b23d0ba | 53 | |
marcozecchini | 0:9fca2b23d0ba | 54 | MBED_WEAK void core_util_critical_section_enter(void) |
marcozecchini | 0:9fca2b23d0ba | 55 | { |
marcozecchini | 0:9fca2b23d0ba | 56 | bool interrupts_disabled = !core_util_are_interrupts_enabled(); |
marcozecchini | 0:9fca2b23d0ba | 57 | __disable_irq(); |
marcozecchini | 0:9fca2b23d0ba | 58 | |
marcozecchini | 0:9fca2b23d0ba | 59 | /* Save the interrupt disabled state as it was prior to any nested critical section lock use */ |
marcozecchini | 0:9fca2b23d0ba | 60 | if (!interrupt_enable_counter) { |
marcozecchini | 0:9fca2b23d0ba | 61 | critical_interrupts_disabled = interrupts_disabled; |
marcozecchini | 0:9fca2b23d0ba | 62 | } |
marcozecchini | 0:9fca2b23d0ba | 63 | |
marcozecchini | 0:9fca2b23d0ba | 64 | /* If the interrupt_enable_counter overflows or we are in a nested critical section and interrupts |
marcozecchini | 0:9fca2b23d0ba | 65 | are enabled, then something has gone badly wrong thus assert an error. |
marcozecchini | 0:9fca2b23d0ba | 66 | */ |
marcozecchini | 0:9fca2b23d0ba | 67 | MBED_ASSERT(interrupt_enable_counter < UINT32_MAX); |
marcozecchini | 0:9fca2b23d0ba | 68 | // FIXME |
marcozecchini | 0:9fca2b23d0ba | 69 | #ifndef FEATURE_UVISOR |
marcozecchini | 0:9fca2b23d0ba | 70 | if (interrupt_enable_counter > 0) { |
marcozecchini | 0:9fca2b23d0ba | 71 | MBED_ASSERT(interrupts_disabled); |
marcozecchini | 0:9fca2b23d0ba | 72 | } |
marcozecchini | 0:9fca2b23d0ba | 73 | #else |
marcozecchini | 0:9fca2b23d0ba | 74 | #warning "core_util_critical_section_enter needs fixing to work from unprivileged code" |
marcozecchini | 0:9fca2b23d0ba | 75 | #endif /* FEATURE_UVISOR */ |
marcozecchini | 0:9fca2b23d0ba | 76 | interrupt_enable_counter++; |
marcozecchini | 0:9fca2b23d0ba | 77 | } |
marcozecchini | 0:9fca2b23d0ba | 78 | |
marcozecchini | 0:9fca2b23d0ba | 79 | MBED_WEAK void core_util_critical_section_exit(void) |
marcozecchini | 0:9fca2b23d0ba | 80 | { |
marcozecchini | 0:9fca2b23d0ba | 81 | /* If critical_section_enter has not previously been called, do nothing */ |
marcozecchini | 0:9fca2b23d0ba | 82 | if (interrupt_enable_counter) { |
marcozecchini | 0:9fca2b23d0ba | 83 | |
marcozecchini | 0:9fca2b23d0ba | 84 | // FIXME |
marcozecchini | 0:9fca2b23d0ba | 85 | #ifndef FEATURE_UVISOR |
marcozecchini | 0:9fca2b23d0ba | 86 | bool interrupts_disabled = !core_util_are_interrupts_enabled(); /* get the current interrupt disabled state */ |
marcozecchini | 0:9fca2b23d0ba | 87 | |
marcozecchini | 0:9fca2b23d0ba | 88 | MBED_ASSERT(interrupts_disabled); /* Interrupts must be disabled on invoking an exit from a critical section */ |
marcozecchini | 0:9fca2b23d0ba | 89 | #else |
marcozecchini | 0:9fca2b23d0ba | 90 | #warning "core_util_critical_section_exit needs fixing to work from unprivileged code" |
marcozecchini | 0:9fca2b23d0ba | 91 | #endif /* FEATURE_UVISOR */ |
marcozecchini | 0:9fca2b23d0ba | 92 | |
marcozecchini | 0:9fca2b23d0ba | 93 | interrupt_enable_counter--; |
marcozecchini | 0:9fca2b23d0ba | 94 | |
marcozecchini | 0:9fca2b23d0ba | 95 | /* Only re-enable interrupts if we are exiting the last of the nested critical sections and |
marcozecchini | 0:9fca2b23d0ba | 96 | interrupts were enabled on entry to the first critical section. |
marcozecchini | 0:9fca2b23d0ba | 97 | */ |
marcozecchini | 0:9fca2b23d0ba | 98 | if (!interrupt_enable_counter && !critical_interrupts_disabled) { |
marcozecchini | 0:9fca2b23d0ba | 99 | __enable_irq(); |
marcozecchini | 0:9fca2b23d0ba | 100 | } |
marcozecchini | 0:9fca2b23d0ba | 101 | } |
marcozecchini | 0:9fca2b23d0ba | 102 | } |
marcozecchini | 0:9fca2b23d0ba | 103 | |
marcozecchini | 0:9fca2b23d0ba | 104 | #if __EXCLUSIVE_ACCESS |
marcozecchini | 0:9fca2b23d0ba | 105 | |
marcozecchini | 0:9fca2b23d0ba | 106 | /* Supress __ldrex and __strex deprecated warnings - "#3731-D: intrinsic is deprecated" */ |
marcozecchini | 0:9fca2b23d0ba | 107 | #if defined (__CC_ARM) |
marcozecchini | 0:9fca2b23d0ba | 108 | #pragma diag_suppress 3731 |
marcozecchini | 0:9fca2b23d0ba | 109 | #endif |
marcozecchini | 0:9fca2b23d0ba | 110 | |
marcozecchini | 0:9fca2b23d0ba | 111 | bool core_util_atomic_cas_u8(uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue) |
marcozecchini | 0:9fca2b23d0ba | 112 | { |
marcozecchini | 0:9fca2b23d0ba | 113 | uint8_t currentValue = __LDREXB((volatile uint8_t*)ptr); |
marcozecchini | 0:9fca2b23d0ba | 114 | if (currentValue != *expectedCurrentValue) { |
marcozecchini | 0:9fca2b23d0ba | 115 | *expectedCurrentValue = currentValue; |
marcozecchini | 0:9fca2b23d0ba | 116 | __CLREX(); |
marcozecchini | 0:9fca2b23d0ba | 117 | return false; |
marcozecchini | 0:9fca2b23d0ba | 118 | } |
marcozecchini | 0:9fca2b23d0ba | 119 | |
marcozecchini | 0:9fca2b23d0ba | 120 | return !__STREXB(desiredValue, (volatile uint8_t*)ptr); |
marcozecchini | 0:9fca2b23d0ba | 121 | } |
marcozecchini | 0:9fca2b23d0ba | 122 | |
marcozecchini | 0:9fca2b23d0ba | 123 | bool core_util_atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue) |
marcozecchini | 0:9fca2b23d0ba | 124 | { |
marcozecchini | 0:9fca2b23d0ba | 125 | uint16_t currentValue = __LDREXH((volatile uint16_t*)ptr); |
marcozecchini | 0:9fca2b23d0ba | 126 | if (currentValue != *expectedCurrentValue) { |
marcozecchini | 0:9fca2b23d0ba | 127 | *expectedCurrentValue = currentValue; |
marcozecchini | 0:9fca2b23d0ba | 128 | __CLREX(); |
marcozecchini | 0:9fca2b23d0ba | 129 | return false; |
marcozecchini | 0:9fca2b23d0ba | 130 | } |
marcozecchini | 0:9fca2b23d0ba | 131 | |
marcozecchini | 0:9fca2b23d0ba | 132 | return !__STREXH(desiredValue, (volatile uint16_t*)ptr); |
marcozecchini | 0:9fca2b23d0ba | 133 | } |
marcozecchini | 0:9fca2b23d0ba | 134 | |
marcozecchini | 0:9fca2b23d0ba | 135 | |
marcozecchini | 0:9fca2b23d0ba | 136 | bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue) |
marcozecchini | 0:9fca2b23d0ba | 137 | { |
marcozecchini | 0:9fca2b23d0ba | 138 | uint32_t currentValue = __LDREXW((volatile uint32_t*)ptr); |
marcozecchini | 0:9fca2b23d0ba | 139 | if (currentValue != *expectedCurrentValue) { |
marcozecchini | 0:9fca2b23d0ba | 140 | *expectedCurrentValue = currentValue; |
marcozecchini | 0:9fca2b23d0ba | 141 | __CLREX(); |
marcozecchini | 0:9fca2b23d0ba | 142 | return false; |
marcozecchini | 0:9fca2b23d0ba | 143 | } |
marcozecchini | 0:9fca2b23d0ba | 144 | |
marcozecchini | 0:9fca2b23d0ba | 145 | return !__STREXW(desiredValue, (volatile uint32_t*)ptr); |
marcozecchini | 0:9fca2b23d0ba | 146 | } |
marcozecchini | 0:9fca2b23d0ba | 147 | |
marcozecchini | 0:9fca2b23d0ba | 148 | uint8_t core_util_atomic_incr_u8(uint8_t *valuePtr, uint8_t delta) |
marcozecchini | 0:9fca2b23d0ba | 149 | { |
marcozecchini | 0:9fca2b23d0ba | 150 | uint8_t newValue; |
marcozecchini | 0:9fca2b23d0ba | 151 | do { |
marcozecchini | 0:9fca2b23d0ba | 152 | newValue = __LDREXB((volatile uint8_t*)valuePtr) + delta; |
marcozecchini | 0:9fca2b23d0ba | 153 | } while (__STREXB(newValue, (volatile uint8_t*)valuePtr)); |
marcozecchini | 0:9fca2b23d0ba | 154 | return newValue; |
marcozecchini | 0:9fca2b23d0ba | 155 | } |
marcozecchini | 0:9fca2b23d0ba | 156 | |
marcozecchini | 0:9fca2b23d0ba | 157 | uint16_t core_util_atomic_incr_u16(uint16_t *valuePtr, uint16_t delta) |
marcozecchini | 0:9fca2b23d0ba | 158 | { |
marcozecchini | 0:9fca2b23d0ba | 159 | uint16_t newValue; |
marcozecchini | 0:9fca2b23d0ba | 160 | do { |
marcozecchini | 0:9fca2b23d0ba | 161 | newValue = __LDREXH((volatile uint16_t*)valuePtr) + delta; |
marcozecchini | 0:9fca2b23d0ba | 162 | } while (__STREXH(newValue, (volatile uint16_t*)valuePtr)); |
marcozecchini | 0:9fca2b23d0ba | 163 | return newValue; |
marcozecchini | 0:9fca2b23d0ba | 164 | } |
marcozecchini | 0:9fca2b23d0ba | 165 | |
marcozecchini | 0:9fca2b23d0ba | 166 | uint32_t core_util_atomic_incr_u32(uint32_t *valuePtr, uint32_t delta) |
marcozecchini | 0:9fca2b23d0ba | 167 | { |
marcozecchini | 0:9fca2b23d0ba | 168 | uint32_t newValue; |
marcozecchini | 0:9fca2b23d0ba | 169 | do { |
marcozecchini | 0:9fca2b23d0ba | 170 | newValue = __LDREXW((volatile uint32_t*)valuePtr) + delta; |
marcozecchini | 0:9fca2b23d0ba | 171 | } while (__STREXW(newValue, (volatile uint32_t*)valuePtr)); |
marcozecchini | 0:9fca2b23d0ba | 172 | return newValue; |
marcozecchini | 0:9fca2b23d0ba | 173 | } |
marcozecchini | 0:9fca2b23d0ba | 174 | |
marcozecchini | 0:9fca2b23d0ba | 175 | |
marcozecchini | 0:9fca2b23d0ba | 176 | uint8_t core_util_atomic_decr_u8(uint8_t *valuePtr, uint8_t delta) |
marcozecchini | 0:9fca2b23d0ba | 177 | { |
marcozecchini | 0:9fca2b23d0ba | 178 | uint8_t newValue; |
marcozecchini | 0:9fca2b23d0ba | 179 | do { |
marcozecchini | 0:9fca2b23d0ba | 180 | newValue = __LDREXB((volatile uint8_t*)valuePtr) - delta; |
marcozecchini | 0:9fca2b23d0ba | 181 | } while (__STREXB(newValue, (volatile uint8_t*)valuePtr)); |
marcozecchini | 0:9fca2b23d0ba | 182 | return newValue; |
marcozecchini | 0:9fca2b23d0ba | 183 | } |
marcozecchini | 0:9fca2b23d0ba | 184 | |
marcozecchini | 0:9fca2b23d0ba | 185 | uint16_t core_util_atomic_decr_u16(uint16_t *valuePtr, uint16_t delta) |
marcozecchini | 0:9fca2b23d0ba | 186 | { |
marcozecchini | 0:9fca2b23d0ba | 187 | uint16_t newValue; |
marcozecchini | 0:9fca2b23d0ba | 188 | do { |
marcozecchini | 0:9fca2b23d0ba | 189 | newValue = __LDREXH((volatile uint16_t*)valuePtr) - delta; |
marcozecchini | 0:9fca2b23d0ba | 190 | } while (__STREXH(newValue, (volatile uint16_t*)valuePtr)); |
marcozecchini | 0:9fca2b23d0ba | 191 | return newValue; |
marcozecchini | 0:9fca2b23d0ba | 192 | } |
marcozecchini | 0:9fca2b23d0ba | 193 | |
marcozecchini | 0:9fca2b23d0ba | 194 | uint32_t core_util_atomic_decr_u32(uint32_t *valuePtr, uint32_t delta) |
marcozecchini | 0:9fca2b23d0ba | 195 | { |
marcozecchini | 0:9fca2b23d0ba | 196 | uint32_t newValue; |
marcozecchini | 0:9fca2b23d0ba | 197 | do { |
marcozecchini | 0:9fca2b23d0ba | 198 | newValue = __LDREXW((volatile uint32_t*)valuePtr) - delta; |
marcozecchini | 0:9fca2b23d0ba | 199 | } while (__STREXW(newValue, (volatile uint32_t*)valuePtr)); |
marcozecchini | 0:9fca2b23d0ba | 200 | return newValue; |
marcozecchini | 0:9fca2b23d0ba | 201 | } |
marcozecchini | 0:9fca2b23d0ba | 202 | |
marcozecchini | 0:9fca2b23d0ba | 203 | #else |
marcozecchini | 0:9fca2b23d0ba | 204 | |
marcozecchini | 0:9fca2b23d0ba | 205 | bool core_util_atomic_cas_u8(uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue) |
marcozecchini | 0:9fca2b23d0ba | 206 | { |
marcozecchini | 0:9fca2b23d0ba | 207 | bool success; |
marcozecchini | 0:9fca2b23d0ba | 208 | uint8_t currentValue; |
marcozecchini | 0:9fca2b23d0ba | 209 | core_util_critical_section_enter(); |
marcozecchini | 0:9fca2b23d0ba | 210 | currentValue = *ptr; |
marcozecchini | 0:9fca2b23d0ba | 211 | if (currentValue == *expectedCurrentValue) { |
marcozecchini | 0:9fca2b23d0ba | 212 | *ptr = desiredValue; |
marcozecchini | 0:9fca2b23d0ba | 213 | success = true; |
marcozecchini | 0:9fca2b23d0ba | 214 | } else { |
marcozecchini | 0:9fca2b23d0ba | 215 | *expectedCurrentValue = currentValue; |
marcozecchini | 0:9fca2b23d0ba | 216 | success = false; |
marcozecchini | 0:9fca2b23d0ba | 217 | } |
marcozecchini | 0:9fca2b23d0ba | 218 | core_util_critical_section_exit(); |
marcozecchini | 0:9fca2b23d0ba | 219 | return success; |
marcozecchini | 0:9fca2b23d0ba | 220 | } |
marcozecchini | 0:9fca2b23d0ba | 221 | |
marcozecchini | 0:9fca2b23d0ba | 222 | bool core_util_atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue) |
marcozecchini | 0:9fca2b23d0ba | 223 | { |
marcozecchini | 0:9fca2b23d0ba | 224 | bool success; |
marcozecchini | 0:9fca2b23d0ba | 225 | uint16_t currentValue; |
marcozecchini | 0:9fca2b23d0ba | 226 | core_util_critical_section_enter(); |
marcozecchini | 0:9fca2b23d0ba | 227 | currentValue = *ptr; |
marcozecchini | 0:9fca2b23d0ba | 228 | if (currentValue == *expectedCurrentValue) { |
marcozecchini | 0:9fca2b23d0ba | 229 | *ptr = desiredValue; |
marcozecchini | 0:9fca2b23d0ba | 230 | success = true; |
marcozecchini | 0:9fca2b23d0ba | 231 | } else { |
marcozecchini | 0:9fca2b23d0ba | 232 | *expectedCurrentValue = currentValue; |
marcozecchini | 0:9fca2b23d0ba | 233 | success = false; |
marcozecchini | 0:9fca2b23d0ba | 234 | } |
marcozecchini | 0:9fca2b23d0ba | 235 | core_util_critical_section_exit(); |
marcozecchini | 0:9fca2b23d0ba | 236 | return success; |
marcozecchini | 0:9fca2b23d0ba | 237 | } |
marcozecchini | 0:9fca2b23d0ba | 238 | |
marcozecchini | 0:9fca2b23d0ba | 239 | |
marcozecchini | 0:9fca2b23d0ba | 240 | bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue) |
marcozecchini | 0:9fca2b23d0ba | 241 | { |
marcozecchini | 0:9fca2b23d0ba | 242 | bool success; |
marcozecchini | 0:9fca2b23d0ba | 243 | uint32_t currentValue; |
marcozecchini | 0:9fca2b23d0ba | 244 | core_util_critical_section_enter(); |
marcozecchini | 0:9fca2b23d0ba | 245 | currentValue = *ptr; |
marcozecchini | 0:9fca2b23d0ba | 246 | if (currentValue == *expectedCurrentValue) { |
marcozecchini | 0:9fca2b23d0ba | 247 | *ptr = desiredValue; |
marcozecchini | 0:9fca2b23d0ba | 248 | success = true; |
marcozecchini | 0:9fca2b23d0ba | 249 | } else { |
marcozecchini | 0:9fca2b23d0ba | 250 | *expectedCurrentValue = currentValue; |
marcozecchini | 0:9fca2b23d0ba | 251 | success = false; |
marcozecchini | 0:9fca2b23d0ba | 252 | } |
marcozecchini | 0:9fca2b23d0ba | 253 | core_util_critical_section_exit(); |
marcozecchini | 0:9fca2b23d0ba | 254 | return success; |
marcozecchini | 0:9fca2b23d0ba | 255 | } |
marcozecchini | 0:9fca2b23d0ba | 256 | |
marcozecchini | 0:9fca2b23d0ba | 257 | |
marcozecchini | 0:9fca2b23d0ba | 258 | uint8_t core_util_atomic_incr_u8(uint8_t *valuePtr, uint8_t delta) |
marcozecchini | 0:9fca2b23d0ba | 259 | { |
marcozecchini | 0:9fca2b23d0ba | 260 | uint8_t newValue; |
marcozecchini | 0:9fca2b23d0ba | 261 | core_util_critical_section_enter(); |
marcozecchini | 0:9fca2b23d0ba | 262 | newValue = *valuePtr + delta; |
marcozecchini | 0:9fca2b23d0ba | 263 | *valuePtr = newValue; |
marcozecchini | 0:9fca2b23d0ba | 264 | core_util_critical_section_exit(); |
marcozecchini | 0:9fca2b23d0ba | 265 | return newValue; |
marcozecchini | 0:9fca2b23d0ba | 266 | } |
marcozecchini | 0:9fca2b23d0ba | 267 | |
marcozecchini | 0:9fca2b23d0ba | 268 | uint16_t core_util_atomic_incr_u16(uint16_t *valuePtr, uint16_t delta) |
marcozecchini | 0:9fca2b23d0ba | 269 | { |
marcozecchini | 0:9fca2b23d0ba | 270 | uint16_t newValue; |
marcozecchini | 0:9fca2b23d0ba | 271 | core_util_critical_section_enter(); |
marcozecchini | 0:9fca2b23d0ba | 272 | newValue = *valuePtr + delta; |
marcozecchini | 0:9fca2b23d0ba | 273 | *valuePtr = newValue; |
marcozecchini | 0:9fca2b23d0ba | 274 | core_util_critical_section_exit(); |
marcozecchini | 0:9fca2b23d0ba | 275 | return newValue; |
marcozecchini | 0:9fca2b23d0ba | 276 | } |
marcozecchini | 0:9fca2b23d0ba | 277 | |
marcozecchini | 0:9fca2b23d0ba | 278 | uint32_t core_util_atomic_incr_u32(uint32_t *valuePtr, uint32_t delta) |
marcozecchini | 0:9fca2b23d0ba | 279 | { |
marcozecchini | 0:9fca2b23d0ba | 280 | uint32_t newValue; |
marcozecchini | 0:9fca2b23d0ba | 281 | core_util_critical_section_enter(); |
marcozecchini | 0:9fca2b23d0ba | 282 | newValue = *valuePtr + delta; |
marcozecchini | 0:9fca2b23d0ba | 283 | *valuePtr = newValue; |
marcozecchini | 0:9fca2b23d0ba | 284 | core_util_critical_section_exit(); |
marcozecchini | 0:9fca2b23d0ba | 285 | return newValue; |
marcozecchini | 0:9fca2b23d0ba | 286 | } |
marcozecchini | 0:9fca2b23d0ba | 287 | |
marcozecchini | 0:9fca2b23d0ba | 288 | |
marcozecchini | 0:9fca2b23d0ba | 289 | uint8_t core_util_atomic_decr_u8(uint8_t *valuePtr, uint8_t delta) |
marcozecchini | 0:9fca2b23d0ba | 290 | { |
marcozecchini | 0:9fca2b23d0ba | 291 | uint8_t newValue; |
marcozecchini | 0:9fca2b23d0ba | 292 | core_util_critical_section_enter(); |
marcozecchini | 0:9fca2b23d0ba | 293 | newValue = *valuePtr - delta; |
marcozecchini | 0:9fca2b23d0ba | 294 | *valuePtr = newValue; |
marcozecchini | 0:9fca2b23d0ba | 295 | core_util_critical_section_exit(); |
marcozecchini | 0:9fca2b23d0ba | 296 | return newValue; |
marcozecchini | 0:9fca2b23d0ba | 297 | } |
marcozecchini | 0:9fca2b23d0ba | 298 | |
marcozecchini | 0:9fca2b23d0ba | 299 | uint16_t core_util_atomic_decr_u16(uint16_t *valuePtr, uint16_t delta) |
marcozecchini | 0:9fca2b23d0ba | 300 | { |
marcozecchini | 0:9fca2b23d0ba | 301 | uint16_t newValue; |
marcozecchini | 0:9fca2b23d0ba | 302 | core_util_critical_section_enter(); |
marcozecchini | 0:9fca2b23d0ba | 303 | newValue = *valuePtr - delta; |
marcozecchini | 0:9fca2b23d0ba | 304 | *valuePtr = newValue; |
marcozecchini | 0:9fca2b23d0ba | 305 | core_util_critical_section_exit(); |
marcozecchini | 0:9fca2b23d0ba | 306 | return newValue; |
marcozecchini | 0:9fca2b23d0ba | 307 | } |
marcozecchini | 0:9fca2b23d0ba | 308 | |
marcozecchini | 0:9fca2b23d0ba | 309 | uint32_t core_util_atomic_decr_u32(uint32_t *valuePtr, uint32_t delta) |
marcozecchini | 0:9fca2b23d0ba | 310 | { |
marcozecchini | 0:9fca2b23d0ba | 311 | uint32_t newValue; |
marcozecchini | 0:9fca2b23d0ba | 312 | core_util_critical_section_enter(); |
marcozecchini | 0:9fca2b23d0ba | 313 | newValue = *valuePtr - delta; |
marcozecchini | 0:9fca2b23d0ba | 314 | *valuePtr = newValue; |
marcozecchini | 0:9fca2b23d0ba | 315 | core_util_critical_section_exit(); |
marcozecchini | 0:9fca2b23d0ba | 316 | return newValue; |
marcozecchini | 0:9fca2b23d0ba | 317 | } |
marcozecchini | 0:9fca2b23d0ba | 318 | |
marcozecchini | 0:9fca2b23d0ba | 319 | #endif |
marcozecchini | 0:9fca2b23d0ba | 320 | |
marcozecchini | 0:9fca2b23d0ba | 321 | |
marcozecchini | 0:9fca2b23d0ba | 322 | bool core_util_atomic_cas_ptr(void **ptr, void **expectedCurrentValue, void *desiredValue) { |
marcozecchini | 0:9fca2b23d0ba | 323 | return core_util_atomic_cas_u32( |
marcozecchini | 0:9fca2b23d0ba | 324 | (uint32_t *)ptr, |
marcozecchini | 0:9fca2b23d0ba | 325 | (uint32_t *)expectedCurrentValue, |
marcozecchini | 0:9fca2b23d0ba | 326 | (uint32_t)desiredValue); |
marcozecchini | 0:9fca2b23d0ba | 327 | } |
marcozecchini | 0:9fca2b23d0ba | 328 | |
marcozecchini | 0:9fca2b23d0ba | 329 | void *core_util_atomic_incr_ptr(void **valuePtr, ptrdiff_t delta) { |
marcozecchini | 0:9fca2b23d0ba | 330 | return (void *)core_util_atomic_incr_u32((uint32_t *)valuePtr, (uint32_t)delta); |
marcozecchini | 0:9fca2b23d0ba | 331 | } |
marcozecchini | 0:9fca2b23d0ba | 332 | |
marcozecchini | 0:9fca2b23d0ba | 333 | void *core_util_atomic_decr_ptr(void **valuePtr, ptrdiff_t delta) { |
marcozecchini | 0:9fca2b23d0ba | 334 | return (void *)core_util_atomic_decr_u32((uint32_t *)valuePtr, (uint32_t)delta); |
marcozecchini | 0:9fca2b23d0ba | 335 | } |
marcozecchini | 0:9fca2b23d0ba | 336 |