about summary refs log tree commit diff
path: root/compiler/rustc_thread_pool/src/tlv.rs
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_thread_pool/src/tlv.rs')
-rw-r--r--compiler/rustc_thread_pool/src/tlv.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/compiler/rustc_thread_pool/src/tlv.rs b/compiler/rustc_thread_pool/src/tlv.rs
new file mode 100644
index 00000000000..b5f63479e2f
--- /dev/null
+++ b/compiler/rustc_thread_pool/src/tlv.rs
@@ -0,0 +1,32 @@
+//! Allows access to the Rayon's thread local value
+//! which is preserved when moving jobs across threads
+
+use std::cell::Cell;
+use std::ptr;
+
+thread_local!(pub static TLV: Cell<*const ()> = const { Cell::new(ptr::null()) });
+
+#[derive(Copy, Clone)]
+pub(crate) struct Tlv(pub(crate) *const ());
+
+impl Tlv {
+    #[inline]
+    pub(crate) fn null() -> Self {
+        Self(ptr::null())
+    }
+}
+
+unsafe impl Sync for Tlv {}
+unsafe impl Send for Tlv {}
+
+/// Sets the current thread-local value
+#[inline]
+pub(crate) fn set(value: Tlv) {
+    TLV.with(|tlv| tlv.set(value.0));
+}
+
+/// Returns the current thread-local value
+#[inline]
+pub(crate) fn get() -> Tlv {
+    TLV.with(|tlv| Tlv(tlv.get()))
+}