diff options
| author | Botahamec <botahamec@outlook.com> | 2022-10-27 17:38:50 -0400 |
|---|---|---|
| committer | Botahamec <botahamec@outlook.com> | 2022-10-27 17:38:50 -0400 |
| commit | d2c3a6c6688685d466b69df94d610948d8657c31 (patch) | |
| tree | 646003e2c783ad3bb570e76989b0efba160c4bea | |
| parent | c2ec772dac7cf2a488af539613bf31ed15dfbaa5 (diff) | |
Remove the mutex
| -rw-r--r-- | Cargo.toml | 3 | ||||
| -rw-r--r-- | src/lib.rs | 25 |
2 files changed, 8 insertions, 20 deletions
@@ -5,5 +5,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[dependencies] -parking_lot = "0.12"
\ No newline at end of file +[dependencies]
\ No newline at end of file @@ -1,13 +1,12 @@ use std::any::type_name; use std::fmt::{self, Debug}; -use parking_lot::Mutex; - mod lock; +pub use lock::Lock; + thread_local! { - // safety: this is the only place where a ThreadLock is created - pub static KEY: Mutex<Option<ThreadKey>> = Mutex::new(Some(unsafe { ThreadKey::new() })); + pub static KEY: Lock = Lock::new(); } /// The key for the current thread. @@ -26,23 +25,13 @@ impl Debug for ThreadKey { impl Drop for ThreadKey { fn drop(&mut self) { - KEY.with(|thread_lock| { - let mut key = thread_lock.lock(); - - // safety: this ThreadKey is about to be destroyed, so there'll - // still only be one ThreadKey - *key = Some(unsafe { Self::new() }); - }) + KEY.with(|lock| lock.unlock()); } } impl ThreadKey { - /// Create a new `ThreadKey`. - /// - /// # Safety - /// - /// There should only be one `ThreadKey` per thread. - unsafe fn new() -> Self { + /// Create a new `ThreadKey`. There should only be one `ThreadKey` per thread. + fn new() -> Self { Self { _priv: std::ptr::null(), } @@ -54,7 +43,7 @@ impl ThreadKey { /// `ThreadKey`. However, future calls to this function will return /// [`None`], unless the key is dropped or unlocked first. pub fn lock() -> Option<Self> { - KEY.with(|thread_lock| thread_lock.lock().take()) + KEY.with(|lock| lock.try_lock().then_some(Self::new())) } /// Unlocks the `ThreadKey`. |
