rustc_thread_pool/tlv.rs
1//! Allows access to the Rayon's thread local value
2//! which is preserved when moving jobs across threads
3
4use std::cell::Cell;
5use std::ptr;
6
7thread_local!(pub static TLV: Cell<*const ()> = const { Cell::new(ptr::null()) });
8
9#[derive(Copy, Clone)]
10pub(crate) struct Tlv(pub(crate) *const ());
11
12impl Tlv {
13 #[inline]
14 pub(crate) fn null() -> Self {
15 Self(ptr::null())
16 }
17}
18
19unsafe impl Sync for Tlv {}
20unsafe impl Send for Tlv {}
21
22/// Sets the current thread-local value
23#[inline]
24pub(crate) fn set(value: Tlv) {
25 TLV.with(|tlv| tlv.set(value.0));
26}
27
28/// Returns the current thread-local value
29#[inline]
30pub(crate) fn get() -> Tlv {
31 TLV.with(|tlv| Tlv(tlv.get()))
32}