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
Add _PyRuntimeState.main_thread.
  • Loading branch information
ericsnowcurrently committed Mar 8, 2019
commit d6b12ab63ccb2a9e8f7d9ef6feee2cd8fd60ec68
1 change: 0 additions & 1 deletion Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ extern "C" {
#include "pythread.h"

struct _pending_calls {
unsigned long main_thread;
PyThread_type_lock lock;
/* Request for running pending calls. */
_Py_atomic_int calls_to_do;
Expand Down
2 changes: 2 additions & 0 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ typedef struct pyruntimestate {
struct _xidregitem *head;
} xidregistry;

unsigned long main_thread;

#define NEXITFUNCS 32
void (*exitfuncs[NEXITFUNCS])(void);
int nexitfuncs;
Expand Down
22 changes: 11 additions & 11 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,11 @@ PyEval_InitThreads(void)
PyThread_init_thread();
create_gil();
take_gil(_PyThreadState_GET());
_PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
if (!_PyRuntime.ceval.pending.lock)
// Set it to the ID of the main thread of the main interpreter.
_PyRuntime.main_thread = PyThread_get_thread_ident();
if (!_PyRuntime.ceval.pending.lock) {
_PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
}
}

void
Expand Down Expand Up @@ -243,9 +245,9 @@ PyEval_ReInitThreads(void)
if (!gil_created())
return;
recreate_gil();
_PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
take_gil(current_tstate);
_PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
_PyRuntime.main_thread = PyThread_get_thread_ident();
_PyRuntime.ceval.pending.lock = PyThread_allocate_lock();

/* Destroy all threads except the current one */
_PyThreadState_DeleteExcept(current_tstate);
Expand Down Expand Up @@ -395,10 +397,10 @@ Py_AddPendingCall(int (*func)(void *), void *arg)
static int
handle_signals(void)
{
/* Only handle signals on main thread. */
if (_PyRuntime.ceval.pending.main_thread &&
PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread)
{
/* Only handle signals on main thread. PyEval_InitThreads must
* have been called already.
*/
if (PyThread_get_thread_ident() != _PyRuntime.main_thread) {
return 0;
}
/*
Expand All @@ -423,9 +425,7 @@ make_pending_calls(void)
static int busy = 0;

/* only service pending calls on main thread */
if (_PyRuntime.ceval.pending.main_thread &&
PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread)
{
if (PyThread_get_thread_ident() != _PyRuntime.main_thread) {
return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ _PyRuntimeState_Init_impl(_PyRuntimeState *runtime)
return _Py_INIT_ERR("Can't initialize threads for cross-interpreter data registry");
}

// runtime->main_thread is set in PyEval_InitThreads().

return _Py_INIT_OK();
}

Expand Down