Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-126914: Store the Preallocated Thread State's Pointer in a PyInterpreterState Field#126989
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
Merged
ericsnowcurrently merged 9 commits into python:main from ericsnowcurrently:thread-state-freelistNov 19, 2024
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
9 commits Select commit Hold shift + click to select a range
7c6ca28 Move the location of a PyInterpreterState field.
ericsnowcurrently e0d363c Add a test.
ericsnowcurrently bbd4ae6 Always reset the initial tstate when re-used.
ericsnowcurrently e627718 Add a thread state freelist.
ericsnowcurrently 7cdc4cf Fix a typo.
ericsnowcurrently d8fc3c5 Add a NULL check.
ericsnowcurrently 243e444 Apply a testing decorator.
ericsnowcurrently 4f815c6 Retry if already running in the test.
ericsnowcurrently fcbe56d freelist -> preallocated
ericsnowcurrently File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -629,6 +629,8 @@ init_interpreter(PyInterpreterState *interp, | ||
| assert(next != NULL || (interp == runtime->interpreters.main)); | ||
| interp->next = next; | ||
| interp->threads.preallocated = &interp->_initial_thread; | ||
| // We would call _PyObject_InitState() at this point | ||
| // if interp->feature_flags were alredy set. | ||
| @@ -766,7 +768,6 @@ PyInterpreterState_New(void) | ||
| return interp; | ||
| } | ||
| static void | ||
| interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate) | ||
| { | ||
| @@ -910,6 +911,9 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate) | ||
| // XXX Once we have one allocator per interpreter (i.e. | ||
| // per-interpreter GC) we must ensure that all of the interpreter's | ||
| // objects have been cleaned up at the point. | ||
| // We could clear interp->threads.freelist here | ||
| // if it held more than just the initial thread state. | ||
| } | ||
| @@ -1386,22 +1390,45 @@ allocate_chunk(int size_in_bytes, _PyStackChunk* previous) | ||
| return res; | ||
| } | ||
| static void | ||
| reset_threadstate(_PyThreadStateImpl *tstate) | ||
| { | ||
| // Set to _PyThreadState_INIT directly? | ||
| memcpy(tstate, | ||
| &initial._main_interpreter._initial_thread, | ||
| sizeof(*tstate)); | ||
| } | ||
| static _PyThreadStateImpl * | ||
| alloc_threadstate(void) | ||
| alloc_threadstate(PyInterpreterState *interp) | ||
| { | ||
| return PyMem_RawCalloc(1, sizeof(_PyThreadStateImpl)); | ||
| _PyThreadStateImpl *tstate; | ||
| // Try the preallocated tstate first. | ||
| tstate = _Py_atomic_exchange_ptr(&interp->threads.preallocated, NULL); | ||
| // Fall back to the allocator. | ||
| if (tstate == NULL){ | ||
| tstate = PyMem_RawCalloc(1, sizeof(_PyThreadStateImpl)); | ||
ZeroIntensity marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| if (tstate == NULL){ | ||
| return NULL; | ||
| } | ||
| reset_threadstate(tstate); | ||
| } | ||
| return tstate; | ||
| } | ||
| static void | ||
| free_threadstate(_PyThreadStateImpl *tstate) | ||
| { | ||
| PyInterpreterState *interp = tstate->base.interp; | ||
| // The initial thread state of the interpreter is allocated | ||
| // as part of the interpreter state so should not be freed. | ||
| if (tstate == &tstate->base.interp->_initial_thread){ | ||
| // Restore to _PyThreadState_INIT. | ||
| memcpy(tstate, | ||
| &initial._main_interpreter._initial_thread, | ||
| sizeof(*tstate)); | ||
| if (tstate == &interp->_initial_thread){ | ||
| // Make it available again. | ||
| reset_threadstate(tstate); | ||
ZeroIntensity marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| assert(interp->threads.preallocated == NULL); | ||
| _Py_atomic_store_ptr(&interp->threads.preallocated, tstate); | ||
| } | ||
| else{ | ||
| PyMem_RawFree(tstate); | ||
| @@ -1492,66 +1519,38 @@ add_threadstate(PyInterpreterState *interp, PyThreadState *tstate, | ||
| static PyThreadState * | ||
| new_threadstate(PyInterpreterState *interp, int whence) | ||
| { | ||
| _PyThreadStateImpl *tstate; | ||
| _PyRuntimeState *runtime = interp->runtime; | ||
| // We don't need to allocate a thread state for the main interpreter | ||
| // (the common case), but doing it later for the other case revealed a | ||
| // reentrancy problem (deadlock). So for now we always allocate before | ||
| // taking the interpreters lock. See GH-96071. | ||
| _PyThreadStateImpl *new_tstate = alloc_threadstate(); | ||
| int used_newtstate; | ||
| if (new_tstate == NULL){ | ||
| // Allocate the thread state. | ||
| _PyThreadStateImpl *tstate = alloc_threadstate(interp); | ||
| if (tstate == NULL){ | ||
| return NULL; | ||
| } | ||
| #ifdef Py_GIL_DISABLED | ||
| Py_ssize_t qsbr_idx = _Py_qsbr_reserve(interp); | ||
| if (qsbr_idx < 0){ | ||
| PyMem_RawFree(new_tstate); | ||
| free_threadstate(tstate); | ||
| return NULL; | ||
| } | ||
| int32_t tlbc_idx = _Py_ReserveTLBCIndex(interp); | ||
| if (tlbc_idx < 0){ | ||
| PyMem_RawFree(new_tstate); | ||
| free_threadstate(tstate); | ||
| return NULL; | ||
| } | ||
| #endif | ||
| /* We serialize concurrent creation to protect global state. */ | ||
| HEAD_LOCK(runtime); | ||
| HEAD_LOCK(interp->runtime); | ||
| // Initialize the new thread state. | ||
| interp->threads.next_unique_id += 1; | ||
| uint64_t id = interp->threads.next_unique_id; | ||
| init_threadstate(tstate, interp, id, whence); | ||
| // Allocate the thread state and add it to the interpreter. | ||
| // Add the new thread state to the interpreter. | ||
| PyThreadState *old_head = interp->threads.head; | ||
| if (old_head == NULL){ | ||
| // It's the interpreter's initial thread state. | ||
| used_newtstate = 0; | ||
| tstate = &interp->_initial_thread; | ||
| } | ||
| // XXX Re-use interp->_initial_thread if not in use? | ||
| else{ | ||
| // Every valid interpreter must have at least one thread. | ||
| assert(id > 1); | ||
| assert(old_head->prev == NULL); | ||
| used_newtstate = 1; | ||
| tstate = new_tstate; | ||
| // Set to _PyThreadState_INIT. | ||
| memcpy(tstate, | ||
| &initial._main_interpreter._initial_thread, | ||
| sizeof(*tstate)); | ||
| } | ||
| init_threadstate(tstate, interp, id, whence); | ||
| add_threadstate(interp, (PyThreadState *)tstate, old_head); | ||
| HEAD_UNLOCK(runtime); | ||
| if (!used_newtstate){ | ||
| // Must be called with lock unlocked to avoid re-entrancy deadlock. | ||
| PyMem_RawFree(new_tstate); | ||
| } | ||
| else{ | ||
| } | ||
| HEAD_UNLOCK(interp->runtime); | ||
| #ifdef Py_GIL_DISABLED | ||
| // Must be called with lock unlocked to avoid lock ordering deadlocks. | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.