Thread-safe one-time initialization #111956
Labels
3.13
new features, bugs and security fixes
topic-free-threading
type-feature
A feature request or enhancement
Feature or enhancement
Some CPython internals require initialization exactly once. Some of these one time initializations are not thread-safe without the GIL or have data races according to the C11 memory model.
We should add a lightweight, thread-safe one-time initialization API similar to C++11's
std::call_once1. The proposed internal-only API follows C++11'sstd::call_once, but adapted for C (i.e., error returns and function pointers):As an example, the
Python-ast.crelies on the GIL and aninitializedvariable to ensure that it is only initialized once:cpython/Python/Python-ast.c
Lines 1126 to 1135 in d61313b
Linked PRs
Footnotes
Also,
pthread_onceand C11'scall_once.std::call_oncesupports error returns, which is important for CPython's use cases. ↩The text was updated successfully, but these errors were encountered: