Struct ferroc::heap::ThreadLocal
source · pub struct ThreadLocal<'arena, B: BaseAlloc> { /* private fields */ }
Expand description
A collection for thread-local heaps.
This structure serves as a substitute for builtin dynamic TLS support with
__tls_get_addr
, which uses its slow dlmalloc
to allocate TLS variables.
Most of the direct utilities from this structure is unsafe
. For usages
that are totally safe, see the examples in ThreadData
.
Implementations§
source§impl<'arena, B: BaseAlloc> ThreadLocal<'arena, B>
impl<'arena, B: BaseAlloc> ThreadLocal<'arena, B>
sourcepub const fn new(arena: &'arena Arenas<B>) -> Self
pub const fn new(arena: &'arena Arenas<B>) -> Self
Creates a collection for thread-local heaps.
sourcepub const fn empty_heap(
&'static self
) -> Pin<&'static Heap<'static, 'static, B>>
pub const fn empty_heap( &'static self ) -> Pin<&'static Heap<'static, 'static, B>>
The default uninitialized heap, serving as a const initialization thread-local heap references.
This method is safe because uninitialized heaps don’t mutate their inner
data at all, thus practically Sync
.
source§impl<'arena, B: BaseAlloc> ThreadLocal<'arena, B>
impl<'arena, B: BaseAlloc> ThreadLocal<'arena, B>
sourcepub unsafe fn get(self: Pin<&Self>, id: NonZeroU64) -> Pin<&Heap<'arena, '_, B>>
pub unsafe fn get(self: Pin<&Self>, id: NonZeroU64) -> Pin<&Heap<'arena, '_, B>>
Acquire the current thread-local heap with this thread’s initialized id.
§Safety
id
must be unique with each live thread regarding only this structure
and may or may not be recycled.
sourcepub fn assign(self: Pin<&Self>) -> (Pin<&Heap<'arena, '_, B>>, NonZeroU64)
pub fn assign(self: Pin<&Self>) -> (Pin<&Heap<'arena, '_, B>>, NonZeroU64)
Acquires a new thread id and initialize its associated heap.
§Examples
Users can Store the information in its thread-local variable in 2 ways:
- Store the thread ID only, and get access to the heap every time using
self.get
; - Store both the thread ID and the pinned heap.
Both 2 ways needs to run its corresponding destructor manually. If RAII
is preferred, ThreadData
can be used instead.
§Panics
Panics if the acquisition failed.