]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-115041: Add wrappers that are atomic only in free-threaded builds (#115046)
authormpage <mpage@meta.com>
Wed, 14 Feb 2024 20:15:05 +0000 (12:15 -0800)
committerGitHub <noreply@github.com>
Wed, 14 Feb 2024 20:15:05 +0000 (15:15 -0500)
These are intended to be used in places where atomics are required in
free-threaded builds but not in the default build. We don't want to
introduce the potential performance overhead of an atomic operation in the
default build.

Include/internal/pycore_pyatomic_ft_wrappers.h [new file with mode: 0644]
Makefile.pre.in
PCbuild/pythoncore.vcxproj
PCbuild/pythoncore.vcxproj.filters

diff --git a/Include/internal/pycore_pyatomic_ft_wrappers.h b/Include/internal/pycore_pyatomic_ft_wrappers.h
new file mode 100644 (file)
index 0000000..d131397
--- /dev/null
@@ -0,0 +1,35 @@
+// This header file provides wrappers around the atomic operations found in
+// `pyatomic.h` that are only atomic in free-threaded builds.
+//
+// These are intended to be used in places where atomics are required in
+// free-threaded builds, but not in the default build, and we don't want to
+// introduce the potential performance overhead of an atomic operation in the
+// default build.
+//
+// All usages of these macros should be replaced with unconditionally atomic or
+// non-atomic versions, and this file should be removed, once the dust settles
+// on free threading.
+#ifndef Py_ATOMIC_FT_WRAPPERS_H
+#define Py_ATOMIC_FT_WRAPPERS_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifndef Py_BUILD_CORE
+#error "this header requires Py_BUILD_CORE define"
+#endif
+
+#ifdef Py_GIL_DISABLED
+#define FT_ATOMIC_LOAD_SSIZE_RELAXED(value) \
+    _Py_atomic_load_ssize_relaxed(&value)
+#define FT_ATOMIC_STORE_SSIZE_RELAXED(value, new_value) \
+    _Py_atomic_store_ssize_relaxed(&value, new_value)
+#else
+#define FT_ATOMIC_LOAD_SSIZE_RELAXED(value) value
+#define FT_ATOMIC_STORE_SSIZE_RELAXED(value, new_value) value = new_value
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* !Py_ATOMIC_FT_WRAPPERS_H */
index 96886adf309d81ffb1831ef5bb082230faea96aa..8252e6631c5af58fd428610463d1570ec09e1230 100644 (file)
@@ -1150,6 +1150,7 @@ PYTHON_HEADERS= \
                $(srcdir)/Include/internal/pycore_parser.h \
                $(srcdir)/Include/internal/pycore_pathconfig.h \
                $(srcdir)/Include/internal/pycore_pyarena.h \
+               $(srcdir)/Include/internal/pycore_pyatomic_ft_wrappers.h \
                $(srcdir)/Include/internal/pycore_pybuffer.h \
                $(srcdir)/Include/internal/pycore_pyerrors.h \
                $(srcdir)/Include/internal/pycore_pyhash.h \
index 4cc0ca4b9af8deb9c31270dfbbeeec686e45a736..abfafbb2a32f45c7b67a1cc6c597274273a6bfd7 100644 (file)
     <ClInclude Include="..\Include\internal\pycore_parking_lot.h" />
     <ClInclude Include="..\Include\internal\pycore_pathconfig.h" />
     <ClInclude Include="..\Include\internal\pycore_pyarena.h" />
+    <ClInclude Include="..\Include\internal\pycore_pyatomic_ft_wrappers.h" />
     <ClInclude Include="..\Include\internal\pycore_pyerrors.h" />
     <ClInclude Include="..\Include\internal\pycore_pyhash.h" />
     <ClInclude Include="..\Include\internal\pycore_pylifecycle.h" />
index ceaa21217267cf1dca3931d00de5dc67bb1435e1..d14f5a6d7fb0fca06c303c8e7d1308c7e2bfcff3 100644 (file)
     <ClInclude Include="..\Include\internal\pycore_pyarena.h">
       <Filter>Include\internal</Filter>
     </ClInclude>
+    <ClInclude Include="..\Include\internal\pycore_pyatomic_ft_wrappers.h">
+      <Filter>Include\internal</Filter>
+    </ClInclude>
     <ClInclude Include="..\Include\internal\pycore_pyerrors.h">
       <Filter>Include\internal</Filter>
     </ClInclude>