@@ -963,16 +963,15 @@ a file, so that other Python threads can run in the meantime.
963963
964964The Python interpreter keeps some thread-specific bookkeeping information
965965inside 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
967- be retrieved using :c:func:`PyThreadState_Get`.
966+ There's also one :term:` thread-local variable <active thread state>` pointing to the
967+ current :c:type:`PyThreadState`: it can be retrieved using :c:func:`PyThreadState_Get`.
968968
969969A thread can only have one :term:`attached thread state` at a time. An attached
970970thread 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,
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 thread state attached to the current thread to call most of
975- the C API.
972+ :term:`attaching <attached thread state>` a thread state will block until the :term:`GIL`
973+ can be acquired. However, even on builds with the :term:`GIL` disabled, it is still required
974+ to have an attached thread state to call most of the C API.
976975
977976In general, there will always be an :term:`attached thread state` when using Python's C API.
978977Only in some specific cases (such as in a :c:macro:`Py_BEGIN_ALLOW_THREADS` block) will the
@@ -1132,9 +1131,8 @@ thread* that initiated finalization (typically the main thread) is allowed to
11321131acquire the :term:`GIL`.
11331132
11341133If any thread, other than the finalization thread, attempts to attach a :term:`thread state`
1135- during finalization, either explicitly via a :term:`thread state` function, or
1136- implicitly when the interpreter attempts yields the :term:`GIL` by detaching the
1137- :term:`thread state <attached thread state>`, the thread enters **a permanently blocked state**
1134+ during finalization, either explicitly or
1135+ implicitly, the thread enters **a permanently blocked state**
11381136where it remains until the program exits. In most cases this is harmless, but this can result
11391137in deadlock if a later stage of finalization attempts to acquire a lock owned by the
11401138blocked thread, or otherwise waits on the blocked thread.
@@ -1212,9 +1210,9 @@ code, or when embedding the Python interpreter:
12121210
12131211.. c:function:: void PyEval_RestoreThread(PyThreadState *tstate)
12141212
1215- Set the current :term:`thread state` to *tstate*, which must not be ``NULL``.
1216- The passed :term:`thread state` **should not** be attached, otherwise deadlock
1217- ensues.
1213+ Set the :term:`active thread state` to *tstate*, which must not be ``NULL``.
1214+ The passed :term:`thread state` **should not** be :term:` attached <attached thread state>`,
1215+ otherwise deadlock ensues.
12181216
12191217 .. note::
12201218 Calling this function from a thread when the runtime is finalizing will
@@ -1228,8 +1226,8 @@ code, or when embedding the Python interpreter:
12281226
12291227.. c:function:: PyThreadState* PyThreadState_Get()
12301228
1231- Return the :term:`active thread state`. If there is no :term:`active thread state` (such
1232- as when inside of :c:macro:`Py_BEGIN_ALLOW_THREADS` block), then this issues a fatal
1229+ Return the :term:`active thread state`. If the :term:`active thread state` is ``NULL``
1230+ (such as when inside of :c:macro:`Py_BEGIN_ALLOW_THREADS` block), then this issues a fatal
12331231 error (so that the caller needn't check for ``NULL``).
12341232
12351233 See also :c:func:`PyThreadState_GetUnchecked`.
@@ -1248,12 +1246,14 @@ code, or when embedding the Python interpreter:
12481246
12491247.. c:function:: PyThreadState* PyThreadState_Swap(PyThreadState *tstate)
12501248
1251- Swap the current :term:`thread state` with *tstate*.
1249+ Set the :term:`active thread state` to *tstate*, and return
1250+ the old value.
12521251
1253- If there is an attached :term:`thread state` for the current
1252+ If there is an :term:`attached thread state` for the current
12541253 thread, it will be detached. Upon returning from this function,
1255- *tstate* will become attached instead if it's not ``NULL``. If it
1256- is ``NULL``, then no :term:`thread state` will be attached upon returning.
1254+ *tstate* will become :term:`attached <attached thread state>` instead
1255+ if it's not ``NULL``. If it is ``NULL``, then the :term:`active thread state`
1256+ will be ``NULL``.
12571257
12581258
12591259The following functions use thread-local storage, and are not compatible
@@ -1271,7 +1271,7 @@ with sub-interpreters:
12711271 :c:macro:`Py_BEGIN_ALLOW_THREADS` and :c:macro:`Py_END_ALLOW_THREADS` macros is
12721272 acceptable.
12731273
1274- The return value is an opaque " handle" to the :term:`thread state` when
1274+ The return value is an opaque " handle" to the :term:`attached thread state` when
12751275 :c:func:`PyGILState_Ensure` was called, and must be passed to
12761276 :c:func:`PyGILState_Release` to ensure Python is left in the same state. Even
12771277 though recursive calls are allowed, these handles *cannot* be shared - each
@@ -1304,7 +1304,7 @@ with sub-interpreters:
13041304
13051305.. c:function:: PyThreadState* PyGILState_GetThisThreadState()
13061306
1307- Get the current :term:`thread state` for this thread. May return ``NULL`` if no
1307+ Get the :term:`active thread state` for this thread. May return ``NULL`` if no
13081308 GILState API has been used on the current thread. Note that the main thread
13091309 always has such a thread-state, even if no auto-thread-state call has been
13101310 made on the main thread. This is mainly a helper/diagnostic function.
@@ -1364,42 +1364,42 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
13641364
13651365.. versionchanged:: 3.7
13661366 :c:func:`Py_Initialize()` now initializes the :term:`GIL`
1367- and actives a :term:`thread state`.
1367+ and sets an :term:`attached thread state`.
13681368
13691369
13701370.. c:function:: PyInterpreterState* PyInterpreterState_New()
13711371
1372- Create a new interpreter state object. A :term:`thread state` need not
1373- be attached, but may be held if it is necessary to serialize calls to this
1372+ Create a new interpreter state object. An :term:`attached thread state` is not needed,
1373+ but may optionally exist if it is necessary to serialize calls to this
13741374 function.
13751375
13761376 .. audit-event:: cpython.PyInterpreterState_New " " c.PyInterpreterState_New
13771377
13781378
13791379.. c:function:: void PyInterpreterState_Clear(PyInterpreterState *interp)
13801380
1381- Reset all information in an interpreter state object. A :term:`thread state`
1382- for the interpreter must be attached .
1381+ Reset all information in an interpreter state object. There must be
1382+ an :term:`attached thread state` for the the interpreter .
13831383
13841384 .. audit-event:: cpython.PyInterpreterState_Clear " " c.PyInterpreterState_Clear
13851385
13861386
13871387.. c:function:: void PyInterpreterState_Delete(PyInterpreterState *interp)
13881388
1389- Destroy an interpreter state object. A :term:`thread state` for the interpreter
1390- shouldn't be attached. The interpreter state must have been reset with a previous call to
1391- :c:func:`PyInterpreterState_Clear`.
1389+ Destroy an interpreter state object. There **should not** be an
1390+ :term:` attached thread state` for the target interpreter. The interpreter
1391+ state must have been reset with a previous call to :c:func:`PyInterpreterState_Clear`.
13921392
13931393
13941394.. c:function:: PyThreadState* PyThreadState_New(PyInterpreterState *interp)
13951395
13961396 Create a new thread state object belonging to the given interpreter object.
1397- A :term:`thread state` can be optionally attached .
1397+ An :term:`attached thread state` is not needed .
13981398
13991399.. c:function:: void PyThreadState_Clear(PyThreadState *tstate)
14001400
1401- Reset all information in a :term:`thread state` object. The
1402- :term:`thread state` must be active for the current thread.
1401+ Reset all information in a :term:`thread state` object. *tstate*
1402+ must be :term:`attached <attached thread state>`
14031403
14041404 .. versionchanged:: 3.9
14051405 This function now calls the :c:member:`PyThreadState.on_delete` callback.
@@ -1411,16 +1411,17 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
14111411
14121412.. c:function:: void PyThreadState_Delete(PyThreadState *tstate)
14131413
1414- Destroy a :term:`thread state`` object. The :term:`thread state` should not
1415- be active. *tstate* must have been reset with a previous call to
1414+ Destroy a :term:`thread state`` object. *tstate* should not
1415+ be :term:`attached <attached thread state>` to any thread.
1416+ *tstate* must have been reset with a previous call to
14161417 :c:func:`PyThreadState_Clear`.
14171418
14181419
14191420.. c:function:: void PyThreadState_DeleteCurrent(void)
14201421
1421- Destroy the :term:`active thread state` and detach it.
1422- The current :term:`thread state` must have been reset with a previous call
1423- to :c:func:`PyThreadState_Clear`.
1422+ Destroy the :term:`attached thread state` and set the :term:`active thread state`
1423+ to ``NULL``. The :term:`thread state <attached thread state> ` must have been reset
1424+ with a previous call to :c:func:`PyThreadState_Clear`.
14241425
14251426
14261427.. c:function:: PyFrameObject* PyThreadState_GetFrame(PyThreadState *tstate)
@@ -1744,11 +1745,11 @@ function. You can create and destroy them using the following functions:
17441745 must be held before calling this function and is still held when it
17451746 returns. Likewise a current thread state must be set on entry. On
17461747 success, the returned thread state will be set as current. If the
1747- sub-interpreter is created with its own :term:`thread state ` then the
1748- :term:`thread state` of the calling interpreter will be detached.
1748+ sub-interpreter is created with its own :term:`GIL ` then the
1749+ :term:`attached thread state` of the calling interpreter will be detached.
17491750 When the function returns, the new interpreter's :term:`thread state`
1750- will be activated for the current thread and the previously interpreter's
1751- :term:`thread state` will remain detached here .
1751+ will be :term:`attached <attached thread state>` to the current thread and
1752+ the previous interpreter's :term:`attached thread state` will remain detached.
17521753
17531754 .. versionadded:: 3.12
17541755
@@ -1836,7 +1837,8 @@ function. You can create and destroy them using the following functions:
18361837 the current thread state is ``NULL``. All thread states associated
18371838 with this interpreter are destroyed. The global interpreter lock
18381839 used by the target interpreter must be held before calling this
1839- function. No :term:`thread state` is active when it returns.
1840+ function. No :term:`thread state` is :term:`attached <attached thread state>`
1841+ when it returns.
18401842
18411843 :c:func:`Py_FinalizeEx` will destroy all sub-interpreters that
18421844 haven't been explicitly destroyed at that point.
@@ -1941,8 +1943,8 @@ pointer and a void pointer argument.
19411943 This function doesn't need a current thread state to run, and it doesn't
19421944 need the global interpreter lock.
19431945
1944- To call this function in a subinterpreter, the caller must have an active
1945- :term:`thread state`. Otherwise, the function *func* can be scheduled to
1946+ To call this function in a subinterpreter, the caller must have an
1947+ :term:`attached thread state`. Otherwise, the function *func* can be scheduled to
19461948 be called from the wrong interpreter.
19471949
19481950 .. warning::
@@ -2162,7 +2164,7 @@ Reference tracing
21622164 any existing exception or set an exception. A :term:`thread state` will be active
21632165 every time the tracer function is called.
21642166
2165- The :term:`thread state` must be active when calling this function.
2167+ There must be an :term:`attached thread state` when calling this function.
21662168
21672169.. versionadded:: 3.13
21682170
@@ -2173,7 +2175,7 @@ Reference tracing
21732175 If no tracer was registered this function will return NULL and will set the
21742176 **data** pointer to NULL.
21752177
2176- A :term:`thread state` must be active when calling this function.
2178+ There must be an :term:`attached thread state` when calling this function.
21772179
21782180.. versionadded:: 3.13
21792181
@@ -2230,8 +2232,8 @@ CPython C level APIs are similar to those offered by pthreads and Windows:
22302232use a thread key and functions to associate a :c:expr:`void*` value per
22312233thread.
22322234
2233- A :term:`thread state` does *not* need to be active when calling these functions; they supply
2234- their own locking.
2235+ A :term:`thread state` does *not* need to be :term:`attached <attached thread state>`
2236+ when calling these functions; they suppl their own locking.
22352237
22362238Note that :file:`Python.h` does not include the declaration of the TLS APIs,
22372239you need to include :file:`pythread.h` to use thread-local storage.
@@ -2400,7 +2402,7 @@ The C-API provides a basic mutual exclusion lock.
24002402
24012403 Lock mutex *m*. If another thread has already locked it, the calling
24022404 thread will block until the mutex is unlocked. While blocked, the thread
2403- will temporarily detach the current :term:`thread state` if one is active .
2405+ will temporarily detach the :term:`attached thread state` if one exists .
24042406
24052407 .. versionadded:: 3.13
24062408
0 commit comments