Skip to content

Commit d9e1170

Browse files
committed
Explicitly mark implementation details in the docs.
1 parent 728ee3a commit d9e1170

1 file changed

Lines changed: 44 additions & 27 deletions

File tree

Doc/c-api/threads.rst

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -262,25 +262,8 @@ Attaching/detaching thread states
262262
263263
It is OK to call this function if the thread already has an
264264
attached thread state, as long as there is a subsequent call to
265-
:c:func:`PyThreadState_Release` that matches this one.
266-
267-
Nested calls to this function will only sometimes create a new
268-
thread state.
269-
270-
First, this function checks if an attached thread state is present.
271-
If there is, this function then checks if the interpreter of that
272-
thread state matches the interpreter guarded by *guard*. If that is
273-
the case, this function simply marks the thread state as being used
274-
by a ``PyThreadState_Ensure`` call and returns.
275-
276-
If there is no attached thread state, then this function checks if any
277-
thread state has been used by the current OS thread. (This is
278-
returned by :c:func:`PyGILState_GetThisThreadState`.)
279-
If there was, then this function checks if that thread state's interpreter
280-
matches *guard*. If it does, it is re-attached and marked as used.
281-
282-
Otherwise, if both of the above cases fail, a new thread state is created
283-
for *guard*. It is then attached and marked as owned by ``PyThreadState_Ensure``.
265+
:c:func:`PyThreadState_Release` that matches this one (meaning that "nested"
266+
calls to this function are permitted).
284267
285268
The function's effect (if any) will be reversed by the matching call to
286269
:c:func:`PyThreadState_Release`.
@@ -296,6 +279,35 @@ Attaching/detaching thread states
296279
and must not be passed to any other C API functions
297280
(unless it matches a known valid :c:type:`!PyThreadState` pointer).
298281
282+
The conditions in which this function creates a new :term:`thread state` are
283+
considered unstable and implementation-dependent. If you need to control the
284+
exact lifetime of a thread state, consider using :c:func:`PyThreadState_New`.
285+
However, do not avoid this function solely on the basis that the lifetime
286+
of the thread state may be inconsistent across versions; changes to this
287+
function will be done with caution and in a backwards-compatible manner.
288+
In particular, the saving of thread-local variables and similar state will
289+
be retained across Python versions.
290+
291+
.. impl-detail::
292+
293+
The exact behavior of whether this function creates a new thread state is
294+
described below, but be aware that this may change in the future.
295+
296+
First, this function checks if an attached thread state is present.
297+
If there is, this function then checks if the interpreter of that
298+
thread state matches the interpreter guarded by *guard*. If that is
299+
the case, this function simply marks the thread state as being used
300+
by a ``PyThreadState_Ensure`` call and returns.
301+
302+
If there is no attached thread state, then this function checks if any
303+
thread state has been used by the current OS thread. (This is
304+
returned by :c:func:`PyGILState_GetThisThreadState`.)
305+
If there was, then this function checks if that thread state's interpreter
306+
matches *guard*. If it does, it is re-attached and marked as used.
307+
308+
Otherwise, if both of the above cases fail, a new thread state is created
309+
for *guard*. It is then attached and marked as owned by ``PyThreadState_Ensure``.
310+
299311
.. versionadded:: next
300312

301313

@@ -314,18 +326,23 @@ Attaching/detaching thread states
314326
Undo a :c:func:`PyThreadState_Ensure` call. This must be called exactly once
315327
for each successful call to ``PyThreadState_Ensure``.
316328
317-
This function will decrement an internal counter on the attached thread state. If
318-
this counter ever reaches below zero, this function emits a fatal error (via
319-
:c:func:`Py_FatalError`).
320-
321-
If the attached thread state is owned by ``PyThreadState_Ensure``, then the
322-
attached thread state will be deallocated and deleted upon the internal counter
323-
reaching zero. Otherwise, nothing happens when the counter reaches zero.
324-
325329
If *tstate* is non-``NULL``, it will be attached upon returning.
326330
If *tstate* indicates that no prior thread state was attached, there will be
327331
no attached thread state upon returning.
328332
333+
The exact behavior of whether this function deletes a thread state is
334+
considered unstable and implementation-dependent.
335+
336+
.. impl-detail::
337+
338+
Currently, this function will decrement an internal counter on the
339+
attached thread state. If this counter ever reaches below zero, this
340+
function emits a fatal error (via :c:func:`Py_FatalError`).
341+
342+
If the attached thread state is owned by ``PyThreadState_Ensure``, then the
343+
attached thread state will be deallocated and deleted upon the internal counter
344+
reaching zero. Otherwise, nothing happens when the counter reaches zero.
345+
329346
330347
.. _legacy-api:
331348
.. _gilstate:

0 commit comments

Comments
 (0)