Skip to content

Commit fc1e4bc

Browse files
Use PyInterpreterState.ceval.gil.
1 parent 279c85e commit fc1e4bc

File tree

5 files changed

+56
-37
lines changed

5 files changed

+56
-37
lines changed

Include/internal/pycore_ceval.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ _PyEval_Vector(PyThreadState *tstate,
9696
PyObject* const* args, size_t argcount,
9797
PyObject *kwnames);
9898

99-
extern int _PyEval_ThreadsInitialized(struct pyruntimestate *runtime);
99+
extern int _PyEval_ThreadsInitialized(void);
100100
extern PyStatus _PyEval_InitGIL(PyThreadState *tstate);
101101
extern void _PyEval_FiniGIL(PyInterpreterState *interp);
102102

Include/internal/pycore_ceval_state.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ struct _ceval_runtime_state {
4949
the main thread of the main interpreter can handle signals: see
5050
_Py_ThreadCanHandleSignals(). */
5151
_Py_atomic_int signals_pending;
52+
53+
/* This is (only) used indirectly through PyInterpreterState.ceval.gil. */
5254
struct _gil_runtime_state gil;
5355
};
5456

Modules/_xxsubinterpretersmodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ interp_create(PyObject *self, PyObject *args, PyObject *kwds)
513513

514514
// Create and initialize the new interpreter.
515515
PyThreadState *save_tstate = _PyThreadState_GET();
516+
assert(save_tstate != NULL);
516517
const PyInterpreterConfig config = isolated
517518
? (PyInterpreterConfig)_PyInterpreterConfig_INIT
518519
: (PyInterpreterConfig)_PyInterpreterConfig_LEGACY_INIT;

Python/ceval_gil.c

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ static void _gil_initialize(struct _gil_runtime_state *gil)
229229

230230
static int gil_created(struct _gil_runtime_state *gil)
231231
{
232+
if (gil == NULL) {
233+
return 0;
234+
}
232235
return (_Py_atomic_load_explicit(&gil->locked, _Py_memory_order_acquire) >= 0);
233236
}
234237

@@ -273,10 +276,9 @@ static void recreate_gil(struct _gil_runtime_state *gil)
273276
#endif
274277

275278
static void
276-
drop_gil(struct _ceval_runtime_state *ceval, struct _ceval_state *ceval2,
277-
PyThreadState *tstate)
279+
drop_gil(struct _ceval_state *ceval, PyThreadState *tstate)
278280
{
279-
struct _gil_runtime_state *gil = &ceval->gil;
281+
struct _gil_runtime_state *gil = ceval->gil;
280282
if (!_Py_atomic_load_relaxed(&gil->locked)) {
281283
Py_FatalError("drop_gil: GIL is not locked");
282284
}
@@ -296,7 +298,7 @@ drop_gil(struct _ceval_runtime_state *ceval, struct _ceval_state *ceval2,
296298
MUTEX_UNLOCK(gil->mutex);
297299

298300
#ifdef FORCE_SWITCHING
299-
if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request) && tstate != NULL) {
301+
if (_Py_atomic_load_relaxed(&ceval->gil_drop_request) && tstate != NULL) {
300302
MUTEX_LOCK(gil->switch_mutex);
301303
/* Not switched yet => wait */
302304
if (((PyThreadState*)_Py_atomic_load_relaxed(&gil->last_holder)) == tstate)
@@ -358,9 +360,8 @@ take_gil(PyThreadState *tstate)
358360

359361
assert(is_tstate_valid(tstate));
360362
PyInterpreterState *interp = tstate->interp;
361-
struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
362-
struct _ceval_state *ceval2 = &interp->ceval;
363-
struct _gil_runtime_state *gil = &ceval->gil;
363+
struct _ceval_state *ceval = &interp->ceval;
364+
struct _gil_runtime_state *gil = ceval->gil;
364365

365366
/* Check that _PyEval_InitThreads() was called to create the lock */
366367
assert(gil_created(gil));
@@ -434,12 +435,12 @@ take_gil(PyThreadState *tstate)
434435
in take_gil() while the main thread called
435436
wait_for_thread_shutdown() from Py_Finalize(). */
436437
MUTEX_UNLOCK(gil->mutex);
437-
drop_gil(ceval, ceval2, tstate);
438+
drop_gil(ceval, tstate);
438439
PyThread_exit_thread();
439440
}
440441
assert(is_tstate_valid(tstate));
441442

442-
if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
443+
if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
443444
RESET_GIL_DROP_REQUEST(interp);
444445
}
445446
else {
@@ -448,7 +449,7 @@ take_gil(PyThreadState *tstate)
448449
handle signals.
449450
450451
Note: RESET_GIL_DROP_REQUEST() calls COMPUTE_EVAL_BREAKER(). */
451-
COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
452+
COMPUTE_EVAL_BREAKER(interp, &_PyRuntime.ceval, ceval);
452453
}
453454

454455
/* Don't access tstate if the thread must exit */
@@ -463,33 +464,47 @@ take_gil(PyThreadState *tstate)
463464

464465
void _PyEval_SetSwitchInterval(unsigned long microseconds)
465466
{
466-
struct _gil_runtime_state *gil = &_PyRuntime.ceval.gil;
467+
/* XXX per-interpreter GIL */
468+
PyInterpreterState *interp = _PyInterpreterState_Main();
469+
struct _gil_runtime_state *gil = interp->ceval.gil;
470+
assert(gil != NULL);
467471
gil->interval = microseconds;
468472
}
469473

470474
unsigned long _PyEval_GetSwitchInterval(void)
471475
{
472-
struct _gil_runtime_state *gil = &_PyRuntime.ceval.gil;
476+
/* XXX per-interpreter GIL */
477+
PyInterpreterState *interp = _PyInterpreterState_Main();
478+
struct _gil_runtime_state *gil = interp->ceval.gil;
479+
assert(gil != NULL);
473480
return gil->interval;
474481
}
475482

476483

477484
int
478-
_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
485+
_PyEval_ThreadsInitialized(void)
479486
{
480-
return gil_created(&runtime->ceval.gil);
487+
/* XXX per-interpreter GIL */
488+
PyInterpreterState *interp = _PyInterpreterState_Main();
489+
if (interp == NULL) {
490+
return 0;
491+
}
492+
struct _gil_runtime_state *gil = interp->ceval.gil;
493+
return gil_created(gil);
481494
}
482495

483496
int
484497
PyEval_ThreadsInitialized(void)
485498
{
486-
_PyRuntimeState *runtime = &_PyRuntime;
487-
return _PyEval_ThreadsInitialized(runtime);
499+
return _PyEval_ThreadsInitialized();
488500
}
489501

490502
PyStatus
491503
_PyEval_InitGIL(PyThreadState *tstate)
492504
{
505+
assert(tstate->interp->ceval.gil == NULL);
506+
507+
/* XXX per-interpreter GIL */
493508
struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
494509
if (!_Py_IsMainInterpreter(tstate->interp)) {
495510
/* Currently, the GIL is shared by all interpreters,
@@ -504,16 +519,21 @@ _PyEval_InitGIL(PyThreadState *tstate)
504519

505520
PyThread_init_thread();
506521
create_gil(gil);
507-
take_gil(tstate);
508522
assert(gil_created(gil));
509-
510523
tstate->interp->ceval.gil = gil;
524+
take_gil(tstate);
511525
return _PyStatus_OK();
512526
}
513527

514528
void
515529
_PyEval_FiniGIL(PyInterpreterState *interp)
516530
{
531+
if (interp->ceval.gil == NULL) {
532+
/* It was already finalized (or hasn't been initialized yet). */
533+
return;
534+
}
535+
536+
/* XXX per-interpreter GIL */
517537
struct _gil_runtime_state *gil = &interp->runtime->ceval.gil;
518538
if (!_Py_IsMainInterpreter(interp)) {
519539
/* Currently, the GIL is shared by all interpreters,
@@ -560,22 +580,19 @@ PyEval_AcquireLock(void)
560580
void
561581
PyEval_ReleaseLock(void)
562582
{
563-
_PyRuntimeState *runtime = &_PyRuntime;
564583
PyThreadState *tstate = _PyThreadState_GET();
565584
/* This function must succeed when the current thread state is NULL.
566585
We therefore avoid PyThreadState_Get() which dumps a fatal error
567586
in debug mode. */
568-
struct _ceval_runtime_state *ceval = &runtime->ceval;
569-
struct _ceval_state *ceval2 = &tstate->interp->ceval;
570-
drop_gil(ceval, ceval2, tstate);
587+
struct _ceval_state *ceval = &tstate->interp->ceval;
588+
drop_gil(ceval, tstate);
571589
}
572590

573591
void
574592
_PyEval_ReleaseLock(PyThreadState *tstate)
575593
{
576-
struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
577-
struct _ceval_state *ceval2 = &tstate->interp->ceval;
578-
drop_gil(ceval, ceval2, tstate);
594+
struct _ceval_state *ceval = &tstate->interp->ceval;
595+
drop_gil(ceval, tstate);
579596
}
580597

581598
void
@@ -600,9 +617,8 @@ PyEval_ReleaseThread(PyThreadState *tstate)
600617
if (new_tstate != tstate) {
601618
Py_FatalError("wrong thread state");
602619
}
603-
struct _ceval_runtime_state *ceval = &runtime->ceval;
604-
struct _ceval_state *ceval2 = &tstate->interp->ceval;
605-
drop_gil(ceval, ceval2, tstate);
620+
struct _ceval_state *ceval = &tstate->interp->ceval;
621+
drop_gil(ceval, tstate);
606622
}
607623

608624
#ifdef HAVE_FORK
@@ -612,9 +628,9 @@ PyEval_ReleaseThread(PyThreadState *tstate)
612628
PyStatus
613629
_PyEval_ReInitThreads(PyThreadState *tstate)
614630
{
615-
_PyRuntimeState *runtime = tstate->interp->runtime;
631+
assert(tstate->interp == _PyInterpreterState_Main());
616632

617-
struct _gil_runtime_state *gil = &runtime->ceval.gil;
633+
struct _gil_runtime_state *gil = tstate->interp->ceval.gil;
618634
if (!gil_created(gil)) {
619635
return _PyStatus_OK();
620636
}
@@ -649,10 +665,9 @@ PyEval_SaveThread(void)
649665
PyThreadState *tstate = _PyThreadState_Swap(runtime, NULL);
650666
_Py_EnsureTstateNotNULL(tstate);
651667

652-
struct _ceval_runtime_state *ceval = &runtime->ceval;
653-
struct _ceval_state *ceval2 = &tstate->interp->ceval;
654-
assert(gil_created(&ceval->gil));
655-
drop_gil(ceval, ceval2, tstate);
668+
struct _ceval_state *ceval = &tstate->interp->ceval;
669+
assert(gil_created(ceval->gil));
670+
drop_gil(ceval, tstate);
656671
return tstate;
657672
}
658673

@@ -911,6 +926,7 @@ Py_MakePendingCalls(void)
911926
void
912927
_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
913928
{
929+
/* XXX per-interpreter GIL */
914930
_gil_initialize(&ceval->gil);
915931
}
916932

@@ -969,7 +985,7 @@ _Py_HandlePending(PyThreadState *tstate)
969985
if (_PyThreadState_Swap(runtime, NULL) != tstate) {
970986
Py_FatalError("tstate mix-up");
971987
}
972-
drop_gil(ceval, interp_ceval_state, tstate);
988+
drop_gil(interp_ceval_state, tstate);
973989

974990
/* Other threads may run now */
975991

Python/pystate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2186,7 +2186,7 @@ PyGILState_Ensure(void)
21862186

21872187
/* Ensure that _PyEval_InitThreads() and _PyGILState_Init() have been
21882188
called by Py_Initialize() */
2189-
assert(_PyEval_ThreadsInitialized(runtime));
2189+
assert(_PyEval_ThreadsInitialized());
21902190
assert(gilstate_tss_initialized(runtime));
21912191
assert(runtime->gilstate.autoInterpreterState != NULL);
21922192

0 commit comments

Comments
 (0)