@@ -877,21 +877,20 @@ make_pending_calls(PyThreadState *tstate)
877
877
/* Only one thread (per interpreter) may run the pending calls
878
878
at once. In the same way, we don't do recursive pending calls. */
879
879
PyMutex_Lock (& pending -> mutex );
880
- if (pending -> busy ) {
880
+ if (pending -> handling_thread != NULL ) {
881
881
/* A pending call was added after another thread was already
882
882
handling the pending calls (and had already "unsignaled").
883
883
Once that thread is done, it may have taken care of all the
884
884
pending calls, or there might be some still waiting.
885
- Regardless, this interpreter's pending calls will stay
886
- "signaled" until that first thread has finished. At that
887
- point the next thread to trip the eval breaker will take
888
- care of any remaining pending calls. Until then, though,
889
- all the interpreter's threads will be tripping the eval
890
- breaker every time it's checked. */
885
+ To avoid all threads constantly stopping on the eval breaker,
886
+ we clear the bit for this thread and make sure it is set
887
+ for the thread currently handling the pending call. */
888
+ _Py_set_eval_breaker_bit (pending -> handling_thread , _PY_CALLS_TO_DO_BIT );
889
+ _Py_unset_eval_breaker_bit (tstate , _PY_CALLS_TO_DO_BIT );
891
890
PyMutex_Unlock (& pending -> mutex );
892
891
return 0 ;
893
892
}
894
- pending -> busy = 1 ;
893
+ pending -> handling_thread = tstate ;
895
894
PyMutex_Unlock (& pending -> mutex );
896
895
897
896
/* unsignal before starting to call callbacks, so that any callback
@@ -900,7 +899,7 @@ make_pending_calls(PyThreadState *tstate)
900
899
901
900
int32_t npending ;
902
901
if (_make_pending_calls (pending , & npending ) != 0 ) {
903
- pending -> busy = 0 ;
902
+ pending -> handling_thread = NULL ;
904
903
/* There might not be more calls to make, but we play it safe. */
905
904
signal_pending_calls (tstate , interp );
906
905
return -1 ;
@@ -912,7 +911,7 @@ make_pending_calls(PyThreadState *tstate)
912
911
913
912
if (_Py_IsMainThread () && _Py_IsMainInterpreter (interp )) {
914
913
if (_make_pending_calls (pending_main , & npending ) != 0 ) {
915
- pending -> busy = 0 ;
914
+ pending -> handling_thread = NULL ;
916
915
/* There might not be more calls to make, but we play it safe. */
917
916
signal_pending_calls (tstate , interp );
918
917
return -1 ;
@@ -923,7 +922,7 @@ make_pending_calls(PyThreadState *tstate)
923
922
}
924
923
}
925
924
926
- pending -> busy = 0 ;
925
+ pending -> handling_thread = NULL ;
927
926
return 0 ;
928
927
}
929
928
0 commit comments