@@ -499,6 +499,15 @@ PyEval_ThreadsInitialized(void)
499
499
return _PyEval_ThreadsInitialized ();
500
500
}
501
501
502
+ static inline int
503
+ current_thread_holds_gil (struct _gil_runtime_state * gil , PyThreadState * tstate )
504
+ {
505
+ if (((PyThreadState * )_Py_atomic_load_relaxed (& gil -> last_holder )) != tstate ) {
506
+ return 0 ;
507
+ }
508
+ return _Py_atomic_load_relaxed (& gil -> locked );
509
+ }
510
+
502
511
static void
503
512
init_shared_gil (PyInterpreterState * interp , struct _gil_runtime_state * gil )
504
513
{
@@ -525,8 +534,9 @@ _PyEval_InitGIL(PyThreadState *tstate, int own_gil)
525
534
if (!own_gil ) {
526
535
PyInterpreterState * main_interp = _PyInterpreterState_Main ();
527
536
assert (tstate -> interp != main_interp );
528
- init_shared_gil (tstate -> interp , main_interp -> ceval .gil );
529
- locked = _Py_atomic_load_relaxed (& main_interp -> ceval .gil -> locked );
537
+ struct _gil_runtime_state * gil = main_interp -> ceval .gil ;
538
+ init_shared_gil (tstate -> interp , gil );
539
+ locked = current_thread_holds_gil (gil , tstate );
530
540
}
531
541
/* XXX per-interpreter GIL */
532
542
else if (!_Py_IsMainInterpreter (tstate -> interp )) {
@@ -537,7 +547,7 @@ _PyEval_InitGIL(PyThreadState *tstate, int own_gil)
537
547
init_shared_gil (tstate -> interp , main_gil );
538
548
// XXX For now we lie.
539
549
tstate -> interp -> ceval .own_gil = 1 ;
540
- locked = _Py_atomic_load_relaxed ( & main_gil -> locked );
550
+ locked = current_thread_holds_gil ( main_gil , tstate );
541
551
}
542
552
else {
543
553
PyThread_init_thread ();
0 commit comments