Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Thread-safe one-time initialization #111956

Closed
Assignees
colesbury
Labels
@colesbury

Description

@colesbury

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'sstd::call_once1. The proposed internal-only API follows C++11'sstd::call_once, but adapted for C (i.e., error returns and function pointers):

typedefstruct {uint8_tv;}_PyOnceFlag;typedefint_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, thePython-ast.c relies on the GIL and aninitialized variable to ensure that it is only initialized once:

staticint
init_types(structast_state*state)
{
// init_types() must not be called after _PyAST_Fini()
// has been called
assert(state->initialized >=0);
if (state->initialized) {
return1;
}

Linked PRs

Footnotes

  1. Also,pthread_once and C11'scall_once.std::call_once supports error returns, which is important for CPython's use cases.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp