Debian Codegraph @

debian/rust-vendor/portable-atomic/src/imp/atomic64/macros.rs

raw
1 // SPDX-License-Identifier: Apache-2.0 OR MIT
2
3 macro_rules! atomic64 {
4 ($atomic_type:ident, $int_type:ident, $atomic_max:ident, $atomic_min:ident) => {
5 #[repr(C, align(8))]
6 pub(crate) struct $atomic_type {
7 v: core::cell::UnsafeCell<$int_type>,
8 }
9
10 // Send is implicitly implemented.
11 // SAFETY: any data races are prevented by atomic intrinsics, the kernel user helper, or the lock.
12 unsafe impl Sync for $atomic_type {}
13
14 impl_default_no_fetch_ops!($atomic_type, $int_type);
15 impl_default_bit_opts!($atomic_type, $int_type);
16 impl $atomic_type {
17 #[inline]
18 pub(crate) const fn new(v: $int_type) -> Self {
19 Self { v: core::cell::UnsafeCell::new(v) }
20 }
21
22 #[inline]
23 pub(crate) fn is_lock_free() -> bool {
24 is_lock_free()
25 }
26 pub(crate) const IS_ALWAYS_LOCK_FREE: bool = IS_ALWAYS_LOCK_FREE;
27
28 #[inline]
29 #[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
30 pub(crate) fn load(&self, order: Ordering) -> $int_type {
31 crate::utils::assert_load_ordering(order);
32 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
33 // SAFETY: any data races are prevented by atomic intrinsics, the kernel user helper, or the lock
34 // and the raw pointer passed in is valid because we got it from a reference.
35 unsafe {
36 atomic_load(self.v.get().cast::<u64>(), order) as $int_type
37 }
38 }
39
40 #[inline]
41 #[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
42 pub(crate) fn store(&self, val: $int_type, order: Ordering) {
43 crate::utils::assert_store_ordering(order);
44 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
45 // SAFETY: any data races are prevented by atomic intrinsics, the kernel user helper, or the lock
46 // and the raw pointer passed in is valid because we got it from a reference.
47 unsafe {
48 atomic_store(self.v.get().cast::<u64>(), val as u64, order)
49 }
50 }
51
52 #[inline]
53 pub(crate) fn swap(&self, val: $int_type, order: Ordering) -> $int_type {
54 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
55 // SAFETY: any data races are prevented by atomic intrinsics, the kernel user helper, or the lock
56 // and the raw pointer passed in is valid because we got it from a reference.
57 unsafe {
58 atomic_swap(self.v.get().cast::<u64>(), val as u64, order) as $int_type
59 }
60 }
61
62 #[inline]
63 #[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
64 pub(crate) fn compare_exchange(
65 &self,
66 current: $int_type,
67 new: $int_type,
68 success: Ordering,
69 failure: Ordering,
70 ) -> Result<$int_type, $int_type> {
71 crate::utils::assert_compare_exchange_ordering(success, failure);
72 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
73 // SAFETY: any data races are prevented by atomic intrinsics, the kernel user helper, or the lock
74 // and the raw pointer passed in is valid because we got it from a reference.
75 unsafe {
76 match atomic_compare_exchange(
77 self.v.get().cast::<u64>(),
78 current as u64,
79 new as u64,
80 success,
81 failure,
82 ) {
83 Ok(v) => Ok(v as $int_type),
84 Err(v) => Err(v as $int_type),
85 }
86 }
87 }
88
89 #[inline]
90 #[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
91 pub(crate) fn compare_exchange_weak(
92 &self,
93 current: $int_type,
94 new: $int_type,
95 success: Ordering,
96 failure: Ordering,
97 ) -> Result<$int_type, $int_type> {
98 crate::utils::assert_compare_exchange_ordering(success, failure);
99 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
100 // SAFETY: any data races are prevented by atomic intrinsics, the kernel user helper, or the lock
101 // and the raw pointer passed in is valid because we got it from a reference.
102 unsafe {
103 match atomic_compare_exchange_weak(
104 self.v.get().cast::<u64>(),
105 current as u64,
106 new as u64,
107 success,
108 failure,
109 ) {
110 Ok(v) => Ok(v as $int_type),
111 Err(v) => Err(v as $int_type),
112 }
113 }
114 }
115
116 #[inline]
117 pub(crate) fn fetch_add(&self, val: $int_type, order: Ordering) -> $int_type {
118 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
119 // SAFETY: any data races are prevented by atomic intrinsics, the kernel user helper, or the lock
120 // and the raw pointer passed in is valid because we got it from a reference.
121 unsafe {
122 atomic_add(self.v.get().cast::<u64>(), val as u64, order) as $int_type
123 }
124 }
125
126 #[inline]
127 pub(crate) fn fetch_sub(&self, val: $int_type, order: Ordering) -> $int_type {
128 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
129 // SAFETY: any data races are prevented by atomic intrinsics, the kernel user helper, or the lock
130 // and the raw pointer passed in is valid because we got it from a reference.
131 unsafe {
132 atomic_sub(self.v.get().cast::<u64>(), val as u64, order) as $int_type
133 }
134 }
135
136 #[inline]
137 pub(crate) fn fetch_and(&self, val: $int_type, order: Ordering) -> $int_type {
138 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
139 // SAFETY: any data races are prevented by atomic intrinsics, the kernel user helper, or the lock
140 // and the raw pointer passed in is valid because we got it from a reference.
141 unsafe {
142 atomic_and(self.v.get().cast::<u64>(), val as u64, order) as $int_type
143 }
144 }
145
146 #[inline]
147 pub(crate) fn fetch_nand(&self, val: $int_type, order: Ordering) -> $int_type {
148 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
149 // SAFETY: any data races are prevented by atomic intrinsics, the kernel user helper, or the lock
150 // and the raw pointer passed in is valid because we got it from a reference.
151 unsafe {
152 atomic_nand(self.v.get().cast::<u64>(), val as u64, order) as $int_type
153 }
154 }
155
156 #[inline]
157 pub(crate) fn fetch_or(&self, val: $int_type, order: Ordering) -> $int_type {
158 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
159 // SAFETY: any data races are prevented by atomic intrinsics, the kernel user helper, or the lock
160 // and the raw pointer passed in is valid because we got it from a reference.
161 unsafe {
162 atomic_or(self.v.get().cast::<u64>(), val as u64, order) as $int_type
163 }
164 }
165
166 #[inline]
167 pub(crate) fn fetch_xor(&self, val: $int_type, order: Ordering) -> $int_type {
168 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
169 // SAFETY: any data races are prevented by atomic intrinsics, the kernel user helper, or the lock
170 // and the raw pointer passed in is valid because we got it from a reference.
171 unsafe {
172 atomic_xor(self.v.get().cast::<u64>(), val as u64, order) as $int_type
173 }
174 }
175
176 #[inline]
177 pub(crate) fn fetch_max(&self, val: $int_type, order: Ordering) -> $int_type {
178 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
179 // SAFETY: any data races are prevented by atomic intrinsics, the kernel user helper, or the lock
180 // and the raw pointer passed in is valid because we got it from a reference.
181 unsafe {
182 $atomic_max(self.v.get().cast::<u64>(), val as u64, order) as $int_type
183 }
184 }
185
186 #[inline]
187 pub(crate) fn fetch_min(&self, val: $int_type, order: Ordering) -> $int_type {
188 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
189 // SAFETY: any data races are prevented by atomic intrinsics, the kernel user helper, or the lock
190 // and the raw pointer passed in is valid because we got it from a reference.
191 unsafe {
192 $atomic_min(self.v.get().cast::<u64>(), val as u64, order) as $int_type
193 }
194 }
195
196 #[inline]
197 pub(crate) fn fetch_not(&self, order: Ordering) -> $int_type {
198 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
199 // SAFETY: any data races are prevented by atomic intrinsics, the kernel user helper, or the lock
200 // and the raw pointer passed in is valid because we got it from a reference.
201 unsafe {
202 atomic_not(self.v.get().cast::<u64>(), order) as $int_type
203 }
204 }
205 #[inline]
206 pub(crate) fn not(&self, order: Ordering) {
207 self.fetch_not(order);
208 }
209
210 #[inline]
211 pub(crate) fn fetch_neg(&self, order: Ordering) -> $int_type {
212 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
213 // SAFETY: any data races are prevented by atomic intrinsics, the kernel user helper, or the lock
214 // and the raw pointer passed in is valid because we got it from a reference.
215 unsafe {
216 atomic_neg(self.v.get().cast::<u64>(), order) as $int_type
217 }
218 }
219 #[inline]
220 pub(crate) fn neg(&self, order: Ordering) {
221 self.fetch_neg(order);
222 }
223
224 #[inline]
225 pub(crate) const fn as_ptr(&self) -> *mut $int_type {
226 self.v.get()
227 }
228 }
229 };
230 }