Debian Codegraph @

third_party/rust/atomic/src/fallback.rs

raw
1 // Copyright 2016 Amanieu d'Antras
2 //
3 // Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4 // http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5 // http://opensource.org/licenses/MIT>, at your option. This file may not be
6 // copied, modified, or distributed except according to those terms.
7
8 use core::cmp;
9 use core::mem;
10 use core::num::Wrapping;
11 use core::ops;
12 use core::ptr;
13 use core::slice;
14 use core::sync::atomic::{self, AtomicUsize, Ordering};
15
16 // We use an AtomicUsize instead of an AtomicBool because it performs better
17 // on architectures that don't have byte-sized atomics.
18 //
19 // We give each spinlock its own cache line to avoid false sharing.
20 #[repr(align(64))]
21 struct SpinLock(AtomicUsize);
22
23 impl SpinLock {
24 fn lock(&self) {
25 while self
26 .0
27 .compare_exchange_weak(0, 1, Ordering::Acquire, Ordering::Relaxed)
28 .is_err()
29 {
30 while self.0.load(Ordering::Relaxed) != 0 {
31 atomic::spin_loop_hint();
32 }
33 }
34 }
35
36 fn unlock(&self) {
37 self.0.store(0, Ordering::Release);
38 }
39 }
40
41 // A big array of spinlocks which we use to guard atomic accesses. A spinlock is
42 // chosen based on a hash of the address of the atomic object, which helps to
43 // reduce contention compared to a single global lock.
44 macro_rules! array {
45 (@accum (0, $($_es:expr),*) -> ($($body:tt)*))
46 => {array!(@as_expr [$($body)*])};
47 (@accum (1, $($es:expr),*) -> ($($body:tt)*))
48 => {array!(@accum (0, $($es),*) -> ($($body)* $($es,)*))};
49 (@accum (2, $($es:expr),*) -> ($($body:tt)*))
50 => {array!(@accum (0, $($es),*) -> ($($body)* $($es,)* $($es,)*))};
51 (@accum (4, $($es:expr),*) -> ($($body:tt)*))
52 => {array!(@accum (2, $($es,)* $($es),*) -> ($($body)*))};
53 (@accum (8, $($es:expr),*) -> ($($body:tt)*))
54 => {array!(@accum (4, $($es,)* $($es),*) -> ($($body)*))};
55 (@accum (16, $($es:expr),*) -> ($($body:tt)*))
56 => {array!(@accum (8, $($es,)* $($es),*) -> ($($body)*))};
57 (@accum (32, $($es:expr),*) -> ($($body:tt)*))
58 => {array!(@accum (16, $($es,)* $($es),*) -> ($($body)*))};
59 (@accum (64, $($es:expr),*) -> ($($body:tt)*))
60 => {array!(@accum (32, $($es,)* $($es),*) -> ($($body)*))};
61
62 (@as_expr $e:expr) => {$e};
63
64 [$e:expr; $n:tt] => { array!(@accum ($n, $e) -> ()) };
65 }
66 static SPINLOCKS: [SpinLock; 64] = array![SpinLock(AtomicUsize::new(0)); 64];
67
68 // Spinlock pointer hashing function from compiler-rt
69 #[inline]
70 fn lock_for_addr(addr: usize) -> &'static SpinLock {
71 // Disregard the lowest 4 bits. We want all values that may be part of the
72 // same memory operation to hash to the same value and therefore use the same
73 // lock.
74 let mut hash = addr >> 4;
75 // Use the next bits as the basis for the hash
76 let low = hash & (SPINLOCKS.len() - 1);
77 // Now use the high(er) set of bits to perturb the hash, so that we don't
78 // get collisions from atomic fields in a single object
79 hash >>= 16;
80 hash ^= low;
81 // Return a pointer to the lock to use
82 &SPINLOCKS[hash & (SPINLOCKS.len() - 1)]
83 }
84
85 #[inline]
86 fn lock(addr: usize) -> LockGuard {
87 let lock = lock_for_addr(addr);
88 lock.lock();
89 LockGuard(lock)
90 }
91
92 struct LockGuard(&'static SpinLock);
93 impl Drop for LockGuard {
94 #[inline]
95 fn drop(&mut self) {
96 self.0.unlock();
97 }
98 }
99
100 #[inline]
101 pub unsafe fn atomic_load<T>(dst: *mut T) -> T {
102 let _l = lock(dst as usize);
103 ptr::read(dst)
104 }
105
106 #[inline]
107 pub unsafe fn atomic_store<T>(dst: *mut T, val: T) {
108 let _l = lock(dst as usize);
109 ptr::write(dst, val);
110 }
111
112 #[inline]
113 pub unsafe fn atomic_swap<T>(dst: *mut T, val: T) -> T {
114 let _l = lock(dst as usize);
115 ptr::replace(dst, val)
116 }
117
118 #[inline]
119 pub unsafe fn atomic_compare_exchange<T>(dst: *mut T, current: T, new: T) -> Result<T, T> {
120 let _l = lock(dst as usize);
121 let result = ptr::read(dst);
122 // compare_exchange compares with memcmp instead of Eq
123 let a = slice::from_raw_parts(&result as *const _ as *const u8, mem::size_of_val(&result));
124 let b = slice::from_raw_parts(
125 &current as *const _ as *const u8,
126 mem::size_of_val(&current),
127 );
128 if a == b {
129 ptr::write(dst, new);
130 Ok(result)
131 } else {
132 Err(result)
133 }
134 }
135
136 #[inline]
137 pub unsafe fn atomic_add<T: Copy>(dst: *mut T, val: T) -> T
138 where
139 Wrapping<T>: ops::Add<Output = Wrapping<T>>,
140 {
141 let _l = lock(dst as usize);
142 let result = ptr::read(dst);
143 ptr::write(dst, (Wrapping(result) + Wrapping(val)).0);
144 result
145 }
146
147 #[inline]
148 pub unsafe fn atomic_sub<T: Copy>(dst: *mut T, val: T) -> T
149 where
150 Wrapping<T>: ops::Sub<Output = Wrapping<T>>,
151 {
152 let _l = lock(dst as usize);
153 let result = ptr::read(dst);
154 ptr::write(dst, (Wrapping(result) - Wrapping(val)).0);
155 result
156 }
157
158 #[inline]
159 pub unsafe fn atomic_and<T: Copy + ops::BitAnd<Output = T>>(dst: *mut T, val: T) -> T {
160 let _l = lock(dst as usize);
161 let result = ptr::read(dst);
162 ptr::write(dst, result & val);
163 result
164 }
165
166 #[inline]
167 pub unsafe fn atomic_or<T: Copy + ops::BitOr<Output = T>>(dst: *mut T, val: T) -> T {
168 let _l = lock(dst as usize);
169 let result = ptr::read(dst);
170 ptr::write(dst, result | val);
171 result
172 }
173
174 #[inline]
175 pub unsafe fn atomic_xor<T: Copy + ops::BitXor<Output = T>>(dst: *mut T, val: T) -> T {
176 let _l = lock(dst as usize);
177 let result = ptr::read(dst);
178 ptr::write(dst, result ^ val);
179 result
180 }
181
182 #[inline]
183 pub unsafe fn atomic_min<T: Copy + cmp::Ord>(dst: *mut T, val: T) -> T {
184 let _l = lock(dst as usize);
185 let result = ptr::read(dst);
186 ptr::write(dst, cmp::min(result, val));
187 result
188 }
189
190 #[inline]
191 pub unsafe fn atomic_max<T: Copy + cmp::Ord>(dst: *mut T, val: T) -> T {
192 let _l = lock(dst as usize);
193 let result = ptr::read(dst);
194 ptr::write(dst, cmp::max(result, val));
195 result
196 }