alloc-tls
provides the alloc_thread_local!
macro, a near-drop-in replacement
for the standard library's thread_local!
macro that is safe for use in
implementing a global allocator.
Unlike thread_local!
, alloc_thread_local!
address the following issues
unique to implementing a global allocator:
- On platforms that support the
#[thread_local]
attribute, registering destructors for types that implementDrop
requires allocation. When a thread-local is initialized from a call to an allocation function (malloc
,free
, etc), this causes reentrancy.alloc_thread_local!
can detect this. - On Mac, it is not safe to access TLS while a dynamic library is being loaded.
When implementing a Mac dynamic library that provides a global allocator,
alloc_thread_local!
can detect whether the library has been loaded or not, and can avoid using TLS ifmalloc
or other similar calls are made from the loader itself during loading.
Known limitations:
alloc-tls
does not currently support platforms that do not support the#[thread_local]
attribute.