Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,14 @@ cfg_if::cfg_if! {

#[cfg(feature = "std")]
mod lock {
use std::boxed::Box;
use std::cell::Cell;
use std::ptr;
use std::sync::{Mutex, MutexGuard, Once};
use std::sync::{Mutex, MutexGuard};

/// A "Maybe" LockGuard
pub struct LockGuard(Option<MutexGuard<'static, ()>>);

/// The global lock, lazily allocated on first use
static mut LOCK: *mut Mutex<()> = ptr::null_mut();
static INIT: Once = Once::new();
/// The global lock
static LOCK: Mutex<()> = Mutex::new(());
// Whether this thread is the one that holds the lock
thread_local!(static LOCK_HELD: Cell<bool> = const { Cell::new(false) });

Expand Down Expand Up @@ -226,14 +223,8 @@ mod lock {
// Insist that we totally are the thread holding the lock
// (our thread will block until we are)
LOCK_HELD.with(|s| s.set(true));
unsafe {
// lazily allocate the lock if necessary
INIT.call_once(|| {
LOCK = Box::into_raw(Box::new(Mutex::new(())));
});
// ok *actually* try to acquire the lock, blocking as necessary
LockGuard(Some((*LOCK).lock().unwrap()))
}
// ok *actually* try to acquire the lock, blocking as necessary
LockGuard(Some(LOCK.lock().unwrap()))
}
}

Expand Down
Loading