@@ -966,18 +966,17 @@ inside a data structure called :c:type:`PyThreadState`, known as a :term:`thread
966966There's also one thread-local variable pointing to the current :c:type:`PyThreadState`: it can
967967be retrieved using :c:func:`PyThreadState_Get`.
968968
969- A thread can only have one attached :term:`thread state` at a time. An attached
970- :term:` thread state` is typically analogous with holding the :term:`GIL`, except on
969+ A thread can only have one :term:`attached thread state` at a time. An attached
970+ thread state is typically analogous with holding the :term:`GIL`, except on
971971:term:`free-threaded <free threading>` builds. On builds with the :term:`GIL` enabled,
972972attaching a thread state will block until the :term:`GIL` can be acquired.
973973However, even on builds with the :term:`GIL` disabled, it is still required
974- to have a :term:` thread state` attached to the current thread to call most of
974+ to have a thread state attached to the current thread to call most of
975975the C API.
976976
977- In general, a :term:`thread state` will always be active for the current thread
978- when using Python's C API. Only in some specific cases (such as in a
979- :c:macro:`Py_BEGIN_ALLOW_THREADS` block) will the thread not have an active
980- thread state. If uncertain, check if :c:func:`PyThreadState_GetUnchecked` returns
977+ In general, there will always be an :term:`attached thread state` when using Python's C API.
978+ Only in some specific cases (such as in a :c:macro:`Py_BEGIN_ALLOW_THREADS` block) will the
979+ thread not have an attached thread state. If uncertain, check if :c:func:`PyThreadState_GetUnchecked` returns
981980``NULL``.
982981
983982Detaching the thread state from extension code
@@ -1018,14 +1017,15 @@ The block above expands to the following code::
10181017
10191018Here is how these functions work:
10201019
1021- The :term:`thread state` holds the :term:`GIL` for the entire interpreter. When detaching
1022- the :term:`thread state`, the :term:`GIL` is released, allowing other threads to attach
1023- their own :term:`thread state`, thus getting the :term:`GIL` and can start executing.
1024- The pointer to the now-detached :term:`thread state` is stored as a local variable.
1025- Upon reaching :c:macro:`Py_END_ALLOW_THREADS`, the :term:`thread state` that was
1026- previously attached is given to :c:func:`PyEval_RestoreThread`. This function will
1027- block until another thread that the :term:`GIL` can be re-acquired, thus allowing
1028- the :term:`thread state` to get re-attached and the C API can be called again.
1020+ The :term:`attached thread state` holds the :term:`GIL` for the entire interpreter. When detaching
1021+ the :term:`attached thread state`, the :term:`GIL` is released, allowing other threads to attach
1022+ a thread state to their own thread, thus getting the :term:`GIL` and can start executing.
1023+ The pointer to the prior :term:`attached thread state` is stored as a local variable.
1024+ Upon reaching :c:macro:`Py_END_ALLOW_THREADS`, the thread state that was
1025+ previously :term:`attached <attached thread state>` is passed to :c:func:`PyEval_RestoreThread`.
1026+ This function will block until another releases its :term:`thread state <attached thread state>`,
1027+ thus allowing the old :term:`thread state <attached thread state>` to get re-attached and the
1028+ C API can be called again.
10291029
10301030For :term:`free-threaded <free threading>` builds, the :term:`GIL` is normally
10311031out of the question, but detaching the thread state is still required for blocking I/O
@@ -1038,7 +1038,7 @@ to be released to attach their thread state, allowing true multi-core parallelis
10381038 long-running computations which don't need access to Python objects, such
10391039 as compression or cryptographic functions operating over memory buffers.
10401040 For example, the standard :mod:`zlib` and :mod:`hashlib` modules detach the
1041- :term:`thread state` when compressing or hashing data.
1041+ :term:`attached thread state` when compressing or hashing data.
10421042
10431043
10441044.. _gilstate:
@@ -1050,14 +1050,14 @@ When threads are created using the dedicated Python APIs (such as the
10501050:mod:`threading` module), a thread state is automatically associated to them
10511051and the code showed above is therefore correct. However, when threads are
10521052created from C (for example by a third-party library with its own thread
1053- management), they don't hold the :term:`GIL`, because they don't have a
1054- :term:`thread state`.
1053+ management), they don't hold the :term:`GIL`, because they don't have an
1054+ :term:`attached thread state`.
10551055
10561056If you need to call Python code from these threads (often this will be part
10571057of a callback API provided by the aforementioned third-party library),
10581058you must first register these threads with the interpreter by
1059- creating a :term:`thread state` before you can start using the Python/C
1060- API. When you are done, you should detach the :term:`thread state`, and
1059+ creating an :term:`attached thread state` before you can start using the Python/C
1060+ API. When you are done, you should detach the :term:`thread state <attached thread state> `, and
10611061finally free it.
10621062
10631063The :c:func:`PyGILState_Ensure` and :c:func:`PyGILState_Release` functions do
@@ -1134,8 +1134,8 @@ acquire the :term:`GIL`.
11341134If any thread, other than the finalization thread, attempts to attach a :term:`thread state`
11351135during finalization, either explicitly via a :term:`thread state` function, or
11361136implicitly when the interpreter attempts yields the :term:`GIL` by detaching the
1137- :term:`thread state`, the thread enters **a permanently blocked state** where it
1138- remains until the program exits. In most cases this is harmless, but this can result
1137+ :term:`thread state <attached thread state> `, the thread enters **a permanently blocked state**
1138+ where it remains until the program exits. In most cases this is harmless, but this can result
11391139in deadlock if a later stage of finalization attempts to acquire a lock owned by the
11401140blocked thread, or otherwise waits on the blocked thread.
11411141
0 commit comments