Skip to content

Atomic operations for only free-threaded builds #115041

Closed
@mpage

Description

Feature or enhancement

Proposal:

Based on discussion in #113830 and elsewhere, there are situations in which we need to use an atomic operation in free-threaded builds, but do not need to use one in the default build, and do not want to introduce the potential performance overhead of an atomic operation in the default build. Since we anticipate that these situations will be common, we'd like to introduce wrappers around the functions found in cpython/atomic.h that perform the operation atomically in free-threaded builds and use the non-atomic equivalent in default builds.

To get discussion started, I propose the following:

  1. We add a header that lives alongside cpython/pyatomic.h (pyatomic_free_threaded_wrappers.h?) that provides definitions of the wrappers.
  2. Wrappers look like:
static inline _Py_ssize_t
_Py_ft_only_atomic_load_ssize(Py_ssize_t *obj)
{
    #ifdef Py_GIL_DISABLED
        return *obj;
    #else
        return _Py_atomic_load_ssize(obj);
    #endif
}

Naming is hard. I'm not particularly in love with _ft_only_ and would love a better alternative to communicate that the operation is atomic in free-threaded builds and non-atomic in default builds.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions