#[cfg(target_os = "linux")]
pub(crate) use inheriting::{Guard, Lock};
#[cfg(not(target_os = "linux"))]
pub(crate) use plain::{Guard, Lock};
#[cfg(target_os = "linux")]
#[allow(unsafe_code)]
mod inheriting {
use std::cell::UnsafeCell;
use std::io;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::sync::atomic::{AtomicU32, Ordering};
thread_local! {
static THREAD: u32 = unsafe { libc::gettid() } as u32;
}
pub(crate) struct Lock<T> {
word: AtomicU32,
value: UnsafeCell<T>,
}
unsafe impl<T: Send> Send for Lock<T> {}
unsafe impl<T: Send> Sync for Lock<T> {}
pub(crate) struct Guard<'a, T> {
lock: &'a Lock<T>,
_thread: PhantomData<*const ()>,
}
impl<T: Default> Default for Lock<T> {
fn default() -> Self {
Self::new(T::default())
}
}
impl<T> Lock<T> {
pub(crate) const fn new(value: T) -> Self {
Self {
word: AtomicU32::new(0),
value: UnsafeCell::new(value),
}
}
pub(crate) fn lock(&self) -> Guard<'_, T> {
let thread = THREAD.with(|thread| *thread);
if self
.word
.compare_exchange(0, thread, Ordering::Acquire, Ordering::Relaxed)
.is_err()
{
loop {
if futex(&self.word, libc::FUTEX_LOCK_PI) == 0 {
break;
}
let error = io::Error::last_os_error();
if !matches!(error.raw_os_error(), Some(libc::EAGAIN | libc::EINTR)) {
panic!("could not take a lock: {error}");
}
}
}
Guard {
lock: self,
_thread: PhantomData,
}
}
}
impl<T> Drop for Guard<'_, T> {
fn drop(&mut self) {
let thread = THREAD.with(|thread| *thread);
if self
.lock
.word
.compare_exchange(thread, 0, Ordering::Release, Ordering::Relaxed)
.is_err()
{
futex(&self.lock.word, libc::FUTEX_UNLOCK_PI);
}
}
}
impl<T> Deref for Guard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.lock.value.get() }
}
}
impl<T> DerefMut for Guard<'_, T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.lock.value.get() }
}
}
fn futex(word: &AtomicU32, operation: libc::c_int) -> libc::c_long {
unsafe {
libc::syscall(
libc::SYS_futex,
word.as_ptr(),
operation | libc::FUTEX_PRIVATE_FLAG,
0,
std::ptr::null::<libc::timespec>(),
)
}
}
}
#[cfg(not(target_os = "linux"))]
mod plain {
use std::sync::{Mutex, MutexGuard, PoisonError};
pub(crate) type Guard<'a, T> = MutexGuard<'a, T>;
#[derive(Default)]
pub(crate) struct Lock<T>(Mutex<T>);
impl<T> Lock<T> {
pub(crate) const fn new(value: T) -> Self {
Self(Mutex::new(value))
}
pub(crate) fn lock(&self) -> Guard<'_, T> {
self.0.lock().unwrap_or_else(PoisonError::into_inner)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn threads_take_turns() {
let count = Lock::new(0u64);
std::thread::scope(|scope| {
for _ in 0..4 {
scope.spawn(|| {
for _ in 0..100_000 {
*count.lock() += 1;
}
});
}
});
assert_eq!(*count.lock(), 400_000);
}
#[test]
fn a_waiting_thread_takes_it_once_let_go() {
let held = Lock::new(Vec::new());
let guard = held.lock();
std::thread::scope(|scope| {
let waiting = scope.spawn(|| held.lock().push(2));
std::thread::sleep(std::time::Duration::from_millis(50));
let mut guard = guard;
guard.push(1);
drop(guard);
waiting.join().unwrap();
});
assert_eq!(*held.lock(), [1, 2]);
}
}