Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new states ATTACHED, DETACHED, GC to PyThreadState to support PEP 703 #109549

Closed
Tracked by #108219
colesbury opened this issue Sep 18, 2023 · 2 comments
Closed
Tracked by #108219
Assignees
Labels
3.13 bugs and security fixes type-feature A feature request or enhancement

Comments

@colesbury
Copy link
Contributor

colesbury commented Sep 18, 2023

Feature or enhancement

To support pausing threads for garbage collection, PEP 703 requires identifying which threads may be accessing Python object internals (like reference counts). Currently, this is effectively controlled by acquiring the GIL. The PEP proposes three thread-states (https://peps.python.org/pep-0703/#thread-states):

  • ATTACHED
  • DETACHED
  • GC

I propose adding these states and a new field to PyThreadState. Like all the other fields in PyThreadState (other than interp), the field is intended to be private.

#define _Py_THREAD_DETACHED 0
#define _Py_THREAD_ATTACHED 1
#define _Py_THREAD_GC 2

struct _ts {
   ...
   int state;
}

Unfortunately, we can't add this to the _status bitfield because operations need to be atomic, and atomic operations don't work well with C bitfields.

Default build (with GIL)

The attached and detached states will correspond precisely with acquiring and releasing the GIL. A thread will set it's state to _Py_THREAD_ATTACHED immediately after acquiring the GIL lock and set it to _Py_THREAD_DETACHED immediately before releasing the GIL lock.

The _Py_THREAD_GC state will not be used in the default build.

--disable-gil build

From https://peps.python.org/pep-0703/#thread-states:

When compiling without the GIL, functions that previously acquired the GIL instead transition the thread state to ATTACHED, and functions that previously released the GIL transition the thread state to DETACHED. Just as threads previously needed to acquire the GIL before accessing or modifying Python objects, they now must be in the ATTACHED state before accessing or modifying Python objects. Since the same public C-API functions “attach” the thread as previously acquired the GIL (e.g., PyEval_RestoreThread), the requirements for thread initialization in extensions remain the same. The substantial difference is that multiple threads can be in the attached state simultaneously, while previously only one thread could acquire the GIL at a time.

cc @ericsnowcurrently

Linked PRs

@colesbury colesbury added type-feature A feature request or enhancement 3.13 bugs and security fixes labels Sep 18, 2023
@colesbury colesbury changed the title Add new states ATTACHED, DETACHED, GC to PyThreadState` to support PEP 703 Add new states ATTACHED, DETACHED, GC to PyThreadState to support PEP 703 Sep 18, 2023
@colesbury colesbury self-assigned this Sep 18, 2023
@ericsnowcurrently
Copy link
Member

Unfortunately, we can't add this to the _status bitfield because operations need to be atomic, and atomic operations don't work well with C bitfields.

Yeah, having a separate field is fine. Ideally this would be part of _status but the constraint with atomics makes sense.

Presumably "with GIL" builds won't need the field to be atomic. Is that right?

@colesbury
Copy link
Contributor Author

Presumably "with GIL" builds won't need the field to be atomic. Is that right?

That's right

colesbury added a commit to colesbury/cpython that referenced this issue Sep 26, 2023
This adds a new field 'state' to PyThreadState that can take on one of
three values: _Py_THREAD_ATTACHED, _Py_THREAD_DETACHED, and
_Py_THREAD_GC. The "attached" and "detached" states correspond closely
to closely to acquiring and releasing the GIL. The "gc" state is
current unused, but will be used to implement stop-the-world GC for
`--disable-gil` builds in the near future.
ericsnowcurrently pushed a commit that referenced this issue Oct 5, 2023
)

This adds a new field 'state' to PyThreadState that can take on one of three values: _Py_THREAD_ATTACHED, _Py_THREAD_DETACHED, or _Py_THREAD_GC.  The "attached" and "detached" states correspond closely to acquiring and releasing the GIL.  The "gc" state is current unused, but will be used to implement stop-the-world GC for --disable-gil builds in the near future.
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
…ythongh-109915)

This adds a new field 'state' to PyThreadState that can take on one of three values: _Py_THREAD_ATTACHED, _Py_THREAD_DETACHED, or _Py_THREAD_GC.  The "attached" and "detached" states correspond closely to acquiring and releasing the GIL.  The "gc" state is current unused, but will be used to implement stop-the-world GC for --disable-gil builds in the near future.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants