Non‐templated base holding the shared thread‐local bookkeeping state.

Synopsis

Declared in <folly/detail/ThreadLocalDetail.h>

struct StaticMetaBase;

Types

Name

Description

EntryID

Represents an ID of a thread local object. Initially set to the maximum uint. This representation allows us to avoid a branch in accessing TLS data (because if you test capacity > id if id = maxint then the test will always fail). It allows us to keep a constexpr constructor and avoid SIOF.

Type Aliases

Name

Description

SynchronizedThreadEntrySet

A ThreadEntrySet guarded by a synchronization wrapper.

Member Functions

Name

Description

StaticMetaBase [constructor]

Constructs the base with a thread‐entry accessor and access mode.

allocate

Allocates a slot id for the given entry.

allocateNewThreadEntry

Allocates a new thread entry for the calling thread.

destroy

Frees the slot id owned by the given entry.

elementsCapacity

Returns the elementsCapacity for the current thread ThreadEntry struct.

ensureThreadEntryIsInSet

Ensure the given ThreadEntry* is present in the tracking set for the given id. Once added, we do not remove it until the thread exits or the whole set is reaped when the TL id itself is destroyed.

getElement

Returns the element wrapper for the given entry on the current thread.

getThreadEntrySetsPtrSpan

Returns a span over the pointers to all active thread entry sets.

isThreadEntryInSet

Return true if given ThreadEntry is already present in the ThreadEntrySet for the given id.

isThreadEntryRemovedFromAllInMap

Check if ThreadEntry* is present in the map for all slots of ids.

popThreadEntrySetAndClearElementPtrs

Pop current ThreadEntrySet and for each ThreadEntry in it, clear its ElementWrapper for the 'id' and return them in the accumulated vector. This is called when an TL object is destroyed. The ElementWrapper returned are the responsibility of the calling thread to dispose of.

removeThreadEntryFromAllInMap

Remove a ThreadEntry* from the map of allId2ThreadEntrySets_ for all slot

reserve

Reserve enough space in the ThreadEntry::elements for the item

Static Member Functions

Name

Description

cleanupThreadEntriesAndList

Helper to do final free and delete of ThreadEntry and ThreadEntryList structures.

dying

Reports whether the process is in thread‐local teardown.

getThreadEntryList

Returns the calling thread's list of thread entries.

onThreadExit

Thread‐exit callback that disposes the thread's entries.

reallocate

Static helper method to reallocate the ThreadEntry::elements.

Data Members

Name

Description

accessAllThreadsLock_

Guards accessAllThreads() against concurrent per‐thread resets.

allId2ThreadEntrySets_

This is a map of all thread entries mapped to index i with active elements[i];

forkHandlerLock_

Serializes fork handling against all thread‐entry‐set locking.

freeIds_

The pool of freed slot ids available for reuse.

lock_

The lock_ is used to protect the freeIds_ list as well as synchronize reallocation of a thread's private array of ElementWrappers. The freeIds_ vector is manipulated on TL object id allocation and destroy. Resize of ElementWrappers array can only be done by its owner thread but other threads may try to be accessing the array at the same time if in the middle of destroying a TL object.

nextId_

The next slot id to allocate.

pthreadKey_

The pthread key backing per‐thread entry storage.

strict_

Whether strict access mode is enabled.

threadEntry_

Function returning the calling thread's entry.

totalElementWrappers_

Total size of ElementWrapper arrays across all threads. This is meant to surface the overhead of thread local tracking machinery since the array can be sparse when there are lots of thread local variables under the same tag.

Static Data Members

Name

Description

kUseThreadLocal

Whether native thread‐local storage is used on this platform.

Derived Classes

Name

Description

StaticMeta

Held in a singleton to track our global instances. We have one of these per "Tag", by default one for the whole system (Tag=void).

Created with MrDocs