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