# 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_once`](https://en.cppreference.com/w/cpp/thread/call_once) [^1]. The proposed internal-only API follows C++11's `std::call_once`, but adapted for C (i.e., error returns and function pointers): ```c typedef struct { uint8_t v; } _PyOnceFlag; typedef int _Py_once_fn_t(void *arg); // Calls `fn` once using `flag`. The `arg` is passed to the call to `fn`. // // Returns 1 on success and 0 on failure. // // If `fn` returns 1 (success), then subsequent calls immediately return 1. // If `fn` returns 0 (failure), then subsequent calls will retry the call. int _PyOnceFlag_CallOnce(_PyOnceFlag *flag, _Py_once_fn_t *fn, void *arg); ``` As an example, the `Python-ast.c` relies on the GIL and an `initialized` variable to ensure that it is only initialized once: https://github.com/python/cpython/blob/d61313bdb1eee3e4bb111e0b248ac2dbb48be917/Python/Python-ast.c#L1126-L1135 [^1]: Also, [`pthread_once`](https://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_once.html) and C11's [`call_once`](https://en.cppreference.com/w/c/thread/call_once). `std::call_once` supports error returns, which is important for CPython's use cases. <!-- gh-linked-prs --> ### Linked PRs * gh-111960 <!-- /gh-linked-prs -->