Skip to content

[WIP] bpo-33608: Factor out a private, per-interpreter _Py_AddPendingCall(). #9334

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion Include/ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ PyAPI_FUNC(Py_ssize_t) _PyEval_RequestCodeExtraIndex(freefunc);
#ifndef Py_LIMITED_API
PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
PyAPI_FUNC(int) _PyEval_SliceIndexNotNone(PyObject *, Py_ssize_t *);
PyAPI_FUNC(void) _PyEval_SignalAsyncExc(void);
PyAPI_FUNC(void) _PyEval_SignalAsyncExc(PyInterpreterState *);
#endif

/* Masks and values used by FORMAT_VALUE opcode. */
Expand Down
11 changes: 6 additions & 5 deletions Include/internal/ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ extern "C" {
#include "pyatomic.h"
#include "pythread.h"

struct _is; // See PyInterpreterState in pystate.h.

PyAPI_FUNC(int) _Py_AddPendingCall(struct _is*, unsigned long, int (*)(void *), void *);
PyAPI_FUNC(int) _Py_MakePendingCalls(struct _is*);

struct _pending_calls {
unsigned long main_thread;
PyThread_type_lock lock;
/* Request for running pending calls. */
_Py_atomic_int calls_to_do;
Expand All @@ -18,6 +22,7 @@ struct _pending_calls {
int async_exc;
#define NPENDINGCALLS 32
struct {
unsigned long thread_id;
int (*func)(void *);
void *arg;
} calls[NPENDINGCALLS];
Expand All @@ -35,12 +40,8 @@ struct _ceval_runtime_state {
c_tracefunc. This speeds up the if statement in
PyEval_EvalFrameEx() after fast_next_opcode. */
int tracing_possible;
/* This single variable consolidates all requests to break out of
the fast path in the eval loop. */
_Py_atomic_int eval_breaker;
/* Request for dropping the GIL */
_Py_atomic_int gil_drop_request;
struct _pending_calls pending;
struct _gil_runtime_state gil;
};

Expand Down
4 changes: 4 additions & 0 deletions Include/internal/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ typedef struct pyruntimestate {
struct _xidregitem *head;
} xidregistry;

unsigned long main_thread;

#define NEXITFUNCS 32
void (*exitfuncs[NEXITFUNCS])(void);
int nexitfuncs;
Expand All @@ -211,6 +213,8 @@ PyAPI_FUNC(void) _PyRuntimeState_Fini(_PyRuntimeState *);
Return NULL on success, or return an error message on failure. */
PyAPI_FUNC(_PyInitError) _PyRuntime_Initialize(void);

PyAPI_FUNC(unsigned long) _PyRuntimeState_GetMainThreadID(_PyRuntimeState*);

#define _Py_CURRENTLY_FINALIZING(tstate) \
(_PyRuntime.finalizing == tstate)

Expand Down
15 changes: 15 additions & 0 deletions Include/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ extern "C" {

#include "pythread.h"
#include "coreconfig.h"
#ifdef Py_BUILD_CORE
#include "pyatomic.h"
#include "internal/ceval.h"
#endif

/* This limitation is for performance and simplicity. If needed it can be
removed (with effort). */
Expand Down Expand Up @@ -56,6 +60,8 @@ typedef struct _is {
int64_t id_refcount;
PyThread_type_lock id_mutex;

int finalizing;

PyObject *modules;
PyObject *modules_by_index;
PyObject *sysdict;
Expand All @@ -65,6 +71,15 @@ typedef struct _is {
/* Used in Python/sysmodule.c. */
int check_interval;

#ifdef Py_BUILD_CORE
struct _ceval {
/* This single variable consolidates all requests to break out of
the fast path in the eval loop. */
_Py_atomic_int eval_breaker;
struct _pending_calls pending;
} ceval;
#endif

/* Used in Modules/_threadmodule.c. */
long num_threads;
/* Support for runtime thread stack size tuning.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
We added a new internal _Py_AddPendingCall() that operates relative to the
provided interpreter. This allows us to use the existing implementation to
ask another interpreter to do work that cannot be done in the current
interpreter, like decref an object the other interpreter owns. The existing
Py_AddPendingCall() only operates relative to the main interpreter.
1 change: 1 addition & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2441,6 +2441,7 @@ pending_threadfunc(PyObject *self, PyObject *arg)
Py_INCREF(callable);

Py_BEGIN_ALLOW_THREADS
/* XXX Use the internal _Py_AddPendingCall(). */
r = Py_AddPendingCall(&_pending_callback, callable);
Py_END_ALLOW_THREADS

Expand Down
17 changes: 10 additions & 7 deletions Modules/signalmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <process.h>
#endif
#endif
#include "internal/pystate.h"

#ifdef HAVE_SIGNAL_H
#include <signal.h>
Expand Down Expand Up @@ -95,7 +96,7 @@ class sigset_t_converter(CConverter):

#include <sys/types.h> /* For pid_t */
#include "pythread.h"
static unsigned long main_thread;
#define main_thread _PyRuntimeState_GetMainThreadID(&_PyRuntime)
static pid_t main_pid;

static volatile struct {
Expand Down Expand Up @@ -279,8 +280,10 @@ trip_signal(int sig_num)
{
/* Py_AddPendingCall() isn't signal-safe, but we
still use it for this exceptional case. */
Py_AddPendingCall(report_wakeup_send_error,
(void *)(intptr_t) last_error);
_Py_AddPendingCall(_PyRuntime.interpreters.main,
main_thread,
report_wakeup_send_error,
(void *)(intptr_t) last_error);
}
}
}
Expand All @@ -297,8 +300,10 @@ trip_signal(int sig_num)
{
/* Py_AddPendingCall() isn't signal-safe, but we
still use it for this exceptional case. */
Py_AddPendingCall(report_wakeup_write_error,
(void *)(intptr_t)errno);
_Py_AddPendingCall(_PyRuntime.interpreters.main,
main_thread,
report_wakeup_write_error,
(void *)(intptr_t)errno);
}
}
}
Expand Down Expand Up @@ -1278,7 +1283,6 @@ PyInit__signal(void)
PyObject *m, *d, *x;
int i;

main_thread = PyThread_get_thread_ident();
main_pid = getpid();

/* Create the module and add the functions */
Expand Down Expand Up @@ -1680,7 +1684,6 @@ _PySignal_AfterFork(void)
* in both processes if they came in just before the fork() but before
* the interpreter had an opportunity to call the handlers. issue9535. */
_clear_pending_signals();
main_thread = PyThread_get_thread_ident();
main_pid = getpid();
}

Expand Down
Loading