Skip to content

Commit 9ed3a0b

Browse files
committed
Drop the phrase 'current thread state' and only use 'attached thread state' in its place
1 parent 3dee63f commit 9ed3a0b

File tree

3 files changed

+65
-61
lines changed

3 files changed

+65
-61
lines changed

Doc/c-api/init.rst

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -963,8 +963,8 @@ a file, so that other Python threads can run in the meantime.
963963
964964
The Python interpreter keeps some thread-specific bookkeeping information
965965
inside a data structure called :c:type:`PyThreadState`, known as a :term:`thread state`.
966-
There's also one :term:`thread-local variable <current thread state>` pointing to the
967-
current :c:type:`PyThreadState`: it can be retrieved using :c:func:`PyThreadState_Get`.
966+
Each OS thread has a thread-local pointer to a :c:type:`PyThreadState`; a thread state
967+
referenced by this pointer is considered to be :term:`attached <attached thread state>`.
968968
969969
A thread can only have one :term:`attached thread state` at a time. An attached
970970
thread state is typically analogous with holding the :term:`GIL`, except on
@@ -1204,14 +1204,13 @@ code, or when embedding the Python interpreter:
12041204
12051205
.. c:function:: PyThreadState* PyEval_SaveThread()
12061206
1207-
Detach the :term:`thread state <attached thread state>` and
1208-
return it. The :term:`current thread state` will be ``NULL`` upon
1209-
returning.
1207+
Detach the :term:`attached thread state` and return it.
1208+
The thread will have no :term:`thread state` upon returning.
12101209
12111210
12121211
.. c:function:: void PyEval_RestoreThread(PyThreadState *tstate)
12131212
1214-
Set the :term:`current thread state` to *tstate*, which must not be ``NULL``.
1213+
Set the :term:`attached thread state` to *tstate*.
12151214
The passed :term:`thread state` **should not** be :term:`attached <attached thread state>`,
12161215
otherwise deadlock ensues. *tstate* will be attached upon returning.
12171216
@@ -1227,13 +1226,13 @@ code, or when embedding the Python interpreter:
12271226
12281227
.. c:function:: PyThreadState* PyThreadState_Get()
12291228
1230-
Return the :term:`current thread state`. If the :term:`current thread state` is ``NULL``
1231-
(such as when inside of :c:macro:`Py_BEGIN_ALLOW_THREADS` block), then this issues a fatal
1232-
error (so that the caller needn't check for ``NULL``).
1229+
Return the :term:`attached thread state`. If the thread has no attached
1230+
thread state, (such as when inside of :c:macro:`Py_BEGIN_ALLOW_THREADS`
1231+
block), then this issues a fatal error (so that the caller needn't check
1232+
for ``NULL``).
12331233
12341234
See also :c:func:`PyThreadState_GetUnchecked`.
12351235
1236-
12371236
.. c:function:: PyThreadState* PyThreadState_GetUnchecked()
12381237
12391238
Similar to :c:func:`PyThreadState_Get`, but don't kill the process with a
@@ -1247,14 +1246,11 @@ code, or when embedding the Python interpreter:
12471246
12481247
.. c:function:: PyThreadState* PyThreadState_Swap(PyThreadState *tstate)
12491248
1250-
Set the :term:`current thread state` to *tstate* (which may be ``NULL``), and return
1251-
the old value.
1249+
Set the :term:`attached thread state` to *tstate*, and return the
1250+
:term:`thread state` that was attached prior to calling.
12521251
1253-
If there is an :term:`attached thread state` for the current
1254-
thread, it will be detached. Upon returning from this function,
1255-
*tstate* will become :term:`attached <attached thread state>` instead
1256-
(if it's not ``NULL``). If it is ``NULL``, then the :term:`current thread state`
1257-
will be ``NULL``.
1252+
This function is safe to call without an :term:`attached thread state`; it
1253+
will simply return ``NULL`` indicating that there was no prior thread state.
12581254
12591255
12601256
The following functions use thread-local storage, and are not compatible
@@ -1263,7 +1259,7 @@ with sub-interpreters:
12631259
.. c:function:: PyGILState_STATE PyGILState_Ensure()
12641260
12651261
Ensure that the current thread is ready to call the Python C API regardless
1266-
of the current state of Python, or of the :term:`current thread state`. This may
1262+
of the current state of Python, or of the :term:`attached thread state`. This may
12671263
be called as many times as desired by a thread as long as each call is
12681264
matched with a call to :c:func:`PyGILState_Release`. In general, other
12691265
thread-related APIs may be used between :c:func:`PyGILState_Ensure` and
@@ -1305,11 +1301,13 @@ with sub-interpreters:
13051301
13061302
.. c:function:: PyThreadState* PyGILState_GetThisThreadState()
13071303
1308-
Get the :term:`current thread state` for this thread. May return ``NULL`` if no
1304+
Get the :term:`attached thread state` for this thread. May return ``NULL`` if no
13091305
GILState API has been used on the current thread. Note that the main thread
13101306
always has such a thread-state, even if no auto-thread-state call has been
13111307
made on the main thread. This is mainly a helper/diagnostic function.
13121308
1309+
.. seealso: PyThreadState_Get
1310+
13131311
13141312
.. c:function:: int PyGILState_Check()
13151313
@@ -1420,10 +1418,11 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
14201418
14211419
.. c:function:: void PyThreadState_DeleteCurrent(void)
14221420
1423-
Destroy the :term:`attached thread state` and set the :term:`current thread state`
1424-
to ``NULL``. The :term:`thread state <attached thread state>` must have been reset
1425-
with a previous call to :c:func:`PyThreadState_Clear`.
1421+
Detach the :term:`attached thread state` (which must have been reset
1422+
with a previous call to :c:func:`PyThreadState_Clear`) and then destroy it.
14261423
1424+
No :term:`thread state` will be :term:`attached <attached thread state>` upon
1425+
returning.
14271426
14281427
.. c:function:: PyFrameObject* PyThreadState_GetFrame(PyThreadState *tstate)
14291428
@@ -1564,8 +1563,10 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
15641563
15651564
.. c:function:: void PyEval_AcquireThread(PyThreadState *tstate)
15661565
1567-
Set the :term:`current thread state` to *tstate*, which must not be ``NULL`` or
1568-
:term:`attached <attached thread state>`.
1566+
:term:`Attach <attached thread state>` *tstate* to the current thread,
1567+
which must not be ``NULL`` or already :term:`attached <attached thread state>`.
1568+
1569+
The calling thread must not already have an :term:`attached thread state`.
15691570
15701571
.. note::
15711572
Calling this function from a thread when the runtime is finalizing will
@@ -1588,7 +1589,7 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
15881589
15891590
.. c:function:: void PyEval_ReleaseThread(PyThreadState *tstate)
15901591
1591-
Reset the :term:`current thread state` to ``NULL``.
1592+
Detach the :term:`attached thread state`.
15921593
The *tstate* argument, which must not be ``NULL``, is only used to check
15931594
that it represents the :term:`attached thread state` --- if it isn't, a fatal error is
15941595
reported.
@@ -1830,7 +1831,7 @@ function. You can create and destroy them using the following functions:
18301831
18311832
Destroy the (sub-)interpreter represented by the given :term:`thread state`.
18321833
The given thread state must be :term:`attached <attached thread state>`.
1833-
When the call returns, the :term:`current thread state` is ``NULL``.
1834+
When the call returns, there will be no :term:`attached thread state`.
18341835
All thread states associated with this interpreter are destroyed.
18351836
18361837
:c:func:`Py_FinalizeEx` will destroy all sub-interpreters that
@@ -1933,12 +1934,9 @@ pointer and a void pointer argument.
19331934
notification recursively, but it can still be interrupted to switch
19341935
threads if the :term:`thread state <attached thread state>` is detached.
19351936
1936-
This function doesn't need a current thread state to run, and it doesn't
1937-
need an :term:`attached thread state`.
1938-
1939-
To call this function in a subinterpreter, the caller must have an
1940-
:term:`attached thread state`. Otherwise, the function *func* can be scheduled to
1941-
be called from the wrong interpreter.
1937+
This function doesn't need an :term:`attached thread state`. However, to call this
1938+
function in a subinterpreter, the caller must have an :term:`attached thread state`.
1939+
Otherwise, the function *func* can be scheduled to be called from the wrong interpreter.
19421940
19431941
.. warning::
19441942
This is a low-level function, only useful for very special cases.

Doc/c-api/reflection.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Reflection
5555
5656
.. c:function:: PyFrameObject* PyEval_GetFrame(void)
5757
58-
Return the current thread state's frame, which is ``NULL`` if no frame is
58+
Return the :term:`attached thread state`'s frame, which is ``NULL`` if no frame is
5959
currently executing.
6060
6161
See also :c:func:`PyThreadState_GetFrame`.

Doc/glossary.rst

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,24 @@ Glossary
134134

135135
attached thread state
136136

137-
A :term:`thread state` that is stored in the :term:`current thread state`.
138-
If no thread state is attached, then the :term:`current thread state` is ``NULL``.
139-
Attempting to call Python's C API without an attached thread state will result
140-
in a fatal error or an undefined behavior.
137+
A :term:`thread state` that is active for the current OS thread.
141138

142-
A thread state can be attached and detached explicitly by the user, or
143-
implicitly by the interpreter in between calls. For example, an attached
144-
thread state is detached upon entering a :c:macro:`Py_BEGIN_ALLOW_THREADS`
145-
block, and then re-attached when :c:macro:`Py_END_ALLOW_THREADS` is reached.
139+
When a :term:`thread state` is attached, the OS thread has
140+
access to the full Python C API and can safely invoke the
141+
bytecode interpreter.
146142

147-
On most builds of Python, having an attached thread state means that the
148-
caller holds the :term:`GIL` for the current interpreter.
143+
Unless a function explicitly notes otherwise, attempting to call
144+
the C API without an attached thread state will result in a fatal
145+
error or undefined behavior. A thread state can be attached and detached
146+
explicitly by the user through the C API, or implicitly by the runtime,
147+
including during blocking C calls and by the bytecode interpreter in between
148+
calls.
149+
150+
On most builds of Python, having an attached context means that the
151+
caller holds the :term:`GIL` for the current interpreter, so only
152+
one OS thread can have an attached thread state at a given moment. In
153+
:term:`free-threaded <free threading>` builds of Python, threads can concurrently
154+
hold an attached thread state, allowing for true parallelism.
149155

150156
attribute
151157
A value associated with an object which is usually referenced by name
@@ -348,19 +354,6 @@ Glossary
348354
tasks (see :mod:`asyncio`) associate each task with a context which
349355
becomes the current context whenever the task starts or resumes execution.
350356

351-
current thread state
352-
353-
A per-thread :c:data:`PyThreadState` pointer.
354-
355-
The pointer might be ``NULL``, in which case Python code must not
356-
get executed.
357-
358-
If the current thread state is non-``NULL``, then the :term:`thread state`
359-
that it points to is considered to be :term:`attached <attached thread state>`.
360-
361-
The pointer for the calling thread can be acquired via :c:func:`PyThreadState_Get` or
362-
:c:func:`PyThreadState_GetUnchecked`, if it might be ``NULL``.
363-
364357
decorator
365358
A function returning another function, usually applied as a function
366359
transformation using the ``@wrapper`` syntax. Common examples for
@@ -1314,11 +1307,24 @@ Glossary
13141307
:term:`bytes-like objects <bytes-like object>`.
13151308

13161309
thread state
1317-
In Python's C API, a thread state is a structure that holds
1318-
information about the current thread, typically in the :term:`current thread state`
1319-
pointer. A thread state can be attached or detached. An :term:`attached thread state`
1320-
is required to call most of the C API, unless a function explicitly documents
1321-
otherwise.
1310+
1311+
The information used by the :term:`CPython` runtime to run in an OS thread.
1312+
For example, this includes the current exception, if any, and the
1313+
state of the bytecode interpreter.
1314+
1315+
Each thread state is bound to a single OS thread, but threads may have
1316+
many thread states available. At most, one of them may be
1317+
:term:`attached <attached thread state>` at once.
1318+
1319+
An :term:`attached thread state` is required to call most
1320+
of Python's C API, unless a function explicitly documents otherwise.
1321+
The bytecode interpreter only runs under an attached thread state.
1322+
1323+
Each thread state belongs to a single interpreter, but each interpreter
1324+
may have many thread states, including multiple for the same OS thread.
1325+
Thread states from multiple interpreters may be bound to the same
1326+
thread, but only one can be :term:`attached <attached thread state>` in
1327+
that thread at any given moment.
13221328

13231329
See :ref:`Thread State and the Global Interpreter Lock <threads>` for more
13241330
information.

0 commit comments

Comments
 (0)