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