提交 58deb700 编写于 作者: M Mara Bos

Make it possible to have unboxed mutexes on specific platforms.

This commit keeps all mutexes boxed on all platforms, but makes it
trivial to remove the box on some platforms later.
上级 a8c2d4fc
......@@ -15,6 +15,8 @@
// implemented identically.
pub struct Mutex(RWLock);
pub type MovableMutex = Box<Mutex>;
pub unsafe fn raw(m: &Mutex) -> *mut AtomicU32 {
rwlock::raw(&m.0)
}
......
......@@ -8,6 +8,8 @@ pub struct Mutex {
inner: SpinMutex<WaitVariable<bool>>,
}
pub type MovableMutex = Box<Mutex>;
// Implementation according to “Operating Systems: Three Easy Pieces”, chapter 28
impl Mutex {
pub const fn new() -> Mutex {
......
......@@ -5,6 +5,8 @@ pub struct Mutex {
inner: UnsafeCell<libc::pthread_mutex_t>,
}
pub type MovableMutex = Box<Mutex>;
#[inline]
pub unsafe fn raw(m: &Mutex) -> *mut libc::pthread_mutex_t {
m.inner.get()
......
......@@ -4,6 +4,8 @@ pub struct Mutex {
locked: UnsafeCell<bool>,
}
pub type MovableMutex = Box<Mutex>;
unsafe impl Send for Mutex {}
unsafe impl Sync for Mutex {} // no threads on this platform
......
......@@ -5,6 +5,8 @@ pub struct Mutex {
inner: UnsafeCell<libc::pthread_mutex_t>,
}
pub type MovableMutex = Box<Mutex>;
#[inline]
pub unsafe fn raw(m: &Mutex) -> *mut libc::pthread_mutex_t {
m.inner.get()
......
......@@ -8,6 +8,8 @@ pub struct Mutex {
locked: AtomicUsize,
}
pub type MovableMutex = Box<Mutex>;
// Mutexes have a pretty simple implementation where they contain an `i32`
// internally that is 0 when unlocked and 1 when the mutex is locked.
// Acquisition has a fast path where it attempts to cmpxchg the 0 to a 1, and
......
......@@ -29,6 +29,8 @@ pub struct Mutex {
lock: AtomicUsize,
}
pub type MovableMutex = Box<Mutex>;
unsafe impl Send for Mutex {}
unsafe impl Sync for Mutex {}
......
use crate::sys::condvar as imp;
use crate::sys::mutex as mutex_imp;
use crate::sys_common::mutex::MovableMutex;
use crate::time::Duration;
use check::CondvarCheck;
mod check;
type CondvarCheck = <mutex_imp::MovableMutex as check::CondvarCheck>::Check;
/// An OS-based condition variable.
pub struct Condvar {
inner: Box<imp::Condvar>,
......
......@@ -2,13 +2,22 @@
use crate::sys::mutex as mutex_imp;
use crate::sys_common::mutex::MovableMutex;
/// A `Condvar` will check it's only ever used with the same mutex, based on
/// its (stable) address.
pub struct CondvarCheck {
pub trait CondvarCheck {
type Check;
}
/// For boxed mutexes, a `Condvar` will check it's only ever used with the same
/// mutex, based on its (stable) address.
impl CondvarCheck for Box<mutex_imp::Mutex> {
type Check = SameMutexCheck;
}
pub struct SameMutexCheck {
addr: AtomicUsize,
}
impl CondvarCheck {
#[allow(dead_code)]
impl SameMutexCheck {
pub const fn new() -> Self {
Self { addr: AtomicUsize::new(0) }
}
......@@ -21,3 +30,19 @@ pub fn verify(&self, mutex: &MovableMutex) {
}
}
}
/// Unboxed mutexes may move, so `Condvar` can not require its address to stay
/// constant.
impl CondvarCheck for mutex_imp::Mutex {
type Check = NoCheck;
}
pub struct NoCheck;
#[allow(dead_code)]
impl NoCheck {
pub const fn new() -> Self {
Self
}
pub fn verify(&self, _: &MovableMutex) {}
}
......@@ -58,16 +58,17 @@ fn drop(&mut self) {
///
/// This mutex does not implement poisoning.
///
/// This is a wrapper around `Box<imp::Mutex>`, to allow the object to be moved
/// without moving the raw mutex.
pub struct MovableMutex(Box<imp::Mutex>);
/// This is either a wrapper around `Box<imp::Mutex>` or `imp::Mutex`,
/// depending on the platform. It is boxed on platforms where `imp::Mutex` may
/// not be moved.
pub struct MovableMutex(imp::MovableMutex);
unsafe impl Sync for MovableMutex {}
impl MovableMutex {
/// Creates a new mutex.
pub fn new() -> Self {
let mut mutex = box imp::Mutex::new();
let mut mutex = imp::MovableMutex::from(imp::Mutex::new());
unsafe { mutex.init() };
Self(mutex)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册