about summary refs log tree commit diff
path: root/library/std/src/sys/thread_local/native
diff options
context:
space:
mode:
authorjoboet <jonasboettiger@icloud.com>2024-07-18 13:59:48 +0200
committerjoboet <jonasboettiger@icloud.com>2024-10-02 18:04:21 +0200
commitd868fdce6b9ddef6abcc8de86b3ba8459def36a2 (patch)
treeb03a6ba5ff19869e0ebdeefb996e4387a5bb13b9 /library/std/src/sys/thread_local/native
parent07f08ffb2dbc864d2127abedf7a5917b965c0a4b (diff)
downloadrust-d868fdce6b9ddef6abcc8de86b3ba8459def36a2.tar.gz
rust-d868fdce6b9ddef6abcc8de86b3ba8459def36a2.zip
std: make `thread::current` available in all `thread_local!` destructors
Diffstat (limited to 'library/std/src/sys/thread_local/native')
-rw-r--r--library/std/src/sys/thread_local/native/mod.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/library/std/src/sys/thread_local/native/mod.rs b/library/std/src/sys/thread_local/native/mod.rs
index 1cc45fe892d..f498dee0899 100644
--- a/library/std/src/sys/thread_local/native/mod.rs
+++ b/library/std/src/sys/thread_local/native/mod.rs
@@ -29,6 +29,9 @@
 //! eliminates the `Destroyed` state for these values, which can allow more niche
 //! optimizations to occur for the `State` enum. For `Drop` types, `()` is used.
 
+use crate::cell::Cell;
+use crate::ptr;
+
 mod eager;
 mod lazy;
 
@@ -107,3 +110,31 @@ pub macro thread_local_inner {
             $crate::thread::local_impl::thread_local_inner!(@key $t, $($init)*);
     },
 }
+
+#[rustc_macro_transparency = "semitransparent"]
+pub(crate) macro local_pointer {
+    () => {},
+    ($vis:vis static $name:ident; $($rest:tt)*) => {
+        #[thread_local]
+        $vis static $name: $crate::sys::thread_local::LocalPointer = $crate::sys::thread_local::LocalPointer::__new();
+        $crate::sys::thread_local::local_pointer! { $($rest)* }
+    },
+}
+
+pub(crate) struct LocalPointer {
+    p: Cell<*mut ()>,
+}
+
+impl LocalPointer {
+    pub const fn __new() -> LocalPointer {
+        LocalPointer { p: Cell::new(ptr::null_mut()) }
+    }
+
+    pub fn get(&self) -> *mut () {
+        self.p.get()
+    }
+
+    pub fn set(&self, p: *mut ()) {
+        self.p.set(p)
+    }
+}