Skip to content

Commit 3611c58

Browse files
Add _PyRuntime.ceval.pending_mainthread.
1 parent 426628d commit 3611c58

File tree

2 files changed

+33
-40
lines changed

2 files changed

+33
-40
lines changed

Include/internal/pycore_ceval_state.h

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@ extern "C" {
1313
#include "pycore_gil.h" // struct _gil_runtime_state
1414

1515

16+
struct _pending_calls {
17+
int busy;
18+
PyThread_type_lock lock;
19+
/* Request for running pending calls. */
20+
_Py_atomic_int calls_to_do;
21+
/* Request for looking at the `async_exc` field of the current
22+
thread state.
23+
Guarded by the GIL. */
24+
int async_exc;
25+
#define NPENDINGCALLS 32
26+
struct _pending_call {
27+
int (*func)(void *);
28+
void *arg;
29+
} calls[NPENDINGCALLS];
30+
int first;
31+
int last;
32+
};
33+
1634
typedef enum {
1735
PERF_STATUS_FAILED = -1, // Perf trampoline is in an invalid state
1836
PERF_STATUS_NO_INIT = 0, // Perf trampoline is not initialized
@@ -49,6 +67,8 @@ struct _ceval_runtime_state {
4967
the main thread of the main interpreter can handle signals: see
5068
_Py_ThreadCanHandleSignals(). */
5169
_Py_atomic_int signals_pending;
70+
/* Pending calls to be made only on the main thread. */
71+
struct _pending_calls pending_mainthread;
5272
};
5373

5474
#ifdef PY_HAVE_PERF_TRAMPOLINE
@@ -62,24 +82,6 @@ struct _ceval_runtime_state {
6282
#endif
6383

6484

65-
struct _pending_calls {
66-
int busy;
67-
PyThread_type_lock lock;
68-
/* Request for running pending calls. */
69-
_Py_atomic_int calls_to_do;
70-
/* Request for looking at the `async_exc` field of the current
71-
thread state.
72-
Guarded by the GIL. */
73-
int async_exc;
74-
#define NPENDINGCALLS 32
75-
struct _pending_call {
76-
int (*func)(void *);
77-
void *arg;
78-
} calls[NPENDINGCALLS];
79-
int first;
80-
int last;
81-
};
82-
8385
struct _ceval_state {
8486
/* This single variable consolidates all requests to break out of
8587
the fast path in the eval loop. */

Python/pystate.c

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,16 @@ _Py_COMP_DIAG_IGNORE_DEPR_DECLS
380380
static const _PyRuntimeState initial = _PyRuntimeState_INIT(_PyRuntime);
381381
_Py_COMP_DIAG_POP
382382

383-
#define NUMLOCKS 5
383+
#define NUMLOCKS 6
384+
#define LOCKPTRS(runtime) \
385+
{ \
386+
&(runtime)->interpreters.mutex, \
387+
&(runtime)->xidregistry.mutex, \
388+
&(runtime)->getargs.mutex, \
389+
&(runtime)->unicode_state.ids.lock, \
390+
&(runtime)->imports.extensions.mutex, \
391+
&(runtime)->ceval.pending_mainthread.lock, \
392+
}
384393

385394
static int
386395
alloc_for_runtime(PyThread_type_lock locks[NUMLOCKS])
@@ -427,13 +436,7 @@ init_runtime(_PyRuntimeState *runtime,
427436

428437
PyPreConfig_InitPythonConfig(&runtime->preconfig);
429438

430-
PyThread_type_lock *lockptrs[NUMLOCKS] = {
431-
&runtime->interpreters.mutex,
432-
&runtime->xidregistry.mutex,
433-
&runtime->getargs.mutex,
434-
&runtime->unicode_state.ids.lock,
435-
&runtime->imports.extensions.mutex,
436-
};
439+
PyThread_type_lock *lockptrs[NUMLOCKS] = LOCKPTRS(runtime);
437440
for (int i = 0; i < NUMLOCKS; i++) {
438441
assert(locks[i] != NULL);
439442
*lockptrs[i] = locks[i];
@@ -512,13 +515,7 @@ _PyRuntimeState_Fini(_PyRuntimeState *runtime)
512515
LOCK = NULL; \
513516
}
514517

515-
PyThread_type_lock *lockptrs[NUMLOCKS] = {
516-
&runtime->interpreters.mutex,
517-
&runtime->xidregistry.mutex,
518-
&runtime->getargs.mutex,
519-
&runtime->unicode_state.ids.lock,
520-
&runtime->imports.extensions.mutex,
521-
};
518+
PyThread_type_lock *lockptrs[NUMLOCKS] = LOCKPTRS(runtime);
522519
for (int i = 0; i < NUMLOCKS; i++) {
523520
FREE_LOCK(*lockptrs[i]);
524521
}
@@ -541,13 +538,7 @@ _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
541538
PyMemAllocatorEx old_alloc;
542539
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
543540

544-
PyThread_type_lock *lockptrs[NUMLOCKS] = {
545-
&runtime->interpreters.mutex,
546-
&runtime->xidregistry.mutex,
547-
&runtime->getargs.mutex,
548-
&runtime->unicode_state.ids.lock,
549-
&runtime->imports.extensions.mutex,
550-
};
541+
PyThread_type_lock *lockptrs[NUMLOCKS] = LOCKPTRS(runtime);
551542
int reinit_err = 0;
552543
for (int i = 0; i < NUMLOCKS; i++) {
553544
reinit_err += _PyThread_at_fork_reinit(lockptrs[i]);

0 commit comments

Comments
 (0)