Skip to content

Commit

Permalink
bpo-33608: Factor out a private, per-interpreter _Py_AddPendingCall(). (
Browse files Browse the repository at this point in the history
GH-11617)

This involves moving the global "pending calls" state to PyInterpreterState.

https://bugs.python.org/issue33608
  • Loading branch information
ericsnowcurrently authored Feb 24, 2019
1 parent 463572c commit ef4ac96
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 121 deletions.
2 changes: 1 addition & 1 deletion Include/ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,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
18 changes: 13 additions & 5 deletions Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ extern "C" {
#include "pycore_atomic.h"
#include "pythread.h"

struct _is; // See PyInterpreterState in cpython/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 @@ -22,13 +26,21 @@ struct _pending_calls {
int async_exc;
#define NPENDINGCALLS 32
struct {
unsigned long thread_id;
int (*func)(void *);
void *arg;
} calls[NPENDINGCALLS];
int first;
int last;
};

struct _ceval_interpreter_state {
/* 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;
};

#include "pycore_gil.h"

struct _ceval_runtime_state {
Expand All @@ -39,12 +51,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;
/* Request for checking signals. */
_Py_atomic_int signals_pending;
struct _gil_runtime_state gil;
Expand Down
7 changes: 7 additions & 0 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern "C" {
#include "pystate.h"
#include "pythread.h"

#include "pycore_atomic.h"
#include "pycore_ceval.h"
#include "pycore_pathconfig.h"
#include "pycore_pymem.h"
Expand All @@ -31,6 +32,8 @@ struct _is {
int64_t id_refcount;
PyThread_type_lock id_mutex;

int finalizing;

PyObject *modules;
PyObject *modules_by_index;
PyObject *sysdict;
Expand Down Expand Up @@ -78,6 +81,8 @@ struct _is {
PyObject *pyexitmodule;

uint64_t tstate_next_unique_id;

struct _ceval_interpreter_state ceval;
};

PyAPI_FUNC(struct _is*) _PyInterpreterState_LookUpID(PY_INT64_T);
Expand Down Expand Up @@ -207,6 +212,8 @@ typedef struct pyruntimestate {
struct _xidregitem *head;
} xidregistry;

unsigned long main_thread;

#define NEXITFUNCS 32
void (*exitfuncs[NEXITFUNCS])(void);
int nexitfuncs;
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 @@ -2445,6 +2445,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
13 changes: 9 additions & 4 deletions Modules/signalmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <process.h>
#endif
#endif
#include "internal/pycore_pystate.h"

#ifdef HAVE_SIGNAL_H
#include <signal.h>
Expand Down Expand Up @@ -295,8 +296,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 @@ -313,8 +316,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
Loading

0 comments on commit ef4ac96

Please sign in to comment.