@@ -11,30 +11,35 @@ a unique kind of Stable ABI with interpreters having the :term:`global interpret
1111For 3.14 and 3.13, continue compiling with the version-specific ABI. This document describes how
1212to adapt C API extensions to support free threading.
1313
14- Identifying the Free-Threaded Build in C
15- ========================================
14+ Identifying the Free-Threaded Limited API Build in C
15+ ====================================================
1616
17- The CPython C API exposes the `` Py_GIL_DISABLED `` macro: in the free-threaded
17+ The CPython C API exposes the :c:macro: ` !Py_LIMITED_API ` macro: in the free-threaded stable ABI
1818build it's defined to ``1 ``, and in the regular build it's not defined.
1919You can use it to enable code that only runs under the free-threaded build::
2020
21- #ifdef Py_GIL_DISABLED
22- /* code that only runs in the free-threaded build */
21+ #ifdef Py_TARGET_ABI3T
22+ /* code that only runs in the free-threaded stable ABI build */
2323 #endif
2424
25- .. note ::
25+ If you wish to build youe extension with both ``abi3 `` (Stable ABI with GIL) and ``abi3t `` (no-GIL stable ABI) tags,
26+ do one of the following:
27+
28+ - define both :c:macro: `!Py_LIMITED_API ` and :c:macro: `!Py_TARGET_ABI3T `, or
29+ - define only :c:macro: `!Py_LIMITED_API ` and:
2630
27- On Windows, this macro is not defined automatically, but must be specified
28- to the compiler when building. The :func: `sysconfig.get_config_var ` function
29- can be used to determine whether the current running interpreter had the
30- macro defined.
31+ - on Windows, define :c:macro: `!Py_GIL_DISABLED `;
32+ - on other systems, use the headers of free-threaded build of Python.
3133
32- ``PyObject `` opaqueness
34+ ``PyObject `` and `` PyVarObject `` opaqueness
3335=======================
3436
3537Accessing any member of ``PyObject `` directly is now prohibited, like the non-GIL
3638stable ABI. For instance, prefer ``Py_TYPE() `` and ``Py_SET_TYPE() `` over ``ob_type ``,
37- ``Py_REFCNT ``, ``Py_INCREF() `` and ``Py_DecRef() `` over ``ob_refcnt ``, etc.
39+ ``Py_REFCNT ``, ``Py_IncRef() `` and ``Py_DecRef() `` over ``ob_refcnt ``, etc.
40+
41+ Similarly, members of ``PyVarObject `` are not visible. If you need any object of such type
42+ to be passed as a ``PyObject `` parameter to any API function, cast it directly as ``PyObject ``.
3843
3944Module Initialization
4045=====================
@@ -43,9 +48,8 @@ Extension modules need to explicitly indicate that they support running with
4348the GIL disabled; otherwise importing the extension will raise a warning and
4449enable the GIL at runtime.
4550
46- There are two ways to indicate that an extension module supports running with
47- the GIL disabled depending on whether the extension uses multi-phase or
48- single-phase initialization.
51+ Multi-phase and single-phase initialization is supported to indicate that an extension module
52+ targeting the stable ABI supports running with the GIL disabled, though the former is preferred.
4953
5054Multi-Phase Initialization
5155..........................
@@ -56,48 +60,43 @@ Extensions that use :ref:`multi-phase initialization <multi-phase-initialization
5660:c:func: `PyModule_FromSlotsAndSpec `) should add a
5761:c:data: `Py_mod_gil ` slot in the module definition.
5862If your extension supports older versions of CPython,
59- you should guard the slot with a :c:data: `PY_VERSION_HEX ` check.
63+ you should guard the slot with a :c:data: `Py_GIL_DISABLED ` check.
6064
6165::
6266
6367 static struct PyModuleDef_Slot module_slots[] = {
6468 ...
65- #if PY_VERSION_HEX >= 0x030D0000
69+ #ifdef Py_GIL_DISABLED
6670 {Py_mod_gil, Py_MOD_GIL_NOT_USED},
6771 #endif
6872 {0, NULL}
6973 };
7074
75+ Additionally, using :c:macro: `PyABIInfo_VAR and :c:data:`Py_mod_abi ` is recommended so that an
76+ extension module loaded for an incompatible interpreter will trigger an exception, rather than
77+ fail with a crash.
7178
72- Single-Phase Initialization
73- ...........................
74-
75- Extensions that use legacy :ref: `single-phase initialization <single-phase-initialization >`
76- (that is, :c:func: `PyModule_Create `) should call :c:func: `PyUnstable_Module_SetGIL ` to
77- indicate that they support running with the GIL disabled. The function is
78- only defined in the free-threaded build, so you should guard the call with
79- ``#ifdef Py_GIL_DISABLED `` to avoid compilation errors in the regular build.
79+ .. code-block :: c
8080
81- ::
81+ #ifdef PY_VERSION_HEX >= 0x030F0000
82+ PyABIInfo_VAR(abi_info);
83+ #endif Py_GIL_DISABLED
8284
83- static struct PyModuleDef moduledef = {
84- PyModuleDef_HEAD_INIT,
85- ...
86- };
85+ static PyModuleDef_Slot mymodule_slots[] = {
86+ ...
87+ #ifdef PY_VERSION_HEX >= 0x030F0000
88+ {Py_mod_abi, &abi_info},
89+ #endif
90+ {0, NULL}
91+ };
8792
88- PyMODINIT_FUNC
89- PyInit_mymodule(void)
90- {
91- PyObject *m = PyModule_Create(&moduledef);
92- if (m == NULL) {
93- return NULL;
94- }
95- #ifdef Py_GIL_DISABLED
96- PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
97- #endif
98- return m;
99- }
93+ Single-Phase Initialization
94+ ...........................
10095
96+ Although members of ``PyModuleDef `` is still available for no-GIL Stable ABI and can be used
97+ for :ref: `single-phase initialization <single-phase-initialization >`
98+ (that is, :c:func: `PyModule_Create `), they are not exposed when targeting the regular Stable ABI.
99+ Prefer multi-phased initializtion when possible.
101100
102101General API Guidelines
103102======================
@@ -106,12 +105,6 @@ Most of the C API is thread-safe, but there are some exceptions.
106105
107106* **Struct Fields **: Accessing fields in Python C API objects or structs
108107 directly is not thread-safe if the field may be concurrently modified.
109- * **Macros **: Accessor macros like :c:macro: `PyList_GET_ITEM `,
110- :c:macro: `PyList_SET_ITEM `, and macros like
111- :c:macro: `PySequence_Fast_GET_SIZE ` that use the object returned by
112- :c:func: `PySequence_Fast ` do not perform any error checking or locking.
113- These macros are not thread-safe if the container object may be modified
114- concurrently.
115108* **Borrowed References **: C API functions that return
116109 :term: `borrowed references <borrowed reference> ` may not be thread-safe if
117110 the containing object is modified concurrently. See the section on
@@ -389,43 +382,31 @@ Important Considerations
389382 internal extension state, standard mutexes or other synchronization
390383 primitives might be more appropriate.
391384
392- .. _per-object-locks :
385+ Platform-specific considerations
386+ ................................
393387
394- Per-Object Locks (``ob_mutex ``)
395- ...............................
388+ On some platforms, Python will look for and load shared library files named
389+ with the ``abi3 `` or ``abi3t `` tag (for example, ``mymodule.abi3.so ``).
390+ :term: `Free-threaded <free-threaded build> ` interpreters only recognize the
391+ ``abi3t `` tag, while non-free-threaded ones will prefer ``abi3 `` but fall back
392+ to ``abi3t ``.
393+ Thus, extensions compatible with both ABIs should use the ``abi3t `` tag.
396394
397- In the free-threaded build, each Python object contains a :c:member: ` ~PyObject.ob_mutex `
398- field of type :c:type: ` PyMutex `. This mutex is ** reserved for use by the
399- critical section API ** ( :c:macro: `Py_BEGIN_CRITICAL_SECTION ` /
400- :c:macro: ` Py_END_CRITICAL_SECTION `) .
395+ Python does not necessarily check that extensions it loads
396+ have compatible ABI.
397+ Extension authors are encouraged to add a check using the :c:macro: `Py_mod_abi `
398+ slot or the :c:func: ` PyABIInfo_Check ` function .
401399
402- .. warning ::
403-
404- Do **not ** lock ``ob_mutex `` directly with ``PyMutex_Lock(&obj->ob_mutex) ``.
405- Mixing direct ``PyMutex_Lock `` calls with the critical section API on the
406- same mutex can cause deadlocks.
407-
408- Even if your own code never uses critical sections on a particular object type,
409- **CPython internals may use the critical section API on any Python object **.
410-
411- If your extension type needs its own lock, add a separate :c:type: `PyMutex `
412- field (or another synchronization primitive) to your object struct.
413- :c:type: `PyMutex ` is very lightweight, so there is negligible cost to having
414- an additional one.
415-
416- Limited C API and Stable ABI
417- ............................
400+ Limited C API Build Tools
401+ .........................
418402
419403If you use
420404`setuptools <https://setuptools.pypa.io/en/latest/setuptools.html >`_ to build
421405your extension, a future version of ``setuptools `` will allow ``py_limited_api=True ``
422406to be set to allow targeting limited API when building with the free-threaded build.
423407
424- Windows
425- .......
426-
427- Due to a limitation of the official Windows installer, you will need to
428- manually define ``Py_GIL_DISABLED=1 `` when building extensions from source.
408+ Other build tools will support this ABI as well:
409+ `<https://packaging.python.org/en/latest/guides/tool-recommendations/#build-backends-for-extension-modules> `
429410
430411.. seealso ::
431412
0 commit comments