| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
/* |
| 4 |
Atomic load/store implementation on AVR. |
| 5 |
|
| 6 |
Operations not supported here are provided by disabling interrupts. |
| 7 |
See also src/imp/interrupt/avr.rs. |
| 8 |
|
| 9 |
See "Atomic operation overview by architecture" in atomic-maybe-uninit for a more comprehensive and |
| 10 |
detailed description of the atomic and synchronize instructions in this architecture: |
| 11 |
https://github.com/taiki-e/atomic-maybe-uninit/blob/HEAD/src/arch/README.md#avr |
| 12 |
|
| 13 |
Note: Ordering is always SeqCst. |
| 14 |
|
| 15 |
Refs: |
| 16 |
- AVR® Instruction Set Manual, Rev. DS40002198B |
| 17 |
https://ww1.microchip.com/downloads/en/DeviceDoc/AVR-InstructionSet-Manual-DS40002198.pdf |
| 18 |
- atomic-maybe-uninit |
| 19 |
https://github.com/taiki-e/atomic-maybe-uninit |
| 20 |
|
| 21 |
See tests/asm-test/asm/portable-atomic for generated assembly. |
| 22 |
*/ |
| 23 |
|
| 24 |
use core::{arch::asm, cell::UnsafeCell, sync::atomic::Ordering}; |
| 25 |
|
| 26 |
macro_rules! atomic8 { |
| 27 |
($atomic_type:ident, $value_type:ty) => { |
| 28 |
#[repr(transparent)] |
| 29 |
pub(crate) struct $atomic_type { |
| 30 |
v: UnsafeCell<$value_type>, |
| 31 |
} |
| 32 |
|
| 33 |
// Send is implicitly implemented for atomic integers, but not for atomic pointers. |
| 34 |
// SAFETY: any data races are prevented by atomic operations. |
| 35 |
unsafe impl Send for $atomic_type {} |
| 36 |
// SAFETY: any data races are prevented by atomic operations. |
| 37 |
unsafe impl Sync for $atomic_type {} |
| 38 |
|
| 39 |
impl $atomic_type { |
| 40 |
#[inline] |
| 41 |
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)] |
| 42 |
pub(crate) fn load(&self, order: Ordering) -> $value_type { |
| 43 |
crate::utils::assert_load_ordering(order); |
| 44 |
let src = self.v.get(); |
| 45 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 46 |
// pointer passed in is valid because we got it from a reference. |
| 47 |
unsafe { |
| 48 |
let out; |
| 49 |
asm!( |
| 50 |
"ld {out}, Z", // atomic { out = *Z } |
| 51 |
out = out(reg) out, |
| 52 |
in("Z") src, |
| 53 |
options(nostack, preserves_flags), |
| 54 |
); |
| 55 |
out |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
#[inline] |
| 60 |
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)] |
| 61 |
pub(crate) fn store(&self, val: $value_type, order: Ordering) { |
| 62 |
crate::utils::assert_store_ordering(order); |
| 63 |
let dst = self.v.get(); |
| 64 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 65 |
// pointer passed in is valid because we got it from a reference. |
| 66 |
unsafe { |
| 67 |
asm!( |
| 68 |
"st Z, {val}", // atomic { *Z = val } |
| 69 |
val = in(reg) val, |
| 70 |
in("Z") dst, |
| 71 |
options(nostack, preserves_flags), |
| 72 |
); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
#[cfg(any(target_feature = "rmw", portable_atomic_target_feature = "rmw"))] |
| 78 |
impl $atomic_type { |
| 79 |
#[inline] |
| 80 |
pub(crate) fn swap(&self, val: $value_type, _order: Ordering) -> $value_type { |
| 81 |
let dst = self.v.get(); |
| 82 |
let out; |
| 83 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 84 |
// pointer passed in is valid because we got it from a reference. |
| 85 |
// cfg guarantee that the CPU supports RMW instructions. |
| 86 |
unsafe { |
| 87 |
asm!( |
| 88 |
"xch Z, {val}", // atomic { _x = *Z; *Z = val; val = _x } |
| 89 |
val = inout(reg) val => out, |
| 90 |
in("Z") dst, |
| 91 |
options(nostack, preserves_flags), |
| 92 |
); |
| 93 |
} |
| 94 |
out |
| 95 |
} |
| 96 |
|
| 97 |
#[inline] |
| 98 |
pub(crate) fn fetch_and(&self, val: $value_type, _order: Ordering) -> $value_type { |
| 99 |
let dst = self.v.get(); |
| 100 |
let out; |
| 101 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 102 |
// pointer passed in is valid because we got it from a reference. |
| 103 |
// cfg guarantee that the CPU supports RMW instructions. |
| 104 |
unsafe { |
| 105 |
asm!( |
| 106 |
"lac Z, {val}", // atomic { _x = *Z; *Z &= !val; val = _x } |
| 107 |
val = inout(reg) !val => out, |
| 108 |
in("Z") dst, |
| 109 |
options(nostack, preserves_flags), |
| 110 |
); |
| 111 |
} |
| 112 |
out |
| 113 |
} |
| 114 |
|
| 115 |
#[inline] |
| 116 |
pub(crate) fn fetch_or(&self, val: $value_type, _order: Ordering) -> $value_type { |
| 117 |
let dst = self.v.get(); |
| 118 |
let out; |
| 119 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 120 |
// pointer passed in is valid because we got it from a reference. |
| 121 |
// cfg guarantee that the CPU supports RMW instructions. |
| 122 |
unsafe { |
| 123 |
asm!( |
| 124 |
"las Z, {val}", // atomic { _x = *Z; *Z |= val; val = _x } |
| 125 |
val = inout(reg) val => out, |
| 126 |
in("Z") dst, |
| 127 |
options(nostack, preserves_flags), |
| 128 |
); |
| 129 |
} |
| 130 |
out |
| 131 |
} |
| 132 |
|
| 133 |
#[inline] |
| 134 |
pub(crate) fn fetch_xor(&self, val: $value_type, _order: Ordering) -> $value_type { |
| 135 |
let dst = self.v.get(); |
| 136 |
let out; |
| 137 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 138 |
// pointer passed in is valid because we got it from a reference. |
| 139 |
// cfg guarantee that the CPU supports RMW instructions. |
| 140 |
unsafe { |
| 141 |
asm!( |
| 142 |
"lat Z, {val}", // atomic { _x = *Z; *Z ^= val; val = _x } |
| 143 |
val = inout(reg) val => out, |
| 144 |
in("Z") dst, |
| 145 |
options(nostack, preserves_flags), |
| 146 |
); |
| 147 |
} |
| 148 |
out |
| 149 |
} |
| 150 |
|
| 151 |
#[inline] |
| 152 |
pub(crate) fn fetch_not(&self, order: Ordering) -> $value_type { |
| 153 |
self.fetch_xor(!0, order) |
| 154 |
} |
| 155 |
} |
| 156 |
}; |
| 157 |
} |
| 158 |
|
| 159 |
atomic8!(AtomicI8, i8); |
| 160 |
atomic8!(AtomicU8, u8); |