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