Thread-local storage support¶
The Python interpreter provides low-level support for thread-local storage(TLS) which wraps the underlying native TLS implementation to support thePython-level thread-local storage API (threading.local). TheCPython C level APIs are similar to those offered by pthreads and Windows:use a thread key and functions to associate avoid* value perthread.
Athread state doesnot need to beattachedwhen calling these functions; they supply their own locking.
Note thatPython.h does not include the declaration of the TLS APIs,you need to includepythread.h to use thread-local storage.
Note
None of these API functions handle memory management on behalf of thevoid* values. You need to allocate and deallocate them yourself.If thevoid* values happen to bePyObject*, thesefunctions don’t do refcount operations on them either.
Thread-specific storage API¶
The thread-specific storage (TSS) API was introduced to supersede the use of the existing TLS API within theCPython interpreter. This API uses a new typePy_tss_t instead ofint to represent thread keys.
Added in version 3.7.
See also
“A New C-API for Thread-Local Storage in CPython” (PEP 539)
- typePy_tss_t¶
This data structure represents the state of a thread key, the definition ofwhich may depend on the underlying TLS implementation, and it has aninternal field representing the key’s initialization state. There are nopublic members in this structure.
WhenPy_LIMITED_API is not defined, static allocation ofthis type by
Py_tss_NEEDS_INITis allowed.
- Py_tss_NEEDS_INIT¶
This macro expands to the initializer for
Py_tss_tvariables.Note that this macro won’t be defined withPy_LIMITED_API.
Dynamic allocation¶
Dynamic allocation of thePy_tss_t, required in extension modulesbuilt withPy_LIMITED_API, where static allocation of this typeis not possible due to its implementation being opaque at build time.
- Py_tss_t*PyThread_tss_alloc()¶
- Part of theStable ABI since version 3.7.
Return a value which is the same state as a value initialized with
Py_tss_NEEDS_INIT, orNULLin the case of dynamic allocationfailure.
- voidPyThread_tss_free(Py_tss_t*key)¶
- Part of theStable ABI since version 3.7.
Free the givenkey allocated by
PyThread_tss_alloc(), afterfirst callingPyThread_tss_delete()to ensure any associatedthread locals have been unassigned. This is a no-op if thekeyargument isNULL.Note
A freed key becomes a dangling pointer. You should reset the key to
NULL.
Methods¶
The parameterkey of these functions must not beNULL. Moreover, thebehaviors ofPyThread_tss_set() andPyThread_tss_get() areundefined if the givenPy_tss_t has not been initialized byPyThread_tss_create().
- intPyThread_tss_is_created(Py_tss_t*key)¶
- Part of theStable ABI since version 3.7.
Return a non-zero value if the given
Py_tss_thas been initializedbyPyThread_tss_create().
- intPyThread_tss_create(Py_tss_t*key)¶
- Part of theStable ABI since version 3.7.
Return a zero value on successful initialization of a TSS key. The behavioris undefined if the value pointed to by thekey argument is notinitialized by
Py_tss_NEEDS_INIT. This function can be calledrepeatedly on the same key – calling it on an already initialized key is ano-op and immediately returns success.
- voidPyThread_tss_delete(Py_tss_t*key)¶
- Part of theStable ABI since version 3.7.
Destroy a TSS key to forget the values associated with the key across allthreads, and change the key’s initialization state to uninitialized. Adestroyed key is able to be initialized again by
PyThread_tss_create(). This function can be called repeatedly onthe same key – calling it on an already destroyed key is a no-op.
- intPyThread_tss_set(Py_tss_t*key,void*value)¶
- Part of theStable ABI since version 3.7.
Return a zero value to indicate successfully associating avoid*value with a TSS key in the current thread. Each thread has a distinctmapping of the key to avoid* value.
- void*PyThread_tss_get(Py_tss_t*key)¶
- Part of theStable ABI since version 3.7.
Return thevoid* value associated with a TSS key in the currentthread. This returns
NULLif no value is associated with the key in thecurrent thread.
Legacy APIs¶
Deprecated since version 3.7:This API is superseded by thethread-specific storage (TSS) API.
Note
This version of the API does not support platforms where the native TLS keyis defined in a way that cannot be safely cast toint. On such platforms,PyThread_create_key() will return immediately with a failure status,and the other TLS functions will all be no-ops on such platforms.
Due to the compatibility problem noted above, this version of the API should notbe used in new code.
- intPyThread_create_key()¶
- Part of theStable ABI.
- voidPyThread_delete_key(intkey)¶
- Part of theStable ABI.
- intPyThread_set_key_value(intkey,void*value)¶
- Part of theStable ABI.
- void*PyThread_get_key_value(intkey)¶
- Part of theStable ABI.
- voidPyThread_delete_key_value(intkey)¶
- Part of theStable ABI.
- voidPyThread_ReInitTLS()¶
- Part of theStable ABI.