1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//! Allows access to the Rayon's thread local value
//! which is preserved when moving jobs across threads

use std::{cell::Cell, 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()))
}