summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2022-10-26 22:51:31 -0400
committerBotahamec <botahamec@outlook.com>2022-10-26 22:51:31 -0400
commita3ff79eb391843b6a6eb144992bd6782bf080019 (patch)
treecc86b22f74b24fbf974eb1aecc6df3e107e3d3cf
parentc45969c24e72d9b73382049cd27c37ad64293ee4 (diff)
Docs for ThreadKey
-rw-r--r--src/lib.rs18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index be3d05b..614820f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -8,6 +8,10 @@ thread_local! {
pub static KEY: Mutex<Option<ThreadKey>> = Mutex::new(Some(unsafe { ThreadKey::new() }));
}
+/// The key for the current thread.
+///
+/// Only one of these exist per thread. To get the current thread's key, call
+/// [`ThreadKey::lock`].
pub struct ThreadKey {
_priv: *const (), // this isn't Send or Sync
}
@@ -19,16 +23,30 @@ impl Debug for ThreadKey {
}
impl ThreadKey {
+ /// Create a new `ThreadKey`.
+ ///
+ /// # Safety
+ ///
+ /// There should only be one `ThreadKey` per thread.
unsafe fn new() -> Self {
Self {
_priv: std::ptr::null(),
}
}
+ /// Get the current thread's `ThreadKey, if it's not already taken.
+ ///
+ /// The first time this is called, it will successfully return a
+ /// `ThreadKey`. However, future calls to this function will return
+ /// [`None`], unless the key is unlocked first.
pub fn lock() -> Option<Self> {
KEY.with(|thread_lock| thread_lock.lock().take())
}
+ /// Unlocks the `ThreadKey`.
+ ///
+ /// After this method is called, a call to [`ThreadKey::lock`] will return
+ /// this `ThreadKey`.
pub fn unlock(lock: ThreadKey) {
KEY.with(|thread_lock| {
let mut thread_lock = thread_lock.lock();