Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-114940: Add a Per-Interpreter Lock For the List of Thread States#127037
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -773,7 +773,6 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate) | ||
| { | ||
| assert(interp != NULL); | ||
| assert(tstate != NULL); | ||
| _PyRuntimeState *runtime = interp->runtime; | ||
| /* XXX Conditions we need to enforce: | ||
| @@ -790,17 +789,17 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate) | ||
| } | ||
| // Clear the current/main thread state last. | ||
| HEAD_LOCK(runtime); | ||
| THREADS_HEAD_LOCK(interp); | ||
| PyThreadState *p = interp->threads.head; | ||
| HEAD_UNLOCK(runtime); | ||
| THREADS_HEAD_UNLOCK(interp); | ||
| while (p != NULL) { | ||
| // See https://github.com/python/cpython/issues/102126 | ||
| // Must be called without HEAD_LOCK held as it can deadlock | ||
| // if any finalizer tries to acquire that lock. | ||
| PyThreadState_Clear(p); | ||
| HEAD_LOCK(runtime); | ||
| THREADS_HEAD_LOCK(interp); | ||
| p = p->next; | ||
| HEAD_UNLOCK(runtime); | ||
| THREADS_HEAD_UNLOCK(interp); | ||
| } | ||
| if (tstate->interp == interp) { | ||
| /* We fix tstate->_status below when we for sure aren't using it | ||
| @@ -1539,7 +1538,7 @@ new_threadstate(PyInterpreterState *interp, int whence) | ||
| #endif | ||
| /* We serialize concurrent creation to protect global state. */ | ||
| HEAD_LOCK(interp->runtime); | ||
| THREADS_HEAD_LOCK(interp); | ||
| // Initialize the new thread state. | ||
| interp->threads.next_unique_id += 1; | ||
| @@ -1550,7 +1549,7 @@ new_threadstate(PyInterpreterState *interp, int whence) | ||
| PyThreadState *old_head = interp->threads.head; | ||
| add_threadstate(interp, (PyThreadState *)tstate, old_head); | ||
| HEAD_UNLOCK(interp->runtime); | ||
| THREADS_HEAD_UNLOCK(interp); | ||
| #ifdef Py_GIL_DISABLED | ||
| // Must be called with lock unlocked to avoid lock ordering deadlocks. | ||
| @@ -1741,7 +1740,7 @@ tstate_delete_common(PyThreadState *tstate, int release_gil) | ||
| } | ||
| _PyRuntimeState *runtime = interp->runtime; | ||
| HEAD_LOCK(runtime); | ||
| THREADS_HEAD_LOCK(interp); | ||
| if (tstate->prev) { | ||
| tstate->prev->next = tstate->next; | ||
| } | ||
| @@ -1757,9 +1756,11 @@ tstate_delete_common(PyThreadState *tstate, int release_gil) | ||
| if (interp->stoptheworld.requested) { | ||
| decrement_stoptheworld_countdown(&interp->stoptheworld); | ||
| } | ||
| HEAD_LOCK(runtime); | ||
| if (runtime->stoptheworld.requested) { | ||
| decrement_stoptheworld_countdown(&runtime->stoptheworld); | ||
| } | ||
| HEAD_UNLOCK(runtime); | ||
Comment on lines
+1759
to
+1763
MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is correct, but need to double-check. | ||
| } | ||
| #if defined(Py_REF_DEBUG) && defined(Py_GIL_DISABLED) | ||
| @@ -1770,7 +1771,7 @@ tstate_delete_common(PyThreadState *tstate, int release_gil) | ||
| assert(tstate_impl->refcounts.values == NULL); | ||
| #endif | ||
| HEAD_UNLOCK(runtime); | ||
| THREADS_HEAD_UNLOCK(interp); | ||
| // XXX Unbind in PyThreadState_Clear(), or earlier | ||
| // (and assert not-equal here)? | ||
| @@ -1851,13 +1852,15 @@ _PyThreadState_RemoveExcept(PyThreadState *tstate) | ||
| { | ||
| assert(tstate != NULL); | ||
| PyInterpreterState *interp = tstate->interp; | ||
| _PyRuntimeState *runtime = interp->runtime; | ||
| #ifdef Py_GIL_DISABLED | ||
| #ifndef NDEBUG | ||
| _PyRuntimeState *runtime = interp->runtime; | ||
| #endif | ||
| assert(runtime->stoptheworld.world_stopped); | ||
| #endif | ||
| HEAD_LOCK(runtime); | ||
| THREADS_HEAD_LOCK(interp); | ||
| /* Remove all thread states, except tstate, from the linked list of | ||
| thread states. */ | ||
| PyThreadState *list = interp->threads.head; | ||
| @@ -1872,7 +1875,7 @@ _PyThreadState_RemoveExcept(PyThreadState *tstate) | ||
| } | ||
| tstate->prev = tstate->next = NULL; | ||
| interp->threads.head = tstate; | ||
| HEAD_UNLOCK(runtime); | ||
| THREADS_HEAD_UNLOCK(interp); | ||
| return list; | ||
| } | ||
| @@ -2339,7 +2342,6 @@ _PyEval_StartTheWorld(PyInterpreterState *interp) | ||
| int | ||
| PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc) | ||
| { | ||
| _PyRuntimeState *runtime = &_PyRuntime; | ||
| PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
| /* Although the GIL is held, a few C API functions can be called | ||
| @@ -2348,7 +2350,7 @@ PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc) | ||
| * list of thread states we're traversing, so to prevent that we lock | ||
| * head_mutex for the duration. | ||
| */ | ||
| HEAD_LOCK(runtime); | ||
| THREADS_HEAD_LOCK(interp); | ||
| for (PyThreadState *tstate = interp->threads.head; tstate != NULL; tstate = tstate->next) { | ||
| if (tstate->thread_id != id) { | ||
| continue; | ||
| @@ -2363,13 +2365,13 @@ PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc) | ||
| */ | ||
| Py_XINCREF(exc); | ||
| PyObject *old_exc = _Py_atomic_exchange_ptr(&tstate->async_exc, exc); | ||
| HEAD_UNLOCK(runtime); | ||
| THREADS_HEAD_UNLOCK(interp); | ||
| Py_XDECREF(old_exc); | ||
| _Py_set_eval_breaker_bit(tstate, _PY_ASYNC_EXCEPTION_BIT); | ||
| return 1; | ||
| } | ||
| HEAD_UNLOCK(runtime); | ||
| THREADS_HEAD_UNLOCK(interp); | ||
| return 0; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I remember correctly, this was an issue that could not be changed because it would cause unexpected issues with CI (it was originally changed in my PR)