summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2022-10-26 22:29:16 -0400
committerBotahamec <botahamec@outlook.com>2022-10-26 22:29:16 -0400
commit7f73e25dfb47f860b9bbd0ad594b9a6695b8c597 (patch)
treeb7c27980e7c6adda7aec2035fa6ba584e6e56e12 /src/lib.rs
parentfc7dcf7652674d6f52653582306b9a1e860ea5ba (diff)
Created ThreadKey
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs36
1 files changed, 26 insertions, 10 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 7d12d9a..3e18d9f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,14 +1,30 @@
-pub fn add(left: usize, right: usize) -> usize {
- left + right
+use parking_lot::Mutex;
+
+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() }));
+}
+
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct ThreadKey {
+ _priv: *const (), // this isn't Send or Sync
}
-#[cfg(test)]
-mod tests {
- use super::*;
+impl ThreadKey {
+ unsafe fn new() -> Self {
+ Self {
+ _priv: std::ptr::null(),
+ }
+ }
+
+ pub fn lock() -> Option<Self> {
+ KEY.with(|thread_lock| thread_lock.lock().take())
+ }
- #[test]
- fn it_works() {
- let result = add(2, 2);
- assert_eq!(result, 4);
- }
+ pub fn unlock(lock: ThreadKey) {
+ KEY.with(|thread_lock| {
+ let mut thread_lock = thread_lock.lock();
+ *thread_lock = Some(lock);
+ })
+ }
}