pub struct ThreadLocalStorage { /* private fields */ }
Expand description
Provider for thread local storage. For non-test usage, there should generally be a single process-wide instance.
Implementations§
Source§impl ThreadLocalStorage
impl ThreadLocalStorage
Sourcepub const unsafe fn new(preferred_mode: Mode) -> Self
pub const unsafe fn new(preferred_mode: Mode) -> Self
§Safety
See Mode
for detailed safety requirements. No matter the preferred
mode, we fall back to Mode::Gettid
if native thread local storage
isn’t set up for a given thread, so that mode’s requirements must always
be met: each thread using this thread local storage must call
Self::unregister_current_thread
before exiting.
Sourcepub unsafe fn unregister_current_thread(&self)
pub unsafe fn unregister_current_thread(&self)
Release this thread’s thread local storage and exit the thread.
Should be called by every thread that accesses thread local storage. This is a no-op when using native thread-local storage, but is required for correctness otherwise, since thread IDs can be reused.
Panics if there are still any live references to this thread’s ShimTlsVar
s.
§Safety
The calling thread must not access this ThreadLocalStorage
again
before exiting.
Sourcepub fn current_key(&self) -> Option<ThreadLocalStorageKey>
pub fn current_key(&self) -> Option<ThreadLocalStorageKey>
An opaque key referencing this thread’s thread-local-storage.
None
if the current thread uses native TLS.
Sourcepub unsafe fn fork_from(&self, prev_key: Option<ThreadLocalStorageKey>)
pub unsafe fn fork_from(&self, prev_key: Option<ThreadLocalStorageKey>)
Reassigns storage from prev_id
to the current thread, and drops
storage for all other threads.
Meant to be called after forking a new process from a thread with ID
prev_id
.
§Safety
self
must not be shared with any other threads. Typically this is ensured
by calling this function after fork
(but not vfork
), and before any
additional threads are created from the new process.
Current thread must have the same native thread local storage as the parent; It is sufficient for parent to not have used CLONE_SETTLS when creating the current thread.