Crate thread_local_macro
source ·Expand description
Provides a thread_local!()
macro, a helper to instantiate lazily-initialized
thread-local storage (TLS) variables.
The primary difference between using this crate’s thread_local!()
macro
and directly using the #[thread_local]
attribute is that static
items
tagged with the #[thread_local]
attribute will never be dropped,
just like all other static
s.
However, static items defined in a thread_local!()
macro block will be
destructed (e.g., dropped, destroyed) when that task exits.
Rust std-based implementation notes
The code in this crate is adapted from this version of the thread_local!()
macro
from the Rust standard library.
The main design has been left unchanged, but we have removed most of the configuration blocks
for complex platform-specific or OS-specific behavior.
Because Theseus supports the #[thread_local]
attribute, we can directly use the
TLS “fast path”, which the Rust standard library refers to as the “FastLocalInnerKey”.
Unsafety
We could probably could remove most of the unsafe code from this implementation, because we don’t have to account for the various raw platform-specific interfaces or using raw libc types like the original Rust std implementation does. However, I have chosen to leave the code as close as possible to the original Rust std implementation in order to make updates as easy as possible, for if and when the Rust std version changes and we wish to track/merge in those changes.
Macros
- Declare a new thread local storage key of type
LocalKey
.
Structs
- An error returned by
LocalKey::try_with
. - A thread-local storage key which owns its contents.