Skip to content

bpo-33608: Factor out a private, per-interpreter _Py_AddPendingCall(). #13714

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
59b2d34
ceval -> ceval_r
ericsnowcurrently May 31, 2019
ceceab7
_Py_FinishPendingCalls -> _PyEval_FinishPendingCalls
ericsnowcurrently May 31, 2019
3fbc879
Drop the GIL_REQUEST macro.
ericsnowcurrently May 31, 2019
dedcf7f
Move pending calls from _PyRuntimeState to PyIntepreterState.
ericsnowcurrently Jul 13, 2018
b2c8a7b
Release the pending calls lock *after* updating the eval breaker flag.
ericsnowcurrently Mar 15, 2019
05e2808
Use the internal function.
ericsnowcurrently Sep 14, 2018
d9d70e8
Add a NEWS entry.
ericsnowcurrently Sep 15, 2018
b02a36b
Optionally lock a pending call to a specific thread.
ericsnowcurrently Sep 15, 2018
28a4583
Move core-only field to end of struct.
ericsnowcurrently Feb 1, 2019
b7150c7
Add a TODO.
ericsnowcurrently Feb 18, 2019
87e15fe
Use _Py_AddPendingCall() for releasing shared data.
ericsnowcurrently Sep 14, 2018
07957ba
Check the result of adding the pending call (for releasing XID).
ericsnowcurrently Jan 28, 2019
78c49a0
Make a copy of data-to-be-deleted.
ericsnowcurrently Jan 29, 2019
70ef8f9
Fix the pending-for-other-thread case.
ericsnowcurrently Apr 5, 2019
b392d32
Ignore any lingering pending calls.
ericsnowcurrently Jun 1, 2019
d8ac442
Always use the main interpreter for signals.
ericsnowcurrently Jun 1, 2019
7c0e3cb
Add more information to the TODO in Py_FinalizeEx().
ericsnowcurrently Jun 1, 2019
b8cfc36
Drop a superfluous comment.
ericsnowcurrently Jun 1, 2019
652c7bc
Do not assume that tstate->interp is not NULL.
ericsnowcurrently Jun 1, 2019
fb0fe21
_pending_calls -> _ceval_pending_calls
ericsnowcurrently Jun 1, 2019
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
13 changes: 8 additions & 5 deletions Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ extern "C" {
#include "pycore_pystate.h"
#include "pythread.h"

PyAPI_FUNC(void) _Py_FinishPendingCalls(_PyRuntimeState *runtime);
PyAPI_FUNC(void) _PyEval_Initialize(struct _ceval_runtime_state *);
PyAPI_FUNC(void) _PyEval_FiniThreads(
struct _ceval_runtime_state *ceval);
struct _ceval_runtime_state *);
PyAPI_FUNC(void) _PyEval_SignalReceived(
struct _ceval_runtime_state *ceval);
struct _ceval_runtime_state *);
PyAPI_FUNC(int) _PyEval_AddPendingCall(
PyThreadState *tstate,
struct _ceval_runtime_state *ceval,
struct _ceval_runtime_state *,
struct _ceval_interpreter_state *,
unsigned long thread_id,
int (*func)(void *),
void *arg);
PyAPI_FUNC(void) _PyEval_FinishPendingCalls(PyInterpreterState *);
PyAPI_FUNC(void) _PyEval_SignalAsyncExc(
struct _ceval_runtime_state *ceval);
struct _ceval_runtime_state *,
struct _ceval_interpreter_state *);
PyAPI_FUNC(void) _PyEval_ReInitThreads(
_PyRuntimeState *runtime);

Expand Down
12 changes: 10 additions & 2 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct pyruntimestate;

/* ceval state */

struct _pending_calls {
struct _ceval_pending_calls {
int finishing;
PyThread_type_lock lock;
/* Request for running pending calls. */
Expand All @@ -36,6 +36,7 @@ struct _pending_calls {
int async_exc;
#define NPENDINGCALLS 32
struct {
unsigned long thread_id;
int (*func)(void *);
void *arg;
} calls[NPENDINGCALLS];
Expand All @@ -53,15 +54,21 @@ struct _ceval_runtime_state {
int tracing_possible;
/* This single variable consolidates all requests to break out of
the fast path in the eval loop. */
// XXX This can move to _ceval_interpreter_state once all parts
// from COMPUTE_EVAL_BREAKER have moved under PyInterpreterState.
_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;
};

struct _ceval_interpreter_state {
struct _ceval_pending_calls pending;
};


/* interpreter state */

typedef PyObject* (*_PyFrameEvalFunction)(struct _frame *, int);
Expand Down Expand Up @@ -136,6 +143,7 @@ struct _is {

uint64_t tstate_next_unique_id;

struct _ceval_interpreter_state ceval;
struct _warnings_runtime_state warnings;

PyObject *audit_hooks;
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_capi.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ def pendingcalls_wait(self, l, n, context = None):
def test_pendingcalls_threaded(self):

#do every callback on a separate thread
n = 32 #total callbacks
n = 32 #total callbacks (see NPENDINGCALLS in pycore_ceval.h)
threads = []
class foo(object):pass
context = foo()
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 @@ -2677,6 +2677,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
12 changes: 10 additions & 2 deletions Modules/signalmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <process.h>
#endif
#endif
#include "internal/pycore_pystate.h"

#ifdef HAVE_SIGNAL_H
#include <signal.h>
Expand Down Expand Up @@ -259,6 +260,7 @@ trip_signal(int sig_num)
/* Notify ceval.c */
_PyRuntimeState *runtime = &_PyRuntime;
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
PyInterpreterState *interp = runtime->interpreters.main;
_PyEval_SignalReceived(&runtime->ceval);

/* And then write to the wakeup fd *after* setting all the globals and
Expand Down Expand Up @@ -299,7 +301,10 @@ trip_signal(int sig_num)
{
/* Py_AddPendingCall() isn't signal-safe, but we
still use it for this exceptional case. */
_PyEval_AddPendingCall(tstate, &runtime->ceval,
_PyEval_AddPendingCall(tstate,
&runtime->ceval,
&interp->ceval,
runtime->main_thread,
report_wakeup_send_error,
(void *)(intptr_t) last_error);
}
Expand All @@ -318,7 +323,10 @@ trip_signal(int sig_num)
{
/* Py_AddPendingCall() isn't signal-safe, but we
still use it for this exceptional case. */
_PyEval_AddPendingCall(tstate, &runtime->ceval,
_PyEval_AddPendingCall(tstate,
&runtime->ceval,
&interp->ceval,
runtime->main_thread,
report_wakeup_write_error,
(void *)(intptr_t)errno);
}
Expand Down
Loading