sel4_shared_memory/atomic_ops/
mod.rs1use core::sync::atomic::{self, Ordering};
8
9use zerocopy::{FromBytes, IntoBytes};
10
11use sel4_abstract_ptr::memory_type::AtomicOps;
12
13use crate::SharedMemory;
14
15mod generic;
16mod ordering;
17
18#[allow(private_bounds)]
19pub trait Atomic: AtomicSealed {
20 type Value: Copy + FromBytes + IntoBytes;
21}
22
23trait AtomicSealed {
24 const IS_SIGNED: bool;
25}
26
27macro_rules! impl_atomic {
28 (
29 $atomic:path,
30 $value:ty,
31 $target_has_atomic_key:literal,
32 $is_signed:literal
33 ) => {
34 #[cfg(target_has_atomic = $target_has_atomic_key)]
35 impl Atomic for $atomic {
36 type Value = $value;
37 }
38
39 #[cfg(target_has_atomic = $target_has_atomic_key)]
40 impl AtomicSealed for $atomic {
41 const IS_SIGNED: bool = $is_signed;
42 }
43
44 #[cfg(target_has_atomic = $target_has_atomic_key)]
45 #[cfg(target_has_atomic_primitive_alignment = $target_has_atomic_key)]
46 impl Atomic for $value {
47 type Value = $value;
48 }
49
50 #[cfg(target_has_atomic = $target_has_atomic_key)]
51 #[cfg(target_has_atomic_primitive_alignment = $target_has_atomic_key)]
52 impl AtomicSealed for $value {
53 const IS_SIGNED: bool = $is_signed;
54 }
55 };
56}
57
58macro_rules! impl_atomic_for_each_signedness {
59 (
60 $value_unsigned:ty,
61 $value_signed:ty,
62 $target_has_atomic_key:literal,
63 $atomic_unsigned:path,
64 $atomic_signed:path
65 ) => {
66 impl_atomic!(
67 $atomic_unsigned,
68 $value_unsigned,
69 $target_has_atomic_key,
70 false
71 );
72 impl_atomic!($atomic_signed, $value_signed, $target_has_atomic_key, true);
73 };
74}
75
76impl_atomic_for_each_signedness!(u8, i8, "8", atomic::AtomicU8, atomic::AtomicI8);
77impl_atomic_for_each_signedness!(u16, i16, "16", atomic::AtomicU16, atomic::AtomicI16);
78impl_atomic_for_each_signedness!(u32, i32, "32", atomic::AtomicU32, atomic::AtomicI32);
79impl_atomic_for_each_signedness!(u64, i64, "64", atomic::AtomicU64, atomic::AtomicI64);
80
81#[cfg(target_pointer_width = "32")]
82impl_atomic_for_each_signedness!(usize, isize, "32", atomic::AtomicUsize, atomic::AtomicIsize);
83
84#[cfg(target_pointer_width = "64")]
85impl_atomic_for_each_signedness!(usize, isize, "64", atomic::AtomicUsize, atomic::AtomicIsize);
86
87impl<T: Atomic> AtomicOps<T> for SharedMemory {
88 type Value = T::Value;
89
90 #[inline]
91 unsafe fn atomic_store(dst: *mut T, val: Self::Value, order: Ordering) {
92 unsafe { generic::atomic_store(dst.cast(), val, order.into()) }
93 }
94
95 #[inline]
96 unsafe fn atomic_load(dst: *const T, order: Ordering) -> Self::Value {
97 unsafe { generic::atomic_load(dst.cast(), order.into()) }
98 }
99
100 #[inline]
101 unsafe fn atomic_swap(dst: *mut T, val: Self::Value, order: Ordering) -> Self::Value {
102 unsafe { generic::atomic_swap(dst.cast(), val, order.into()) }
103 }
104
105 #[inline]
106 unsafe fn atomic_add(dst: *mut T, val: Self::Value, order: Ordering) -> Self::Value {
107 unsafe { generic::atomic_add(dst.cast(), val, order.into()) }
108 }
109
110 #[inline]
111 unsafe fn atomic_sub(dst: *mut T, val: Self::Value, order: Ordering) -> Self::Value {
112 unsafe { generic::atomic_sub(dst.cast(), val, order.into()) }
113 }
114
115 #[inline]
116 unsafe fn atomic_compare_exchange(
117 dst: *mut T,
118 old: Self::Value,
119 new: Self::Value,
120 success: Ordering,
121 failure: Ordering,
122 ) -> Result<Self::Value, Self::Value> {
123 unsafe {
124 generic::atomic_compare_exchange(dst.cast(), old, new, success.into(), failure.into())
125 }
126 }
127
128 #[inline]
129 unsafe fn atomic_compare_exchange_weak(
130 dst: *mut T,
131 old: Self::Value,
132 new: Self::Value,
133 success: Ordering,
134 failure: Ordering,
135 ) -> Result<Self::Value, Self::Value> {
136 unsafe {
137 generic::atomic_compare_exchange_weak(
138 dst.cast(),
139 old,
140 new,
141 success.into(),
142 failure.into(),
143 )
144 }
145 }
146
147 #[inline]
148 unsafe fn atomic_and(dst: *mut T, val: Self::Value, order: Ordering) -> Self::Value {
149 unsafe { generic::atomic_and(dst.cast(), val, order.into()) }
150 }
151
152 #[inline]
153 unsafe fn atomic_nand(dst: *mut T, val: Self::Value, order: Ordering) -> Self::Value {
154 unsafe { generic::atomic_nand(dst.cast(), val, order.into()) }
155 }
156
157 #[inline]
158 unsafe fn atomic_or(dst: *mut T, val: Self::Value, order: Ordering) -> Self::Value {
159 unsafe { generic::atomic_or(dst.cast(), val, order.into()) }
160 }
161
162 #[inline]
163 unsafe fn atomic_xor(dst: *mut T, val: Self::Value, order: Ordering) -> Self::Value {
164 unsafe { generic::atomic_xor(dst.cast(), val, order.into()) }
165 }
166
167 #[inline]
168 unsafe fn atomic_max(dst: *mut T, val: Self::Value, order: Ordering) -> Self::Value {
169 unsafe {
170 if T::IS_SIGNED {
171 generic::atomic_max(dst.cast(), val, order.into())
172 } else {
173 generic::atomic_umax(dst.cast(), val, order.into())
174 }
175 }
176 }
177
178 #[inline]
179 unsafe fn atomic_min(dst: *mut T, val: Self::Value, order: Ordering) -> Self::Value {
180 unsafe {
181 if T::IS_SIGNED {
182 generic::atomic_min(dst.cast(), val, order.into())
183 } else {
184 generic::atomic_umin(dst.cast(), val, order.into())
185 }
186 }
187 }
188}