• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 #include "Python.h"
3 #include "pycore_ceval.h"         // _PyEval_SignalReceived()
4 #include "pycore_initconfig.h"    // _PyStatus_OK()
5 #include "pycore_interp.h"        // _Py_RunGC()
6 #include "pycore_pyerrors.h"      // _PyErr_GetRaisedException()
7 #include "pycore_pylifecycle.h"   // _PyErr_Print()
8 #include "pycore_pymem.h"         // _PyMem_IsPtrFreed()
9 #include "pycore_pystats.h"       // _Py_PrintSpecializationStats()
10 
11 /*
12    Notes about the implementation:
13 
14    - The GIL is just a boolean variable (locked) whose access is protected
15      by a mutex (gil_mutex), and whose changes are signalled by a condition
16      variable (gil_cond). gil_mutex is taken for short periods of time,
17      and therefore mostly uncontended.
18 
19    - In the GIL-holding thread, the main loop (PyEval_EvalFrameEx) must be
20      able to release the GIL on demand by another thread. A volatile boolean
21      variable (gil_drop_request) is used for that purpose, which is checked
22      at every turn of the eval loop. That variable is set after a wait of
23      `interval` microseconds on `gil_cond` has timed out.
24 
25       [Actually, another volatile boolean variable (eval_breaker) is used
26        which ORs several conditions into one. Volatile booleans are
27        sufficient as inter-thread signalling means since Python is run
28        on cache-coherent architectures only.]
29 
30    - A thread wanting to take the GIL will first let pass a given amount of
31      time (`interval` microseconds) before setting gil_drop_request. This
32      encourages a defined switching period, but doesn't enforce it since
33      opcodes can take an arbitrary time to execute.
34 
35      The `interval` value is available for the user to read and modify
36      using the Python API `sys.{get,set}switchinterval()`.
37 
38    - When a thread releases the GIL and gil_drop_request is set, that thread
39      ensures that another GIL-awaiting thread gets scheduled.
40      It does so by waiting on a condition variable (switch_cond) until
41      the value of last_holder is changed to something else than its
42      own thread state pointer, indicating that another thread was able to
43      take the GIL.
44 
45      This is meant to prohibit the latency-adverse behaviour on multi-core
46      machines where one thread would speculatively release the GIL, but still
47      run and end up being the first to re-acquire it, making the "timeslices"
48      much longer than expected.
49      (Note: this mechanism is enabled with FORCE_SWITCHING above)
50 */
51 
52 // GH-89279: Force inlining by using a macro.
53 #if defined(_MSC_VER) && SIZEOF_INT == 4
54 #define _Py_atomic_load_relaxed_int32(ATOMIC_VAL) (assert(sizeof((ATOMIC_VAL)->_value) == 4), *((volatile int*)&((ATOMIC_VAL)->_value)))
55 #else
56 #define _Py_atomic_load_relaxed_int32(ATOMIC_VAL) _Py_atomic_load_relaxed(ATOMIC_VAL)
57 #endif
58 
59 // Atomically copy the bits indicated by mask between two values.
60 static inline void
copy_eval_breaker_bits(uintptr_t * from,uintptr_t * to,uintptr_t mask)61 copy_eval_breaker_bits(uintptr_t *from, uintptr_t *to, uintptr_t mask)
62 {
63     uintptr_t from_bits = _Py_atomic_load_uintptr_relaxed(from) & mask;
64     uintptr_t old_value = _Py_atomic_load_uintptr_relaxed(to);
65     uintptr_t to_bits = old_value & mask;
66     if (from_bits == to_bits) {
67         return;
68     }
69 
70     uintptr_t new_value;
71     do {
72         new_value = (old_value & ~mask) | from_bits;
73     } while (!_Py_atomic_compare_exchange_uintptr(to, &old_value, new_value));
74 }
75 
76 // When attaching a thread, set the global instrumentation version and
77 // _PY_CALLS_TO_DO_BIT from the current state of the interpreter.
78 static inline void
update_eval_breaker_for_thread(PyInterpreterState * interp,PyThreadState * tstate)79 update_eval_breaker_for_thread(PyInterpreterState *interp, PyThreadState *tstate)
80 {
81 #ifdef Py_GIL_DISABLED
82     // Free-threaded builds eagerly update the eval_breaker on *all* threads as
83     // needed, so this function doesn't apply.
84     return;
85 #endif
86 
87     int32_t npending = _Py_atomic_load_int32_relaxed(
88         &interp->ceval.pending.npending);
89     if (npending) {
90         _Py_set_eval_breaker_bit(tstate, _PY_CALLS_TO_DO_BIT);
91     }
92     else if (_Py_IsMainThread()) {
93         npending = _Py_atomic_load_int32_relaxed(
94             &_PyRuntime.ceval.pending_mainthread.npending);
95         if (npending) {
96             _Py_set_eval_breaker_bit(tstate, _PY_CALLS_TO_DO_BIT);
97         }
98     }
99 
100     // _PY_CALLS_TO_DO_BIT was derived from other state above, so the only bits
101     // we copy from our interpreter's state are the instrumentation version.
102     copy_eval_breaker_bits(&interp->ceval.instrumentation_version,
103                            &tstate->eval_breaker,
104                            ~_PY_EVAL_EVENTS_MASK);
105 }
106 
107 /*
108  * Implementation of the Global Interpreter Lock (GIL).
109  */
110 
111 #include <stdlib.h>
112 #include <errno.h>
113 
114 #include "condvar.h"
115 
116 #define MUTEX_INIT(mut) \
117     if (PyMUTEX_INIT(&(mut))) { \
118         Py_FatalError("PyMUTEX_INIT(" #mut ") failed"); };
119 #define MUTEX_FINI(mut) \
120     if (PyMUTEX_FINI(&(mut))) { \
121         Py_FatalError("PyMUTEX_FINI(" #mut ") failed"); };
122 #define MUTEX_LOCK(mut) \
123     if (PyMUTEX_LOCK(&(mut))) { \
124         Py_FatalError("PyMUTEX_LOCK(" #mut ") failed"); };
125 #define MUTEX_UNLOCK(mut) \
126     if (PyMUTEX_UNLOCK(&(mut))) { \
127         Py_FatalError("PyMUTEX_UNLOCK(" #mut ") failed"); };
128 
129 #define COND_INIT(cond) \
130     if (PyCOND_INIT(&(cond))) { \
131         Py_FatalError("PyCOND_INIT(" #cond ") failed"); };
132 #define COND_FINI(cond) \
133     if (PyCOND_FINI(&(cond))) { \
134         Py_FatalError("PyCOND_FINI(" #cond ") failed"); };
135 #define COND_SIGNAL(cond) \
136     if (PyCOND_SIGNAL(&(cond))) { \
137         Py_FatalError("PyCOND_SIGNAL(" #cond ") failed"); };
138 #define COND_WAIT(cond, mut) \
139     if (PyCOND_WAIT(&(cond), &(mut))) { \
140         Py_FatalError("PyCOND_WAIT(" #cond ") failed"); };
141 #define COND_TIMED_WAIT(cond, mut, microseconds, timeout_result) \
142     { \
143         int r = PyCOND_TIMEDWAIT(&(cond), &(mut), (microseconds)); \
144         if (r < 0) \
145             Py_FatalError("PyCOND_WAIT(" #cond ") failed"); \
146         if (r) /* 1 == timeout, 2 == impl. can't say, so assume timeout */ \
147             timeout_result = 1; \
148         else \
149             timeout_result = 0; \
150     } \
151 
152 
153 #define DEFAULT_INTERVAL 5000
154 
_gil_initialize(struct _gil_runtime_state * gil)155 static void _gil_initialize(struct _gil_runtime_state *gil)
156 {
157     gil->locked = -1;
158     gil->interval = DEFAULT_INTERVAL;
159 }
160 
gil_created(struct _gil_runtime_state * gil)161 static int gil_created(struct _gil_runtime_state *gil)
162 {
163     if (gil == NULL) {
164         return 0;
165     }
166     return (_Py_atomic_load_int_acquire(&gil->locked) >= 0);
167 }
168 
create_gil(struct _gil_runtime_state * gil)169 static void create_gil(struct _gil_runtime_state *gil)
170 {
171     MUTEX_INIT(gil->mutex);
172 #ifdef FORCE_SWITCHING
173     MUTEX_INIT(gil->switch_mutex);
174 #endif
175     COND_INIT(gil->cond);
176 #ifdef FORCE_SWITCHING
177     COND_INIT(gil->switch_cond);
178 #endif
179     _Py_atomic_store_ptr_relaxed(&gil->last_holder, 0);
180     _Py_ANNOTATE_RWLOCK_CREATE(&gil->locked);
181     _Py_atomic_store_int_release(&gil->locked, 0);
182 }
183 
destroy_gil(struct _gil_runtime_state * gil)184 static void destroy_gil(struct _gil_runtime_state *gil)
185 {
186     /* some pthread-like implementations tie the mutex to the cond
187      * and must have the cond destroyed first.
188      */
189     COND_FINI(gil->cond);
190     MUTEX_FINI(gil->mutex);
191 #ifdef FORCE_SWITCHING
192     COND_FINI(gil->switch_cond);
193     MUTEX_FINI(gil->switch_mutex);
194 #endif
195     _Py_atomic_store_int_release(&gil->locked, -1);
196     _Py_ANNOTATE_RWLOCK_DESTROY(&gil->locked);
197 }
198 
199 #ifdef HAVE_FORK
recreate_gil(struct _gil_runtime_state * gil)200 static void recreate_gil(struct _gil_runtime_state *gil)
201 {
202     _Py_ANNOTATE_RWLOCK_DESTROY(&gil->locked);
203     /* XXX should we destroy the old OS resources here? */
204     create_gil(gil);
205 }
206 #endif
207 
208 static inline void
drop_gil_impl(PyThreadState * tstate,struct _gil_runtime_state * gil)209 drop_gil_impl(PyThreadState *tstate, struct _gil_runtime_state *gil)
210 {
211     MUTEX_LOCK(gil->mutex);
212     _Py_ANNOTATE_RWLOCK_RELEASED(&gil->locked, /*is_write=*/1);
213     _Py_atomic_store_int_relaxed(&gil->locked, 0);
214     if (tstate != NULL) {
215         tstate->_status.holds_gil = 0;
216     }
217     COND_SIGNAL(gil->cond);
218     MUTEX_UNLOCK(gil->mutex);
219 }
220 
221 static void
drop_gil(PyInterpreterState * interp,PyThreadState * tstate,int final_release)222 drop_gil(PyInterpreterState *interp, PyThreadState *tstate, int final_release)
223 {
224     struct _ceval_state *ceval = &interp->ceval;
225     /* If final_release is true, the caller is indicating that we're releasing
226        the GIL for the last time in this thread.  This is particularly
227        relevant when the current thread state is finalizing or its
228        interpreter is finalizing (either may be in an inconsistent
229        state).  In that case the current thread will definitely
230        never try to acquire the GIL again. */
231     // XXX It may be more correct to check tstate->_status.finalizing.
232     // XXX assert(final_release || !tstate->_status.cleared);
233 
234     assert(final_release || tstate != NULL);
235     struct _gil_runtime_state *gil = ceval->gil;
236 #ifdef Py_GIL_DISABLED
237     // Check if we have the GIL before dropping it. tstate will be NULL if
238     // take_gil() detected that this thread has been destroyed, in which case
239     // we know we have the GIL.
240     if (tstate != NULL && !tstate->_status.holds_gil) {
241         return;
242     }
243 #endif
244     if (!_Py_atomic_load_int_relaxed(&gil->locked)) {
245         Py_FatalError("drop_gil: GIL is not locked");
246     }
247 
248     if (!final_release) {
249         /* Sub-interpreter support: threads might have been switched
250            under our feet using PyThreadState_Swap(). Fix the GIL last
251            holder variable so that our heuristics work. */
252         _Py_atomic_store_ptr_relaxed(&gil->last_holder, tstate);
253     }
254 
255     drop_gil_impl(tstate, gil);
256 
257 #ifdef FORCE_SWITCHING
258     /* We might be releasing the GIL for the last time in this thread.  In that
259        case there's a possible race with tstate->interp getting deleted after
260        gil->mutex is unlocked and before the following code runs, leading to a
261        crash.  We can use final_release to indicate the thread is done with the
262        GIL, and that's the only time we might delete the interpreter.  See
263        https://github.com/python/cpython/issues/104341. */
264     if (!final_release &&
265         _Py_eval_breaker_bit_is_set(tstate, _PY_GIL_DROP_REQUEST_BIT)) {
266         MUTEX_LOCK(gil->switch_mutex);
267         /* Not switched yet => wait */
268         if (((PyThreadState*)_Py_atomic_load_ptr_relaxed(&gil->last_holder)) == tstate)
269         {
270             assert(_PyThreadState_CheckConsistency(tstate));
271             _Py_unset_eval_breaker_bit(tstate, _PY_GIL_DROP_REQUEST_BIT);
272             /* NOTE: if COND_WAIT does not atomically start waiting when
273                releasing the mutex, another thread can run through, take
274                the GIL and drop it again, and reset the condition
275                before we even had a chance to wait for it. */
276             COND_WAIT(gil->switch_cond, gil->switch_mutex);
277         }
278         MUTEX_UNLOCK(gil->switch_mutex);
279     }
280 #endif
281 }
282 
283 
284 /* Take the GIL.
285 
286    The function saves errno at entry and restores its value at exit.
287 
288    tstate must be non-NULL.
289 
290    Returns 1 if the GIL was acquired, or 0 if not. */
291 static void
take_gil(PyThreadState * tstate)292 take_gil(PyThreadState *tstate)
293 {
294     int err = errno;
295 
296     assert(tstate != NULL);
297     /* We shouldn't be using a thread state that isn't viable any more. */
298     // XXX It may be more correct to check tstate->_status.finalizing.
299     // XXX assert(!tstate->_status.cleared);
300 
301     if (_PyThreadState_MustExit(tstate)) {
302         /* bpo-39877: If Py_Finalize() has been called and tstate is not the
303            thread which called Py_Finalize(), exit immediately the thread.
304 
305            This code path can be reached by a daemon thread after Py_Finalize()
306            completes. In this case, tstate is a dangling pointer: points to
307            PyThreadState freed memory. */
308         PyThread_exit_thread();
309     }
310 
311     assert(_PyThreadState_CheckConsistency(tstate));
312     PyInterpreterState *interp = tstate->interp;
313     struct _gil_runtime_state *gil = interp->ceval.gil;
314 #ifdef Py_GIL_DISABLED
315     if (!_Py_atomic_load_int_relaxed(&gil->enabled)) {
316         return;
317     }
318 #endif
319 
320     /* Check that _PyEval_InitThreads() was called to create the lock */
321     assert(gil_created(gil));
322 
323     MUTEX_LOCK(gil->mutex);
324 
325     int drop_requested = 0;
326     while (_Py_atomic_load_int_relaxed(&gil->locked)) {
327         unsigned long saved_switchnum = gil->switch_number;
328 
329         unsigned long interval = (gil->interval >= 1 ? gil->interval : 1);
330         int timed_out = 0;
331         COND_TIMED_WAIT(gil->cond, gil->mutex, interval, timed_out);
332 
333         /* If we timed out and no switch occurred in the meantime, it is time
334            to ask the GIL-holding thread to drop it. */
335         if (timed_out &&
336             _Py_atomic_load_int_relaxed(&gil->locked) &&
337             gil->switch_number == saved_switchnum)
338         {
339             PyThreadState *holder_tstate =
340                 (PyThreadState*)_Py_atomic_load_ptr_relaxed(&gil->last_holder);
341             if (_PyThreadState_MustExit(tstate)) {
342                 MUTEX_UNLOCK(gil->mutex);
343                 // gh-96387: If the loop requested a drop request in a previous
344                 // iteration, reset the request. Otherwise, drop_gil() can
345                 // block forever waiting for the thread which exited. Drop
346                 // requests made by other threads are also reset: these threads
347                 // may have to request again a drop request (iterate one more
348                 // time).
349                 if (drop_requested) {
350                     _Py_unset_eval_breaker_bit(holder_tstate, _PY_GIL_DROP_REQUEST_BIT);
351                 }
352                 PyThread_exit_thread();
353             }
354             assert(_PyThreadState_CheckConsistency(tstate));
355 
356             _Py_set_eval_breaker_bit(holder_tstate, _PY_GIL_DROP_REQUEST_BIT);
357             drop_requested = 1;
358         }
359     }
360 
361 #ifdef Py_GIL_DISABLED
362     if (!_Py_atomic_load_int_relaxed(&gil->enabled)) {
363         // Another thread disabled the GIL between our check above and
364         // now. Don't take the GIL, signal any other waiting threads, and
365         // return.
366         COND_SIGNAL(gil->cond);
367         MUTEX_UNLOCK(gil->mutex);
368         return;
369     }
370 #endif
371 
372 #ifdef FORCE_SWITCHING
373     /* This mutex must be taken before modifying gil->last_holder:
374        see drop_gil(). */
375     MUTEX_LOCK(gil->switch_mutex);
376 #endif
377     /* We now hold the GIL */
378     _Py_atomic_store_int_relaxed(&gil->locked, 1);
379     _Py_ANNOTATE_RWLOCK_ACQUIRED(&gil->locked, /*is_write=*/1);
380 
381     if (tstate != (PyThreadState*)_Py_atomic_load_ptr_relaxed(&gil->last_holder)) {
382         _Py_atomic_store_ptr_relaxed(&gil->last_holder, tstate);
383         ++gil->switch_number;
384     }
385 
386 #ifdef FORCE_SWITCHING
387     COND_SIGNAL(gil->switch_cond);
388     MUTEX_UNLOCK(gil->switch_mutex);
389 #endif
390 
391     if (_PyThreadState_MustExit(tstate)) {
392         /* bpo-36475: If Py_Finalize() has been called and tstate is not
393            the thread which called Py_Finalize(), exit immediately the
394            thread.
395 
396            This code path can be reached by a daemon thread which was waiting
397            in take_gil() while the main thread called
398            wait_for_thread_shutdown() from Py_Finalize(). */
399         MUTEX_UNLOCK(gil->mutex);
400         /* tstate could be a dangling pointer, so don't pass it to
401            drop_gil(). */
402         drop_gil(interp, NULL, 1);
403         PyThread_exit_thread();
404     }
405     assert(_PyThreadState_CheckConsistency(tstate));
406 
407     tstate->_status.holds_gil = 1;
408     _Py_unset_eval_breaker_bit(tstate, _PY_GIL_DROP_REQUEST_BIT);
409     update_eval_breaker_for_thread(interp, tstate);
410 
411     MUTEX_UNLOCK(gil->mutex);
412 
413     errno = err;
414     return;
415 }
416 
_PyEval_SetSwitchInterval(unsigned long microseconds)417 void _PyEval_SetSwitchInterval(unsigned long microseconds)
418 {
419     PyInterpreterState *interp = _PyInterpreterState_GET();
420     struct _gil_runtime_state *gil = interp->ceval.gil;
421     assert(gil != NULL);
422     gil->interval = microseconds;
423 }
424 
_PyEval_GetSwitchInterval(void)425 unsigned long _PyEval_GetSwitchInterval(void)
426 {
427     PyInterpreterState *interp = _PyInterpreterState_GET();
428     struct _gil_runtime_state *gil = interp->ceval.gil;
429     assert(gil != NULL);
430     return gil->interval;
431 }
432 
433 
434 int
_PyEval_ThreadsInitialized(void)435 _PyEval_ThreadsInitialized(void)
436 {
437     /* XXX This is only needed for an assert in PyGILState_Ensure(),
438      * which currently does not work with subinterpreters.
439      * Thus we only use the main interpreter. */
440     PyInterpreterState *interp = _PyInterpreterState_Main();
441     if (interp == NULL) {
442         return 0;
443     }
444     struct _gil_runtime_state *gil = interp->ceval.gil;
445     return gil_created(gil);
446 }
447 
448 // Function removed in the Python 3.13 API but kept in the stable ABI.
449 PyAPI_FUNC(int)
PyEval_ThreadsInitialized(void)450 PyEval_ThreadsInitialized(void)
451 {
452     return _PyEval_ThreadsInitialized();
453 }
454 
455 #ifndef NDEBUG
456 static inline int
current_thread_holds_gil(struct _gil_runtime_state * gil,PyThreadState * tstate)457 current_thread_holds_gil(struct _gil_runtime_state *gil, PyThreadState *tstate)
458 {
459     int holds_gil = tstate->_status.holds_gil;
460 
461     // holds_gil is the source of truth; check that last_holder and gil->locked
462     // are consistent with it.
463     int locked = _Py_atomic_load_int_relaxed(&gil->locked);
464     int is_last_holder =
465         ((PyThreadState*)_Py_atomic_load_ptr_relaxed(&gil->last_holder)) == tstate;
466     assert(!holds_gil || locked);
467     assert(!holds_gil || is_last_holder);
468 
469     return holds_gil;
470 }
471 #endif
472 
473 static void
init_shared_gil(PyInterpreterState * interp,struct _gil_runtime_state * gil)474 init_shared_gil(PyInterpreterState *interp, struct _gil_runtime_state *gil)
475 {
476     assert(gil_created(gil));
477     interp->ceval.gil = gil;
478     interp->ceval.own_gil = 0;
479 }
480 
481 static void
init_own_gil(PyInterpreterState * interp,struct _gil_runtime_state * gil)482 init_own_gil(PyInterpreterState *interp, struct _gil_runtime_state *gil)
483 {
484     assert(!gil_created(gil));
485 #ifdef Py_GIL_DISABLED
486     const PyConfig *config = _PyInterpreterState_GetConfig(interp);
487     gil->enabled = config->enable_gil == _PyConfig_GIL_ENABLE ? INT_MAX : 0;
488 #endif
489     create_gil(gil);
490     assert(gil_created(gil));
491     interp->ceval.gil = gil;
492     interp->ceval.own_gil = 1;
493 }
494 
495 void
_PyEval_InitGIL(PyThreadState * tstate,int own_gil)496 _PyEval_InitGIL(PyThreadState *tstate, int own_gil)
497 {
498     assert(tstate->interp->ceval.gil == NULL);
499     if (!own_gil) {
500         /* The interpreter will share the main interpreter's instead. */
501         PyInterpreterState *main_interp = _PyInterpreterState_Main();
502         assert(tstate->interp != main_interp);
503         struct _gil_runtime_state *gil = main_interp->ceval.gil;
504         init_shared_gil(tstate->interp, gil);
505         assert(!current_thread_holds_gil(gil, tstate));
506     }
507     else {
508         PyThread_init_thread();
509         init_own_gil(tstate->interp, &tstate->interp->_gil);
510     }
511 
512     // Lock the GIL and mark the current thread as attached.
513     _PyThreadState_Attach(tstate);
514 }
515 
516 void
_PyEval_FiniGIL(PyInterpreterState * interp)517 _PyEval_FiniGIL(PyInterpreterState *interp)
518 {
519     struct _gil_runtime_state *gil = interp->ceval.gil;
520     if (gil == NULL) {
521         /* It was already finalized (or hasn't been initialized yet). */
522         assert(!interp->ceval.own_gil);
523         return;
524     }
525     else if (!interp->ceval.own_gil) {
526 #ifdef Py_DEBUG
527         PyInterpreterState *main_interp = _PyInterpreterState_Main();
528         assert(main_interp != NULL && interp != main_interp);
529         assert(interp->ceval.gil == main_interp->ceval.gil);
530 #endif
531         interp->ceval.gil = NULL;
532         return;
533     }
534 
535     if (!gil_created(gil)) {
536         /* First Py_InitializeFromConfig() call: the GIL doesn't exist
537            yet: do nothing. */
538         return;
539     }
540 
541     destroy_gil(gil);
542     assert(!gil_created(gil));
543     interp->ceval.gil = NULL;
544 }
545 
546 void
PyEval_InitThreads(void)547 PyEval_InitThreads(void)
548 {
549     /* Do nothing: kept for backward compatibility */
550 }
551 
552 void
_PyEval_Fini(void)553 _PyEval_Fini(void)
554 {
555 #ifdef Py_STATS
556     _Py_PrintSpecializationStats(1);
557 #endif
558 }
559 
560 // Function removed in the Python 3.13 API but kept in the stable ABI.
561 PyAPI_FUNC(void)
PyEval_AcquireLock(void)562 PyEval_AcquireLock(void)
563 {
564     PyThreadState *tstate = _PyThreadState_GET();
565     _Py_EnsureTstateNotNULL(tstate);
566 
567     take_gil(tstate);
568 }
569 
570 // Function removed in the Python 3.13 API but kept in the stable ABI.
571 PyAPI_FUNC(void)
PyEval_ReleaseLock(void)572 PyEval_ReleaseLock(void)
573 {
574     PyThreadState *tstate = _PyThreadState_GET();
575     /* This function must succeed when the current thread state is NULL.
576        We therefore avoid PyThreadState_Get() which dumps a fatal error
577        in debug mode. */
578     drop_gil(tstate->interp, tstate, 0);
579 }
580 
581 void
_PyEval_AcquireLock(PyThreadState * tstate)582 _PyEval_AcquireLock(PyThreadState *tstate)
583 {
584     _Py_EnsureTstateNotNULL(tstate);
585     take_gil(tstate);
586 }
587 
588 void
_PyEval_ReleaseLock(PyInterpreterState * interp,PyThreadState * tstate,int final_release)589 _PyEval_ReleaseLock(PyInterpreterState *interp,
590                     PyThreadState *tstate,
591                     int final_release)
592 {
593     assert(tstate != NULL);
594     assert(tstate->interp == interp);
595     drop_gil(interp, tstate, final_release);
596 }
597 
598 void
PyEval_AcquireThread(PyThreadState * tstate)599 PyEval_AcquireThread(PyThreadState *tstate)
600 {
601     _Py_EnsureTstateNotNULL(tstate);
602     _PyThreadState_Attach(tstate);
603 }
604 
605 void
PyEval_ReleaseThread(PyThreadState * tstate)606 PyEval_ReleaseThread(PyThreadState *tstate)
607 {
608     assert(_PyThreadState_CheckConsistency(tstate));
609     _PyThreadState_Detach(tstate);
610 }
611 
612 #ifdef HAVE_FORK
613 /* This function is called from PyOS_AfterFork_Child to re-initialize the
614    GIL and pending calls lock. */
615 PyStatus
_PyEval_ReInitThreads(PyThreadState * tstate)616 _PyEval_ReInitThreads(PyThreadState *tstate)
617 {
618     assert(tstate->interp == _PyInterpreterState_Main());
619 
620     struct _gil_runtime_state *gil = tstate->interp->ceval.gil;
621     if (!gil_created(gil)) {
622         return _PyStatus_OK();
623     }
624     recreate_gil(gil);
625 
626     take_gil(tstate);
627 
628     struct _pending_calls *pending = &tstate->interp->ceval.pending;
629     _PyMutex_at_fork_reinit(&pending->mutex);
630 
631     return _PyStatus_OK();
632 }
633 #endif
634 
635 PyThreadState *
PyEval_SaveThread(void)636 PyEval_SaveThread(void)
637 {
638     PyThreadState *tstate = _PyThreadState_GET();
639     _PyThreadState_Detach(tstate);
640     return tstate;
641 }
642 
643 void
PyEval_RestoreThread(PyThreadState * tstate)644 PyEval_RestoreThread(PyThreadState *tstate)
645 {
646 #ifdef MS_WINDOWS
647     int err = GetLastError();
648 #endif
649 
650     _Py_EnsureTstateNotNULL(tstate);
651     _PyThreadState_Attach(tstate);
652 
653 #ifdef MS_WINDOWS
654     SetLastError(err);
655 #endif
656 }
657 
658 
659 void
_PyEval_SignalReceived(void)660 _PyEval_SignalReceived(void)
661 {
662     _Py_set_eval_breaker_bit(_PyRuntime.main_tstate, _PY_SIGNALS_PENDING_BIT);
663 }
664 
665 
666 #ifndef Py_GIL_DISABLED
667 static void
signal_active_thread(PyInterpreterState * interp,uintptr_t bit)668 signal_active_thread(PyInterpreterState *interp, uintptr_t bit)
669 {
670     struct _gil_runtime_state *gil = interp->ceval.gil;
671 
672     // If a thread from the targeted interpreter is holding the GIL, signal
673     // that thread. Otherwise, the next thread to run from the targeted
674     // interpreter will have its bit set as part of taking the GIL.
675     MUTEX_LOCK(gil->mutex);
676     if (_Py_atomic_load_int_relaxed(&gil->locked)) {
677         PyThreadState *holder = (PyThreadState*)_Py_atomic_load_ptr_relaxed(&gil->last_holder);
678         if (holder->interp == interp) {
679             _Py_set_eval_breaker_bit(holder, bit);
680         }
681     }
682     MUTEX_UNLOCK(gil->mutex);
683 }
684 #endif
685 
686 
687 /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
688    signal handlers or Mac I/O completion routines) can schedule calls
689    to a function to be called synchronously.
690    The synchronous function is called with one void* argument.
691    It should return 0 for success or -1 for failure -- failure should
692    be accompanied by an exception.
693 
694    If registry succeeds, the registry function returns 0; if it fails
695    (e.g. due to too many pending calls) it returns -1 (without setting
696    an exception condition).
697 
698    Note that because registry may occur from within signal handlers,
699    or other asynchronous events, calling malloc() is unsafe!
700 
701    Any thread can schedule pending calls, but only the main thread
702    will execute them.
703    There is no facility to schedule calls to a particular thread, but
704    that should be easy to change, should that ever be required.  In
705    that case, the static variables here should go into the python
706    threadstate.
707 */
708 
709 /* Push one item onto the queue while holding the lock. */
710 static int
_push_pending_call(struct _pending_calls * pending,_Py_pending_call_func func,void * arg,int flags)711 _push_pending_call(struct _pending_calls *pending,
712                    _Py_pending_call_func func, void *arg, int flags)
713 {
714     if (pending->npending == pending->max) {
715         return _Py_ADD_PENDING_FULL;
716     }
717     assert(pending->npending < pending->max);
718 
719     int i = pending->next;
720     assert(pending->calls[i].func == NULL);
721 
722     pending->calls[i].func = func;
723     pending->calls[i].arg = arg;
724     pending->calls[i].flags = flags;
725 
726     assert(pending->npending < PENDINGCALLSARRAYSIZE);
727     _Py_atomic_add_int32(&pending->npending, 1);
728 
729     pending->next = (i + 1) % PENDINGCALLSARRAYSIZE;
730     assert(pending->next != pending->first
731             || pending->npending == pending->max);
732 
733     return _Py_ADD_PENDING_SUCCESS;
734 }
735 
736 static int
_next_pending_call(struct _pending_calls * pending,int (** func)(void *),void ** arg,int * flags)737 _next_pending_call(struct _pending_calls *pending,
738                    int (**func)(void *), void **arg, int *flags)
739 {
740     int i = pending->first;
741     if (pending->npending == 0) {
742         /* Queue empty */
743         assert(i == pending->next);
744         assert(pending->calls[i].func == NULL);
745         return -1;
746     }
747     *func = pending->calls[i].func;
748     *arg = pending->calls[i].arg;
749     *flags = pending->calls[i].flags;
750     return i;
751 }
752 
753 /* Pop one item off the queue while holding the lock. */
754 static void
_pop_pending_call(struct _pending_calls * pending,int (** func)(void *),void ** arg,int * flags)755 _pop_pending_call(struct _pending_calls *pending,
756                   int (**func)(void *), void **arg, int *flags)
757 {
758     int i = _next_pending_call(pending, func, arg, flags);
759     if (i >= 0) {
760         pending->calls[i] = (struct _pending_call){0};
761         pending->first = (i + 1) % PENDINGCALLSARRAYSIZE;
762         assert(pending->npending > 0);
763         _Py_atomic_add_int32(&pending->npending, -1);
764     }
765 }
766 
767 /* This implementation is thread-safe.  It allows
768    scheduling to be made from any thread, and even from an executing
769    callback.
770  */
771 
772 _Py_add_pending_call_result
_PyEval_AddPendingCall(PyInterpreterState * interp,_Py_pending_call_func func,void * arg,int flags)773 _PyEval_AddPendingCall(PyInterpreterState *interp,
774                        _Py_pending_call_func func, void *arg, int flags)
775 {
776     struct _pending_calls *pending = &interp->ceval.pending;
777     int main_only = (flags & _Py_PENDING_MAINTHREADONLY) != 0;
778     if (main_only) {
779         /* The main thread only exists in the main interpreter. */
780         assert(_Py_IsMainInterpreter(interp));
781         pending = &_PyRuntime.ceval.pending_mainthread;
782     }
783 
784     PyMutex_Lock(&pending->mutex);
785     _Py_add_pending_call_result result =
786         _push_pending_call(pending, func, arg, flags);
787     PyMutex_Unlock(&pending->mutex);
788 
789     if (main_only) {
790         _Py_set_eval_breaker_bit(_PyRuntime.main_tstate, _PY_CALLS_TO_DO_BIT);
791     }
792     else {
793 #ifdef Py_GIL_DISABLED
794         _Py_set_eval_breaker_bit_all(interp, _PY_CALLS_TO_DO_BIT);
795 #else
796         signal_active_thread(interp, _PY_CALLS_TO_DO_BIT);
797 #endif
798     }
799 
800     return result;
801 }
802 
803 int
Py_AddPendingCall(_Py_pending_call_func func,void * arg)804 Py_AddPendingCall(_Py_pending_call_func func, void *arg)
805 {
806     /* Legacy users of this API will continue to target the main thread
807        (of the main interpreter). */
808     PyInterpreterState *interp = _PyInterpreterState_Main();
809     _Py_add_pending_call_result r =
810         _PyEval_AddPendingCall(interp, func, arg, _Py_PENDING_MAINTHREADONLY);
811     if (r == _Py_ADD_PENDING_FULL) {
812         return -1;
813     }
814     else {
815         assert(r == _Py_ADD_PENDING_SUCCESS);
816         return 0;
817     }
818 }
819 
820 static int
handle_signals(PyThreadState * tstate)821 handle_signals(PyThreadState *tstate)
822 {
823     assert(_PyThreadState_CheckConsistency(tstate));
824     _Py_unset_eval_breaker_bit(tstate, _PY_SIGNALS_PENDING_BIT);
825     if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
826         return 0;
827     }
828     if (_PyErr_CheckSignalsTstate(tstate) < 0) {
829         /* On failure, re-schedule a call to handle_signals(). */
830         _Py_set_eval_breaker_bit(tstate, _PY_SIGNALS_PENDING_BIT);
831         return -1;
832     }
833     return 0;
834 }
835 
836 static int
_make_pending_calls(struct _pending_calls * pending,int32_t * p_npending)837 _make_pending_calls(struct _pending_calls *pending, int32_t *p_npending)
838 {
839     int res = 0;
840     int32_t npending = -1;
841 
842     assert(sizeof(pending->max) <= sizeof(size_t)
843             && ((size_t)pending->max) <= Py_ARRAY_LENGTH(pending->calls));
844     int32_t maxloop = pending->maxloop;
845     if (maxloop == 0) {
846         maxloop = pending->max;
847     }
848     assert(maxloop > 0 && maxloop <= pending->max);
849 
850     /* perform a bounded number of calls, in case of recursion */
851     for (int i=0; i<maxloop; i++) {
852         _Py_pending_call_func func = NULL;
853         void *arg = NULL;
854         int flags = 0;
855 
856         /* pop one item off the queue while holding the lock */
857         PyMutex_Lock(&pending->mutex);
858         _pop_pending_call(pending, &func, &arg, &flags);
859         npending = pending->npending;
860         PyMutex_Unlock(&pending->mutex);
861 
862         /* Check if there are any more pending calls. */
863         if (func == NULL) {
864             assert(npending == 0);
865             break;
866         }
867 
868         /* having released the lock, perform the callback */
869         res = func(arg);
870         if ((flags & _Py_PENDING_RAWFREE) && arg != NULL) {
871             PyMem_RawFree(arg);
872         }
873         if (res != 0) {
874             res = -1;
875             goto finally;
876         }
877     }
878 
879 finally:
880     *p_npending = npending;
881     return res;
882 }
883 
884 static void
signal_pending_calls(PyThreadState * tstate,PyInterpreterState * interp)885 signal_pending_calls(PyThreadState *tstate, PyInterpreterState *interp)
886 {
887 #ifdef Py_GIL_DISABLED
888     _Py_set_eval_breaker_bit_all(interp, _PY_CALLS_TO_DO_BIT);
889 #else
890     _Py_set_eval_breaker_bit(tstate, _PY_CALLS_TO_DO_BIT);
891 #endif
892 }
893 
894 static void
unsignal_pending_calls(PyThreadState * tstate,PyInterpreterState * interp)895 unsignal_pending_calls(PyThreadState *tstate, PyInterpreterState *interp)
896 {
897 #ifdef Py_GIL_DISABLED
898     _Py_unset_eval_breaker_bit_all(interp, _PY_CALLS_TO_DO_BIT);
899 #else
900     _Py_unset_eval_breaker_bit(tstate, _PY_CALLS_TO_DO_BIT);
901 #endif
902 }
903 
904 static void
clear_pending_handling_thread(struct _pending_calls * pending)905 clear_pending_handling_thread(struct _pending_calls *pending)
906 {
907 #ifdef Py_GIL_DISABLED
908     PyMutex_Lock(&pending->mutex);
909     pending->handling_thread = NULL;
910     PyMutex_Unlock(&pending->mutex);
911 #else
912     pending->handling_thread = NULL;
913 #endif
914 }
915 
916 static int
make_pending_calls(PyThreadState * tstate)917 make_pending_calls(PyThreadState *tstate)
918 {
919     PyInterpreterState *interp = tstate->interp;
920     struct _pending_calls *pending = &interp->ceval.pending;
921     struct _pending_calls *pending_main = &_PyRuntime.ceval.pending_mainthread;
922 
923     /* Only one thread (per interpreter) may run the pending calls
924        at once.  In the same way, we don't do recursive pending calls. */
925     PyMutex_Lock(&pending->mutex);
926     if (pending->handling_thread != NULL) {
927         /* A pending call was added after another thread was already
928            handling the pending calls (and had already "unsignaled").
929            Once that thread is done, it may have taken care of all the
930            pending calls, or there might be some still waiting.
931            To avoid all threads constantly stopping on the eval breaker,
932            we clear the bit for this thread and make sure it is set
933            for the thread currently handling the pending call. */
934         _Py_set_eval_breaker_bit(pending->handling_thread, _PY_CALLS_TO_DO_BIT);
935         _Py_unset_eval_breaker_bit(tstate, _PY_CALLS_TO_DO_BIT);
936         PyMutex_Unlock(&pending->mutex);
937         return 0;
938     }
939     pending->handling_thread = tstate;
940     PyMutex_Unlock(&pending->mutex);
941 
942     /* unsignal before starting to call callbacks, so that any callback
943        added in-between re-signals */
944     unsignal_pending_calls(tstate, interp);
945 
946     int32_t npending;
947     if (_make_pending_calls(pending, &npending) != 0) {
948         clear_pending_handling_thread(pending);
949         /* There might not be more calls to make, but we play it safe. */
950         signal_pending_calls(tstate, interp);
951         return -1;
952     }
953     if (npending > 0) {
954         /* We hit pending->maxloop. */
955         signal_pending_calls(tstate, interp);
956     }
957 
958     if (_Py_IsMainThread() && _Py_IsMainInterpreter(interp)) {
959         if (_make_pending_calls(pending_main, &npending) != 0) {
960             clear_pending_handling_thread(pending);
961             /* There might not be more calls to make, but we play it safe. */
962             signal_pending_calls(tstate, interp);
963             return -1;
964         }
965         if (npending > 0) {
966             /* We hit pending_main->maxloop. */
967             signal_pending_calls(tstate, interp);
968         }
969     }
970 
971     clear_pending_handling_thread(pending);
972     return 0;
973 }
974 
975 
976 void
_Py_set_eval_breaker_bit_all(PyInterpreterState * interp,uintptr_t bit)977 _Py_set_eval_breaker_bit_all(PyInterpreterState *interp, uintptr_t bit)
978 {
979     _PyRuntimeState *runtime = &_PyRuntime;
980 
981     HEAD_LOCK(runtime);
982     for (PyThreadState *tstate = interp->threads.head; tstate != NULL; tstate = tstate->next) {
983         _Py_set_eval_breaker_bit(tstate, bit);
984     }
985     HEAD_UNLOCK(runtime);
986 }
987 
988 void
_Py_unset_eval_breaker_bit_all(PyInterpreterState * interp,uintptr_t bit)989 _Py_unset_eval_breaker_bit_all(PyInterpreterState *interp, uintptr_t bit)
990 {
991     _PyRuntimeState *runtime = &_PyRuntime;
992 
993     HEAD_LOCK(runtime);
994     for (PyThreadState *tstate = interp->threads.head; tstate != NULL; tstate = tstate->next) {
995         _Py_unset_eval_breaker_bit(tstate, bit);
996     }
997     HEAD_UNLOCK(runtime);
998 }
999 
1000 void
_Py_FinishPendingCalls(PyThreadState * tstate)1001 _Py_FinishPendingCalls(PyThreadState *tstate)
1002 {
1003     assert(PyGILState_Check());
1004     assert(_PyThreadState_CheckConsistency(tstate));
1005 
1006     struct _pending_calls *pending = &tstate->interp->ceval.pending;
1007     struct _pending_calls *pending_main =
1008             _Py_IsMainThread() && _Py_IsMainInterpreter(tstate->interp)
1009             ? &_PyRuntime.ceval.pending_mainthread
1010             : NULL;
1011     /* make_pending_calls() may return early without making all pending
1012        calls, so we keep trying until we're actually done. */
1013     int32_t npending;
1014 #ifndef NDEBUG
1015     int32_t npending_prev = INT32_MAX;
1016 #endif
1017     do {
1018         if (make_pending_calls(tstate) < 0) {
1019             PyObject *exc = _PyErr_GetRaisedException(tstate);
1020             PyErr_BadInternalCall();
1021             _PyErr_ChainExceptions1(exc);
1022             _PyErr_Print(tstate);
1023         }
1024 
1025         npending = _Py_atomic_load_int32_relaxed(&pending->npending);
1026         if (pending_main != NULL) {
1027             npending += _Py_atomic_load_int32_relaxed(&pending_main->npending);
1028         }
1029 #ifndef NDEBUG
1030         assert(npending_prev > npending);
1031         npending_prev = npending;
1032 #endif
1033     } while (npending > 0);
1034 }
1035 
1036 int
_PyEval_MakePendingCalls(PyThreadState * tstate)1037 _PyEval_MakePendingCalls(PyThreadState *tstate)
1038 {
1039     int res;
1040 
1041     if (_Py_IsMainThread() && _Py_IsMainInterpreter(tstate->interp)) {
1042         /* Python signal handler doesn't really queue a callback:
1043            it only signals that a signal was received,
1044            see _PyEval_SignalReceived(). */
1045         res = handle_signals(tstate);
1046         if (res != 0) {
1047             return res;
1048         }
1049     }
1050 
1051     res = make_pending_calls(tstate);
1052     if (res != 0) {
1053         return res;
1054     }
1055 
1056     return 0;
1057 }
1058 
1059 /* Py_MakePendingCalls() is a simple wrapper for the sake
1060    of backward-compatibility. */
1061 int
Py_MakePendingCalls(void)1062 Py_MakePendingCalls(void)
1063 {
1064     assert(PyGILState_Check());
1065 
1066     PyThreadState *tstate = _PyThreadState_GET();
1067     assert(_PyThreadState_CheckConsistency(tstate));
1068 
1069     /* Only execute pending calls on the main thread. */
1070     if (!_Py_IsMainThread() || !_Py_IsMainInterpreter(tstate->interp)) {
1071         return 0;
1072     }
1073     return _PyEval_MakePendingCalls(tstate);
1074 }
1075 
1076 void
_PyEval_InitState(PyInterpreterState * interp)1077 _PyEval_InitState(PyInterpreterState *interp)
1078 {
1079     _gil_initialize(&interp->_gil);
1080 }
1081 
1082 #ifdef Py_GIL_DISABLED
1083 int
_PyEval_EnableGILTransient(PyThreadState * tstate)1084 _PyEval_EnableGILTransient(PyThreadState *tstate)
1085 {
1086     const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
1087     if (config->enable_gil != _PyConfig_GIL_DEFAULT) {
1088         return 0;
1089     }
1090     struct _gil_runtime_state *gil = tstate->interp->ceval.gil;
1091 
1092     int enabled = _Py_atomic_load_int_relaxed(&gil->enabled);
1093     if (enabled == INT_MAX) {
1094         // The GIL is already enabled permanently.
1095         return 0;
1096     }
1097     if (enabled == INT_MAX - 1) {
1098         Py_FatalError("Too many transient requests to enable the GIL");
1099     }
1100     if (enabled > 0) {
1101         // If enabled is nonzero, we know we hold the GIL. This means that no
1102         // other threads are attached, and nobody else can be concurrently
1103         // mutating it.
1104         _Py_atomic_store_int_relaxed(&gil->enabled, enabled + 1);
1105         return 0;
1106     }
1107 
1108     // Enabling the GIL changes what it means to be an "attached" thread. To
1109     // safely make this transition, we:
1110     // 1. Detach the current thread.
1111     // 2. Stop the world to detach (and suspend) all other threads.
1112     // 3. Enable the GIL, if nobody else did between our check above and when
1113     //    our stop-the-world begins.
1114     // 4. Start the world.
1115     // 5. Attach the current thread. Other threads may attach and hold the GIL
1116     //    before this thread, which is harmless.
1117     _PyThreadState_Detach(tstate);
1118 
1119     // This could be an interpreter-local stop-the-world in situations where we
1120     // know that this interpreter's GIL is not shared, and that it won't become
1121     // shared before the stop-the-world begins. For now, we always stop all
1122     // interpreters for simplicity.
1123     _PyEval_StopTheWorldAll(&_PyRuntime);
1124 
1125     enabled = _Py_atomic_load_int_relaxed(&gil->enabled);
1126     int this_thread_enabled = enabled == 0;
1127     _Py_atomic_store_int_relaxed(&gil->enabled, enabled + 1);
1128 
1129     _PyEval_StartTheWorldAll(&_PyRuntime);
1130     _PyThreadState_Attach(tstate);
1131 
1132     return this_thread_enabled;
1133 }
1134 
1135 int
_PyEval_EnableGILPermanent(PyThreadState * tstate)1136 _PyEval_EnableGILPermanent(PyThreadState *tstate)
1137 {
1138     const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
1139     if (config->enable_gil != _PyConfig_GIL_DEFAULT) {
1140         return 0;
1141     }
1142 
1143     struct _gil_runtime_state *gil = tstate->interp->ceval.gil;
1144     assert(current_thread_holds_gil(gil, tstate));
1145 
1146     int enabled = _Py_atomic_load_int_relaxed(&gil->enabled);
1147     if (enabled == INT_MAX) {
1148         return 0;
1149     }
1150 
1151     _Py_atomic_store_int_relaxed(&gil->enabled, INT_MAX);
1152     return 1;
1153 }
1154 
1155 int
_PyEval_DisableGIL(PyThreadState * tstate)1156 _PyEval_DisableGIL(PyThreadState *tstate)
1157 {
1158     const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
1159     if (config->enable_gil != _PyConfig_GIL_DEFAULT) {
1160         return 0;
1161     }
1162 
1163     struct _gil_runtime_state *gil = tstate->interp->ceval.gil;
1164     assert(current_thread_holds_gil(gil, tstate));
1165 
1166     int enabled = _Py_atomic_load_int_relaxed(&gil->enabled);
1167     if (enabled == INT_MAX) {
1168         return 0;
1169     }
1170 
1171     assert(enabled >= 1);
1172     enabled--;
1173 
1174     // Disabling the GIL is much simpler than enabling it, since we know we are
1175     // the only attached thread. Other threads may start free-threading as soon
1176     // as this store is complete, if it sets gil->enabled to 0.
1177     _Py_atomic_store_int_relaxed(&gil->enabled, enabled);
1178 
1179     if (enabled == 0) {
1180         // We're attached, so we know the GIL will remain disabled until at
1181         // least the next time we detach, which must be after this function
1182         // returns.
1183         //
1184         // Drop the GIL, which will wake up any threads waiting in take_gil()
1185         // and let them resume execution without the GIL.
1186         drop_gil_impl(tstate, gil);
1187 
1188         // If another thread asked us to drop the GIL, they should be
1189         // free-threading by now. Remove any such request so we have a clean
1190         // slate if/when the GIL is enabled again.
1191         _Py_unset_eval_breaker_bit(tstate, _PY_GIL_DROP_REQUEST_BIT);
1192         return 1;
1193     }
1194     return 0;
1195 }
1196 #endif
1197 
1198 
1199 /* Do periodic things, like check for signals and async I/0.
1200 * We need to do reasonably frequently, but not too frequently.
1201 * All loops should include a check of the eval breaker.
1202 * We also check on return from any builtin function.
1203 *
1204 * ## More Details ###
1205 *
1206 * The eval loop (this function) normally executes the instructions
1207 * of a code object sequentially.  However, the runtime supports a
1208 * number of out-of-band execution scenarios that may pause that
1209 * sequential execution long enough to do that out-of-band work
1210 * in the current thread using the current PyThreadState.
1211 *
1212 * The scenarios include:
1213 *
1214 *  - cyclic garbage collection
1215 *  - GIL drop requests
1216 *  - "async" exceptions
1217 *  - "pending calls"  (some only in the main thread)
1218 *  - signal handling (only in the main thread)
1219 *
1220 * When the need for one of the above is detected, the eval loop
1221 * pauses long enough to handle the detected case.  Then, if doing
1222 * so didn't trigger an exception, the eval loop resumes executing
1223 * the sequential instructions.
1224 *
1225 * To make this work, the eval loop periodically checks if any
1226 * of the above needs to happen.  The individual checks can be
1227 * expensive if computed each time, so a while back we switched
1228 * to using pre-computed, per-interpreter variables for the checks,
1229 * and later consolidated that to a single "eval breaker" variable
1230 * (now a PyInterpreterState field).
1231 *
1232 * For the longest time, the eval breaker check would happen
1233 * frequently, every 5 or so times through the loop, regardless
1234 * of what instruction ran last or what would run next.  Then, in
1235 * early 2021 (gh-18334, commit 4958f5d), we switched to checking
1236 * the eval breaker less frequently, by hard-coding the check to
1237 * specific places in the eval loop (e.g. certain instructions).
1238 * The intent then was to check after returning from calls
1239 * and on the back edges of loops.
1240 *
1241 * In addition to being more efficient, that approach keeps
1242 * the eval loop from running arbitrary code between instructions
1243 * that don't handle that well.  (See gh-74174.)
1244 *
1245 * Currently, the eval breaker check happens on back edges in
1246 * the control flow graph, which pretty much applies to all loops,
1247 * and most calls.
1248 * (See bytecodes.c for exact information.)
1249 *
1250 * One consequence of this approach is that it might not be obvious
1251 * how to force any specific thread to pick up the eval breaker,
1252 * or for any specific thread to not pick it up.  Mostly this
1253 * involves judicious uses of locks and careful ordering of code,
1254 * while avoiding code that might trigger the eval breaker
1255 * until so desired.
1256 */
1257 int
_Py_HandlePending(PyThreadState * tstate)1258 _Py_HandlePending(PyThreadState *tstate)
1259 {
1260     uintptr_t breaker = _Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker);
1261 
1262     /* Stop-the-world */
1263     if ((breaker & _PY_EVAL_PLEASE_STOP_BIT) != 0) {
1264         _Py_unset_eval_breaker_bit(tstate, _PY_EVAL_PLEASE_STOP_BIT);
1265         _PyThreadState_Suspend(tstate);
1266 
1267         /* The attach blocks until the stop-the-world event is complete. */
1268         _PyThreadState_Attach(tstate);
1269     }
1270 
1271     /* Pending signals */
1272     if ((breaker & _PY_SIGNALS_PENDING_BIT) != 0) {
1273         if (handle_signals(tstate) != 0) {
1274             return -1;
1275         }
1276     }
1277 
1278     /* Pending calls */
1279     if ((breaker & _PY_CALLS_TO_DO_BIT) != 0) {
1280         if (make_pending_calls(tstate) != 0) {
1281             return -1;
1282         }
1283     }
1284 
1285 #ifdef Py_GIL_DISABLED
1286     /* Objects with refcounts to merge */
1287     if ((breaker & _PY_EVAL_EXPLICIT_MERGE_BIT) != 0) {
1288         _Py_unset_eval_breaker_bit(tstate, _PY_EVAL_EXPLICIT_MERGE_BIT);
1289         _Py_brc_merge_refcounts(tstate);
1290     }
1291 #endif
1292 
1293     /* GC scheduled to run */
1294     if ((breaker & _PY_GC_SCHEDULED_BIT) != 0) {
1295         _Py_unset_eval_breaker_bit(tstate, _PY_GC_SCHEDULED_BIT);
1296         _Py_RunGC(tstate);
1297     }
1298 
1299     /* GIL drop request */
1300     if ((breaker & _PY_GIL_DROP_REQUEST_BIT) != 0) {
1301         /* Give another thread a chance */
1302         _PyThreadState_Detach(tstate);
1303 
1304         /* Other threads may run now */
1305 
1306         _PyThreadState_Attach(tstate);
1307     }
1308 
1309     /* Check for asynchronous exception. */
1310     if ((breaker & _PY_ASYNC_EXCEPTION_BIT) != 0) {
1311         _Py_unset_eval_breaker_bit(tstate, _PY_ASYNC_EXCEPTION_BIT);
1312         PyObject *exc = _Py_atomic_exchange_ptr(&tstate->async_exc, NULL);
1313         if (exc != NULL) {
1314             _PyErr_SetNone(tstate, exc);
1315             Py_DECREF(exc);
1316             return -1;
1317         }
1318     }
1319     return 0;
1320 }
1321