blob: 557b6a0d771d2611fc55241dbf0c7ebd7a73f09c [file] [log] [blame]
Jakub Kotur2b588ff2020-12-21 17:28:14 +01001//! Miscellaneous utilities.
2
3use std::cell::{Cell, UnsafeCell};
4use std::num::Wrapping;
5use std::ops::{Deref, DerefMut};
6use std::sync::atomic::{AtomicBool, Ordering};
7use std::thread;
8use std::time::{Duration, Instant};
9
10use crossbeam_utils::Backoff;
11
12/// Randomly shuffles a slice.
Joel Galensonf6b6a422021-05-19 14:58:42 -070013pub(crate) fn shuffle<T>(v: &mut [T]) {
Jakub Kotur2b588ff2020-12-21 17:28:14 +010014 let len = v.len();
15 if len <= 1 {
16 return;
17 }
18
19 thread_local! {
20 static RNG: Cell<Wrapping<u32>> = Cell::new(Wrapping(1_406_868_647));
21 }
22
23 let _ = RNG.try_with(|rng| {
24 for i in 1..len {
25 // This is the 32-bit variant of Xorshift.
26 //
27 // Source: https://en.wikipedia.org/wiki/Xorshift
28 let mut x = rng.get();
29 x ^= x << 13;
30 x ^= x >> 17;
31 x ^= x << 5;
32 rng.set(x);
33
34 let x = x.0;
35 let n = i + 1;
36
37 // This is a fast alternative to `let j = x % n`.
38 //
39 // Author: Daniel Lemire
40 // Source: https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
41 let j = ((x as u64).wrapping_mul(n as u64) >> 32) as u32 as usize;
42
43 v.swap(i, j);
44 }
45 });
46}
47
48/// Sleeps until the deadline, or forever if the deadline isn't specified.
Joel Galensonf6b6a422021-05-19 14:58:42 -070049pub(crate) fn sleep_until(deadline: Option<Instant>) {
Jakub Kotur2b588ff2020-12-21 17:28:14 +010050 loop {
51 match deadline {
52 None => thread::sleep(Duration::from_secs(1000)),
53 Some(d) => {
54 let now = Instant::now();
55 if now >= d {
56 break;
57 }
58 thread::sleep(d - now);
59 }
60 }
61 }
62}
63
64/// A simple spinlock.
Joel Galensonf6b6a422021-05-19 14:58:42 -070065pub(crate) struct Spinlock<T> {
Jakub Kotur2b588ff2020-12-21 17:28:14 +010066 flag: AtomicBool,
67 value: UnsafeCell<T>,
68}
69
70impl<T> Spinlock<T> {
71 /// Returns a new spinlock initialized with `value`.
Joel Galensonf6b6a422021-05-19 14:58:42 -070072 pub(crate) fn new(value: T) -> Spinlock<T> {
Jakub Kotur2b588ff2020-12-21 17:28:14 +010073 Spinlock {
74 flag: AtomicBool::new(false),
75 value: UnsafeCell::new(value),
76 }
77 }
78
79 /// Locks the spinlock.
Joel Galensonf6b6a422021-05-19 14:58:42 -070080 pub(crate) fn lock(&self) -> SpinlockGuard<'_, T> {
Jakub Kotur2b588ff2020-12-21 17:28:14 +010081 let backoff = Backoff::new();
82 while self.flag.swap(true, Ordering::Acquire) {
83 backoff.snooze();
84 }
85 SpinlockGuard { parent: self }
86 }
87}
88
89/// A guard holding a spinlock locked.
Joel Galensonf6b6a422021-05-19 14:58:42 -070090pub(crate) struct SpinlockGuard<'a, T> {
Jakub Kotur2b588ff2020-12-21 17:28:14 +010091 parent: &'a Spinlock<T>,
92}
93
94impl<T> Drop for SpinlockGuard<'_, T> {
95 fn drop(&mut self) {
96 self.parent.flag.store(false, Ordering::Release);
97 }
98}
99
100impl<T> Deref for SpinlockGuard<'_, T> {
101 type Target = T;
102
103 fn deref(&self) -> &T {
104 unsafe { &*self.parent.value.get() }
105 }
106}
107
108impl<T> DerefMut for SpinlockGuard<'_, T> {
109 fn deref_mut(&mut self) -> &mut T {
110 unsafe { &mut *self.parent.value.get() }
111 }
112}