Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add _PyThreadState_ClearDetached().
  • Loading branch information
ericsnowcurrently committed Mar 27, 2023
commit 181106e3eef47cdfb5b933b8bd3b6f71da48d36c
2 changes: 2 additions & 0 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ PyAPI_FUNC(void) _PyThreadState_Bind(PyThreadState *tstate);
PyAPI_FUNC(void) _PyThreadState_Init(
PyThreadState *tstate);
PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate);

extern void _PyThreadState_InitDetached(PyThreadState *, PyInterpreterState *);
extern void _PyThreadState_ClearDetached(PyThreadState *);


static inline void
Expand Down
36 changes: 28 additions & 8 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,19 @@ _PyThreadState_InitDetached(PyThreadState *tstate, PyInterpreterState *interp)
// We do not call add_threadstate().
}


static void
clear_datastack(PyThreadState *tstate)
{
_PyStackChunk *chunk = tstate->datastack_chunk;
tstate->datastack_chunk = NULL;
while (chunk != NULL) {
_PyStackChunk *prev = chunk->previous;
_PyObject_VirtualFree(chunk, chunk->size);
chunk = prev;
}
}

void
PyThreadState_Clear(PyThreadState *tstate)
{
Expand Down Expand Up @@ -1445,7 +1458,6 @@ PyThreadState_Clear(PyThreadState *tstate)
// XXX Do it as early in the function as possible.
}


/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
static void
tstate_delete_common(PyThreadState *tstate)
Expand Down Expand Up @@ -1478,17 +1490,25 @@ tstate_delete_common(PyThreadState *tstate)
unbind_tstate(tstate);

// XXX Move to PyThreadState_Clear()?
_PyStackChunk *chunk = tstate->datastack_chunk;
tstate->datastack_chunk = NULL;
while (chunk != NULL) {
_PyStackChunk *prev = chunk->previous;
_PyObject_VirtualFree(chunk, chunk->size);
chunk = prev;
}
clear_datastack(tstate);

tstate->_status.finalized = 1;
}

void
_PyThreadState_ClearDetached(PyThreadState *tstate)
{
assert(!tstate->_status.bound);
assert(!tstate->_status.bound_gilstate);
assert(tstate->datastack_chunk == NULL);
assert(tstate->thread_id == 0);
assert(tstate->native_thread_id == 0);
assert(tstate->next == NULL);
assert(tstate->prev == NULL);

PyThreadState_Clear(tstate);
clear_datastack(tstate);
}

static void
zapthreads(PyInterpreterState *interp)
Expand Down