Skip to main content

rc_event_queue/sync/
build.rs

1#[cfg(loom)]
2pub(crate) use loom::sync::atomic::{AtomicPtr, AtomicUsize, AtomicU64, Ordering};
3
4#[cfg(loom)]
5pub(crate) use loom::sync::Arc;
6
7#[cfg(loom)]
8#[derive(Debug)]
9pub struct Mutex<T>(loom::sync::Mutex<T>);
10
11#[cfg(loom)]
12impl<T> Mutex<T>{
13    pub fn new(data: T) -> Self {
14        Self(loom::sync::Mutex::new(data))
15    }
16
17    pub fn lock(&self) -> loom::sync::MutexGuard<'_, T> {
18        self.0.lock().unwrap()
19    }
20
21    pub fn as_mut_ptr(&self) -> *mut T {
22        self.data_ptr()
23    }
24
25    pub fn get_mut(&mut self) -> &mut T {
26        // There is no way to get without lock in loom
27        unsafe{
28            use std::ops::DerefMut;
29            &mut *(self.0.lock().unwrap().deref_mut() as *mut T)
30        }
31    }
32
33    pub fn data_ptr(&self) -> *mut T {
34        // There is no way to get without lock in loom
35        use std::ops::DerefMut;
36        self.0.lock().unwrap().deref_mut() as *mut T
37    }
38}
39
40#[cfg(loom)]
41pub type SpinMutex<T> = Mutex<T>;
42
43#[cfg(loom)]
44pub(crate) type SpinSharedMutex<T> = loom::sync::RwLock<T>;
45
46// ==========================================================================================
47
48#[cfg(not(loom))]
49pub(crate) use std::sync::atomic::{AtomicPtr, AtomicUsize, AtomicU64, Ordering};
50
51#[cfg(not(loom))]
52pub(crate) use std::sync::Arc;
53
54#[cfg(not(loom))]
55//pub(crate) use parking_lot::{Mutex};
56pub(crate) type Mutex<T> = lock_api::Mutex<spin::mutex::Mutex<(), spin::relax::Yield>, T>;
57
58#[cfg(not(loom))]
59pub(crate) use spin::mutex::{SpinMutex};
60
61#[cfg(not(loom))]
62pub(crate) use spin::rwlock::RwLock as SpinSharedMutex;