mirror of
https://github.com/python/cpython.git
synced 2025-10-07 07:31:46 +00:00
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.
This commit is contained in:
parent
9eb2489266
commit
6e97a9647a
6 changed files with 141 additions and 92 deletions
|
@ -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. */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue