@@ -940,7 +940,8 @@ Thread State and the Global Interpreter Lock
940940 single: interpreter lock
941941 single: lock, interpreter
942942
943- The Python interpreter is not fully thread-safe. In order to support
943+ Unless on a :term:`free-threaded <free-threading>` build of :term:`CPython`,
944+ the Python interpreter is not fully thread-safe. In order to support
944945multi-threaded Python programs, there's a global lock, called the :term:`global
945946interpreter lock` or :term:`GIL`, that must be held by the current thread before
946947it can safely access Python objects. Without the lock, even the simplest
@@ -961,14 +962,28 @@ a file, so that other Python threads can run in the meantime.
961962 single: PyThreadState (C type)
962963
963964The Python interpreter keeps some thread-specific bookkeeping information
964- inside a data structure called :c:type:`PyThreadState`. There's also one
965- global variable pointing to the current :c:type:`PyThreadState`: it can
965+ inside a data structure called :c:type:`PyThreadState`, known as a :term:`thread state`.
966+ There's also one thread-local variable pointing to the current :c:type:`PyThreadState`: it can
966967be retrieved using :c:func:`PyThreadState_Get`.
967968
968- Releasing the GIL from extension code
969- -------------------------------------
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
971+ :term:`free-threaded <free-threading>` builds. On builds with the :term:`GIL` enabled,
972+ attaching a thread state will block until the :term:`GIL` can be acquired.
973+ However, 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
975+ the C API.
970976
971- Most extension code manipulating the :term:`GIL` has the following simple
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
981+ ``NULL``.
982+
983+ Detaching the thread state from extension code
984+ ----------------------------------------------
985+
986+ Most extension code manipulating the :term:`thread state` has the following simple
972987structure::
973988
974989 Save the thread state in a local variable.
@@ -1004,20 +1019,20 @@ The block above expands to the following code::
10041019 single: PyEval_SaveThread (C function)
10051020
10061021Here is how these functions work: the global interpreter lock is used to protect the pointer to the
1007- current thread state. When releasing the lock and saving the thread state,
1022+ current :term:` thread state` . When releasing the lock and saving the :term:` thread state` ,
10081023the current thread state pointer must be retrieved before the lock is released
1009- (since another thread could immediately acquire the lock and store its own thread
1010- state in the global variable). Conversely, when acquiring the lock and restoring
1011- the thread state, the lock must be acquired before storing the thread state
1024+ (since another thread could immediately acquire the lock and store its own :term:` thread
1025+ state` in the global variable). Conversely, when acquiring the lock and restoring
1026+ the :term:` thread state` , the lock must be acquired before storing the :term:` thread state``
10121027pointer.
10131028
10141029.. note::
1015- Calling system I/O functions is the most common use case for releasing
1016- the GIL , but it can also be useful before calling long-running computations
1017- which don't need access to Python objects, such as compression or
1018- cryptographic functions operating over memory buffers. For example, the
1019- standard :mod:`zlib` and :mod:`hashlib` modules release the GIL when
1020- compressing or hashing data.
1030+ Calling system I/O functions is the most common use case for detaching
1031+ the :term:`thread state` , but it can also be useful before calling
1032+ long-running computations which don't need access to Python objects, such
1033+ as compression or cryptographic functions operating over memory buffers.
1034+ For example, the standard :mod:`zlib` and :mod:`hashlib` modules detach the
1035+ :term:`thread state` when compressing or hashing data.
10211036
10221037
10231038.. _gilstate:
0 commit comments