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_BindDetached() and _PyThreadState_UnbindDetached().
  • Loading branch information
ericsnowcurrently committed Mar 28, 2023
commit d79afe586c31bdb342f5aa7289f9320ed7d91c9d
2 changes: 2 additions & 0 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate);

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


static inline void
Expand Down
96 changes: 67 additions & 29 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1357,20 +1357,6 @@ _PyThreadState_Init(PyThreadState *tstate)
Py_FatalError("_PyThreadState_Init() is for internal use only");
}

void
_PyThreadState_InitDetached(PyThreadState *tstate, PyInterpreterState *interp)
{
_PyRuntimeState *runtime = interp->runtime;

HEAD_LOCK(runtime);
interp->threads.next_unique_id += 1;
uint64_t id = interp->threads.next_unique_id;
HEAD_UNLOCK(runtime);

init_threadstate(tstate, interp, id);
// We do not call add_threadstate().
}


static void
clear_datastack(PyThreadState *tstate)
Expand Down Expand Up @@ -1495,21 +1481,6 @@ tstate_delete_common(PyThreadState *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 Expand Up @@ -1596,6 +1567,73 @@ _PyThreadState_DeleteExcept(PyThreadState *tstate)
}


//-------------------------
// "detached" thread states
//-------------------------

void
_PyThreadState_InitDetached(PyThreadState *tstate, PyInterpreterState *interp)
{
_PyRuntimeState *runtime = interp->runtime;

HEAD_LOCK(runtime);
interp->threads.next_unique_id += 1;
uint64_t id = interp->threads.next_unique_id;
HEAD_UNLOCK(runtime);

init_threadstate(tstate, interp, id);
// We do not call add_threadstate().
}

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);
}

void
_PyThreadState_BindDetached(PyThreadState *tstate)
{
assert(_Py_IsMainInterpreter(tstate->interp));
assert(_Py_IsMainInterpreter(_PyInterpreterState_GET()));
bind_tstate(tstate);
/* Unlike _PyThreadState_Bind(), we do not modify gilstate TSS. */
}

void
_PyThreadState_UnbindDetached(PyThreadState *tstate)
{
assert(_Py_IsMainInterpreter(tstate->interp));
assert(_Py_IsMainInterpreter(_PyInterpreterState_GET()));
assert(tstate_is_alive(tstate));
assert(!tstate->_status.active);
assert(gilstate_tss_get(tstate->interp->runtime) != tstate);

unbind_tstate(tstate);

/* This thread state may be bound/unbound repeatedly,
so we must erase evidence that it was ever bound (or unbound). */
tstate->_status.bound = 0;
tstate->_status.unbound = 0;

/* We must fully unlink the thread state from any OS thread,
to allow it to be bound more than once. */
tstate->thread_id = 0;
#ifdef PY_HAVE_THREAD_NATIVE_ID
tstate->native_thread_id = 0;
#endif
}


//----------
// accessors
//----------
Expand Down