diff options
author | Sam Gross <colesbury@gmail.com> | 2023-10-05 15:46:33 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-05 15:46:33 (GMT) |
commit | 6e97a9647ae028facb392d12fc24973503693bd6 (patch) | |
tree | cce40bd0d1b8317652405f04c54541d48b3419e9 /Python/ceval_gil.c | |
parent | 9eb2489266c4c1f115b8f72c0728db737cc8a815 (diff) | |
download | cpython-6e97a9647ae028facb392d12fc24973503693bd6.zip cpython-6e97a9647ae028facb392d12fc24973503693bd6.tar.gz cpython-6e97a9647ae028facb392d12fc24973503693bd6.tar.bz2 |
gh-109549: Add new states to PyThreadState to support PEP 703 (gh-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.
Diffstat (limited to 'Python/ceval_gil.c')
-rw-r--r-- | Python/ceval_gil.c | 47 |
1 files changed, 11 insertions, 36 deletions
diff --git a/Python/ceval_gil.c b/Python/ceval_gil.c index 6b4ec8e..f237e38 100644 --- a/Python/ceval_gil.c +++ b/Python/ceval_gil.c @@ -462,24 +462,22 @@ PyStatus _PyEval_InitGIL(PyThreadState *tstate, int own_gil) { assert(tstate->interp->ceval.gil == NULL); - int locked; if (!own_gil) { /* The interpreter will share the main interpreter's instead. */ PyInterpreterState *main_interp = _PyInterpreterState_Main(); assert(tstate->interp != main_interp); struct _gil_runtime_state *gil = main_interp->ceval.gil; init_shared_gil(tstate->interp, gil); - locked = current_thread_holds_gil(gil, tstate); + assert(!current_thread_holds_gil(gil, tstate)); } else { PyThread_init_thread(); init_own_gil(tstate->interp, &tstate->interp->_gil); - locked = 0; - } - if (!locked) { - take_gil(tstate); } + // Lock the GIL and mark the current thread as attached. + _PyThreadState_Attach(tstate); + return _PyStatus_OK(); } @@ -569,24 +567,14 @@ void PyEval_AcquireThread(PyThreadState *tstate) { _Py_EnsureTstateNotNULL(tstate); - - take_gil(tstate); - - if (_PyThreadState_SwapNoGIL(tstate) != NULL) { - Py_FatalError("non-NULL old thread state"); - } + _PyThreadState_Attach(tstate); } void PyEval_ReleaseThread(PyThreadState *tstate) { assert(_PyThreadState_CheckConsistency(tstate)); - - PyThreadState *new_tstate = _PyThreadState_SwapNoGIL(NULL); - if (new_tstate != tstate) { - Py_FatalError("wrong thread state"); - } - drop_gil(tstate->interp, tstate); + _PyThreadState_Detach(tstate); } #ifdef HAVE_FORK @@ -629,11 +617,8 @@ _PyEval_SignalAsyncExc(PyInterpreterState *interp) PyThreadState * PyEval_SaveThread(void) { - PyThreadState *tstate = _PyThreadState_SwapNoGIL(NULL); - _Py_EnsureTstateNotNULL(tstate); - - assert(gil_created(tstate->interp->ceval.gil)); - drop_gil(tstate->interp, tstate); + PyThreadState *tstate = _PyThreadState_GET(); + _PyThreadState_Detach(tstate); return tstate; } @@ -641,10 +626,7 @@ void PyEval_RestoreThread(PyThreadState *tstate) { _Py_EnsureTstateNotNULL(tstate); - - take_gil(tstate); - - _PyThreadState_SwapNoGIL(tstate); + _PyThreadState_Attach(tstate); } @@ -1015,18 +997,11 @@ _Py_HandlePending(PyThreadState *tstate) /* GIL drop request */ if (_Py_eval_breaker_bit_is_set(interp, _PY_GIL_DROP_REQUEST_BIT)) { /* Give another thread a chance */ - if (_PyThreadState_SwapNoGIL(NULL) != tstate) { - Py_FatalError("tstate mix-up"); - } - drop_gil(interp, tstate); + _PyThreadState_Detach(tstate); /* Other threads may run now */ - take_gil(tstate); - - if (_PyThreadState_SwapNoGIL(tstate) != NULL) { - Py_FatalError("orphan tstate"); - } + _PyThreadState_Attach(tstate); } /* Check for asynchronous exception. */ |