summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2022-10-27 17:38:50 -0400
committerBotahamec <botahamec@outlook.com>2022-10-27 17:38:50 -0400
commitd2c3a6c6688685d466b69df94d610948d8657c31 (patch)
tree646003e2c783ad3bb570e76989b0efba160c4bea /src
parentc2ec772dac7cf2a488af539613bf31ed15dfbaa5 (diff)
Remove the mutex
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs25
1 files changed, 7 insertions, 18 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 53fc59a..154ba8f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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`.