Skip to main content

lockless_datastructures/
lib.rs

1use std::ops::{Deref, DerefMut};
2
3mod atomic_ring_buffer_mpmc;
4mod atomic_ring_buffer_spsc;
5mod mutex_ring_buffer;
6mod primitives;
7mod render;
8
9pub use self::atomic_ring_buffer_mpmc::AtomicRingBufferMpmc;
10pub use self::atomic_ring_buffer_spsc::AtomicRingBufferSpsc;
11pub use self::mutex_ring_buffer::MutexRingBuffer;
12
13///Use to prevent cache line collision!
14#[derive(Debug, Default)]
15#[repr(align(64))]
16pub struct Padded<T>(pub T);
17impl<T> Deref for Padded<T> {
18    type Target = T;
19    fn deref(&self) -> &Self::Target {
20        &self.0
21    }
22}
23
24impl<T> DerefMut for Padded<T> {
25    fn deref_mut(&mut self) -> &mut Self::Target {
26        &mut self.0
27    }
28}
29
30use std::hint;
31use std::thread;
32///An exponential backoff
33pub struct Backoff {
34    step: u32,
35}
36impl Default for Backoff {
37    fn default() -> Self {
38        Self::new()
39    }
40}
41impl Backoff {
42    pub fn new() -> Self {
43        Self { step: 0 }
44    }
45    ///Call this where you want to backoff!
46    #[inline]
47    pub fn snooze(&mut self) {
48        if self.step <= 6 {
49            for _ in 0..(1 << self.step) {
50                hint::spin_loop();
51            }
52        } else {
53            thread::yield_now();
54        }
55
56        if self.step <= 6 {
57            self.step += 1;
58        }
59    }
60
61    #[inline]
62    pub fn reset(&mut self) {
63        self.step = 0;
64    }
65}