Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Implementation of the Global Interpreter Lock (GIL). |
| 3 | */ |
| 4 | |
| 5 | #include <stdlib.h> |
| 6 | #include <errno.h> |
| 7 | |
Victor Stinner | 27e2d1f | 2018-11-01 00:52:28 +0100 | [diff] [blame] | 8 | #include "pycore_atomic.h" |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 9 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 10 | |
| 11 | /* |
| 12 | Notes about the implementation: |
| 13 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 14 | - The GIL is just a boolean variable (locked) whose access is protected |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 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. |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 24 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 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. |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 34 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 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 |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 41 | the value of last_holder is changed to something else than its |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 42 | own thread state pointer, indicating that another thread was able to |
| 43 | take the GIL. |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 44 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 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 | |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 52 | #include "condvar.h" |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 53 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 54 | #define MUTEX_INIT(mut) \ |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 55 | if (PyMUTEX_INIT(&(mut))) { \ |
| 56 | Py_FatalError("PyMUTEX_INIT(" #mut ") failed"); }; |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 57 | #define MUTEX_FINI(mut) \ |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 58 | if (PyMUTEX_FINI(&(mut))) { \ |
| 59 | Py_FatalError("PyMUTEX_FINI(" #mut ") failed"); }; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 60 | #define MUTEX_LOCK(mut) \ |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 61 | if (PyMUTEX_LOCK(&(mut))) { \ |
| 62 | Py_FatalError("PyMUTEX_LOCK(" #mut ") failed"); }; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 63 | #define MUTEX_UNLOCK(mut) \ |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 64 | if (PyMUTEX_UNLOCK(&(mut))) { \ |
| 65 | Py_FatalError("PyMUTEX_UNLOCK(" #mut ") failed"); }; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 66 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 67 | #define COND_INIT(cond) \ |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 68 | if (PyCOND_INIT(&(cond))) { \ |
| 69 | Py_FatalError("PyCOND_INIT(" #cond ") failed"); }; |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 70 | #define COND_FINI(cond) \ |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 71 | if (PyCOND_FINI(&(cond))) { \ |
| 72 | Py_FatalError("PyCOND_FINI(" #cond ") failed"); }; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 73 | #define COND_SIGNAL(cond) \ |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 74 | if (PyCOND_SIGNAL(&(cond))) { \ |
| 75 | Py_FatalError("PyCOND_SIGNAL(" #cond ") failed"); }; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 76 | #define COND_WAIT(cond, mut) \ |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 77 | if (PyCOND_WAIT(&(cond), &(mut))) { \ |
| 78 | Py_FatalError("PyCOND_WAIT(" #cond ") failed"); }; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 79 | #define COND_TIMED_WAIT(cond, mut, microseconds, timeout_result) \ |
| 80 | { \ |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 81 | int r = PyCOND_TIMEDWAIT(&(cond), &(mut), (microseconds)); \ |
| 82 | if (r < 0) \ |
| 83 | Py_FatalError("PyCOND_WAIT(" #cond ") failed"); \ |
| 84 | if (r) /* 1 == timeout, 2 == impl. can't say, so assume timeout */ \ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 85 | timeout_result = 1; \ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 86 | else \ |
| 87 | timeout_result = 0; \ |
| 88 | } \ |
| 89 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 90 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 91 | #define DEFAULT_INTERVAL 5000 |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 92 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 93 | static void _gil_initialize(struct _gil_runtime_state *gil) |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 94 | { |
| 95 | _Py_atomic_int uninitialized = {-1}; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 96 | gil->locked = uninitialized; |
| 97 | gil->interval = DEFAULT_INTERVAL; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 98 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 99 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 100 | static int gil_created(struct _gil_runtime_state *gil) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 101 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 102 | return (_Py_atomic_load_explicit(&gil->locked, _Py_memory_order_acquire) >= 0); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 105 | static void create_gil(struct _gil_runtime_state *gil) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 106 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 107 | MUTEX_INIT(gil->mutex); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 108 | #ifdef FORCE_SWITCHING |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 109 | MUTEX_INIT(gil->switch_mutex); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 110 | #endif |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 111 | COND_INIT(gil->cond); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 112 | #ifdef FORCE_SWITCHING |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 113 | COND_INIT(gil->switch_cond); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 114 | #endif |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 115 | _Py_atomic_store_relaxed(&gil->last_holder, 0); |
| 116 | _Py_ANNOTATE_RWLOCK_CREATE(&gil->locked); |
| 117 | _Py_atomic_store_explicit(&gil->locked, 0, _Py_memory_order_release); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 120 | static void destroy_gil(struct _gil_runtime_state *gil) |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 121 | { |
Kristján Valur Jónsson | 187aa54 | 2012-06-05 22:17:42 +0000 | [diff] [blame] | 122 | /* some pthread-like implementations tie the mutex to the cond |
| 123 | * and must have the cond destroyed first. |
| 124 | */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 125 | COND_FINI(gil->cond); |
| 126 | MUTEX_FINI(gil->mutex); |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 127 | #ifdef FORCE_SWITCHING |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 128 | COND_FINI(gil->switch_cond); |
| 129 | MUTEX_FINI(gil->switch_mutex); |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 130 | #endif |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 131 | _Py_atomic_store_explicit(&gil->locked, -1, |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 132 | _Py_memory_order_release); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 133 | _Py_ANNOTATE_RWLOCK_DESTROY(&gil->locked); |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 136 | static void recreate_gil(struct _gil_runtime_state *gil) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 137 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 138 | _Py_ANNOTATE_RWLOCK_DESTROY(&gil->locked); |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 139 | /* XXX should we destroy the old OS resources here? */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 140 | create_gil(gil); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 143 | static void |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 144 | drop_gil(struct _ceval_runtime_state *ceval, struct _ceval_state *ceval2, |
| 145 | PyThreadState *tstate) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 146 | { |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 147 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 148 | struct _gil_runtime_state *gil = &ceval2->gil; |
| 149 | #else |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 150 | struct _gil_runtime_state *gil = &ceval->gil; |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 151 | #endif |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 152 | if (!_Py_atomic_load_relaxed(&gil->locked)) { |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 153 | Py_FatalError("drop_gil: GIL is not locked"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Antoine Pitrou | 6a002c0 | 2011-01-15 11:37:11 +0000 | [diff] [blame] | 156 | /* tstate is allowed to be NULL (early interpreter init) */ |
| 157 | if (tstate != NULL) { |
| 158 | /* Sub-interpreter support: threads might have been switched |
| 159 | under our feet using PyThreadState_Swap(). Fix the GIL last |
| 160 | holder variable so that our heuristics work. */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 161 | _Py_atomic_store_relaxed(&gil->last_holder, (uintptr_t)tstate); |
Antoine Pitrou | 6a002c0 | 2011-01-15 11:37:11 +0000 | [diff] [blame] | 162 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 163 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 164 | MUTEX_LOCK(gil->mutex); |
| 165 | _Py_ANNOTATE_RWLOCK_RELEASED(&gil->locked, /*is_write=*/1); |
| 166 | _Py_atomic_store_relaxed(&gil->locked, 0); |
| 167 | COND_SIGNAL(gil->cond); |
| 168 | MUTEX_UNLOCK(gil->mutex); |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 169 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 170 | #ifdef FORCE_SWITCHING |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 171 | if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request) && tstate != NULL) { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 172 | MUTEX_LOCK(gil->switch_mutex); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 173 | /* Not switched yet => wait */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 174 | if (((PyThreadState*)_Py_atomic_load_relaxed(&gil->last_holder)) == tstate) |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 175 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 176 | assert(is_tstate_valid(tstate)); |
| 177 | RESET_GIL_DROP_REQUEST(tstate->interp); |
Antoine Pitrou | cf4cabb | 2009-11-11 18:11:36 +0000 | [diff] [blame] | 178 | /* NOTE: if COND_WAIT does not atomically start waiting when |
| 179 | releasing the mutex, another thread can run through, take |
| 180 | the GIL and drop it again, and reset the condition |
Antoine Pitrou | a1d2332 | 2009-11-12 22:56:02 +0000 | [diff] [blame] | 181 | before we even had a chance to wait for it. */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 182 | COND_WAIT(gil->switch_cond, gil->switch_mutex); |
| 183 | } |
| 184 | MUTEX_UNLOCK(gil->switch_mutex); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 185 | } |
| 186 | #endif |
| 187 | } |
| 188 | |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 189 | |
| 190 | /* Check if a Python thread must exit immediately, rather than taking the GIL |
| 191 | if Py_Finalize() has been called. |
| 192 | |
| 193 | When this function is called by a daemon thread after Py_Finalize() has been |
| 194 | called, the GIL does no longer exist. |
| 195 | |
| 196 | tstate must be non-NULL. */ |
| 197 | static inline int |
| 198 | tstate_must_exit(PyThreadState *tstate) |
| 199 | { |
| 200 | /* bpo-39877: Access _PyRuntime directly rather than using |
| 201 | tstate->interp->runtime to support calls from Python daemon threads. |
| 202 | After Py_Finalize() has been called, tstate can be a dangling pointer: |
| 203 | point to PyThreadState freed memory. */ |
Victor Stinner | a36adfa | 2020-03-19 19:48:25 +0100 | [diff] [blame] | 204 | PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(&_PyRuntime); |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 205 | return (finalizing != NULL && finalizing != tstate); |
| 206 | } |
| 207 | |
| 208 | |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 209 | /* Take the GIL. |
| 210 | |
| 211 | The function saves errno at entry and restores its value at exit. |
| 212 | |
| 213 | tstate must be non-NULL. */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 214 | static void |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 215 | take_gil(PyThreadState *tstate) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 216 | { |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 217 | int err = errno; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 218 | |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 219 | assert(tstate != NULL); |
| 220 | |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 221 | if (tstate_must_exit(tstate)) { |
Victor Stinner | 9229eee | 2020-03-09 23:10:53 +0100 | [diff] [blame] | 222 | /* bpo-39877: If Py_Finalize() has been called and tstate is not the |
| 223 | thread which called Py_Finalize(), exit immediately the thread. |
| 224 | |
| 225 | This code path can be reached by a daemon thread after Py_Finalize() |
| 226 | completes. In this case, tstate is a dangling pointer: points to |
| 227 | PyThreadState freed memory. */ |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 228 | PyThread_exit_thread(); |
| 229 | } |
| 230 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 231 | assert(is_tstate_valid(tstate)); |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 232 | PyInterpreterState *interp = tstate->interp; |
| 233 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 234 | struct _ceval_state *ceval2 = &interp->ceval; |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 235 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 236 | struct _gil_runtime_state *gil = &ceval2->gil; |
| 237 | #else |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 238 | struct _gil_runtime_state *gil = &ceval->gil; |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 239 | #endif |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 240 | |
| 241 | /* Check that _PyEval_InitThreads() was called to create the lock */ |
| 242 | assert(gil_created(gil)); |
| 243 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 244 | MUTEX_LOCK(gil->mutex); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 245 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 246 | if (!_Py_atomic_load_relaxed(&gil->locked)) { |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 247 | goto _ready; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 248 | } |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 249 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 250 | while (_Py_atomic_load_relaxed(&gil->locked)) { |
Victor Stinner | a36adfa | 2020-03-19 19:48:25 +0100 | [diff] [blame] | 251 | unsigned long saved_switchnum = gil->switch_number; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 252 | |
| 253 | unsigned long interval = (gil->interval >= 1 ? gil->interval : 1); |
Victor Stinner | a36adfa | 2020-03-19 19:48:25 +0100 | [diff] [blame] | 254 | int timed_out = 0; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 255 | COND_TIMED_WAIT(gil->cond, gil->mutex, interval, timed_out); |
Victor Stinner | a36adfa | 2020-03-19 19:48:25 +0100 | [diff] [blame] | 256 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 257 | /* If we timed out and no switch occurred in the meantime, it is time |
| 258 | to ask the GIL-holding thread to drop it. */ |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 259 | if (timed_out && |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 260 | _Py_atomic_load_relaxed(&gil->locked) && |
| 261 | gil->switch_number == saved_switchnum) |
| 262 | { |
Victor Stinner | a36adfa | 2020-03-19 19:48:25 +0100 | [diff] [blame] | 263 | if (tstate_must_exit(tstate)) { |
| 264 | MUTEX_UNLOCK(gil->mutex); |
| 265 | PyThread_exit_thread(); |
| 266 | } |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 267 | assert(is_tstate_valid(tstate)); |
Victor Stinner | a36adfa | 2020-03-19 19:48:25 +0100 | [diff] [blame] | 268 | |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 269 | SET_GIL_DROP_REQUEST(interp); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 272 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 273 | _ready: |
| 274 | #ifdef FORCE_SWITCHING |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 275 | /* This mutex must be taken before modifying gil->last_holder: |
| 276 | see drop_gil(). */ |
| 277 | MUTEX_LOCK(gil->switch_mutex); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 278 | #endif |
| 279 | /* We now hold the GIL */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 280 | _Py_atomic_store_relaxed(&gil->locked, 1); |
| 281 | _Py_ANNOTATE_RWLOCK_ACQUIRED(&gil->locked, /*is_write=*/1); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 282 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 283 | if (tstate != (PyThreadState*)_Py_atomic_load_relaxed(&gil->last_holder)) { |
| 284 | _Py_atomic_store_relaxed(&gil->last_holder, (uintptr_t)tstate); |
| 285 | ++gil->switch_number; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 286 | } |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 287 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 288 | #ifdef FORCE_SWITCHING |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 289 | COND_SIGNAL(gil->switch_cond); |
| 290 | MUTEX_UNLOCK(gil->switch_mutex); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 291 | #endif |
Victor Stinner | 5a3a71d | 2020-03-19 17:40:12 +0100 | [diff] [blame] | 292 | |
Victor Stinner | a36adfa | 2020-03-19 19:48:25 +0100 | [diff] [blame] | 293 | if (tstate_must_exit(tstate)) { |
| 294 | /* bpo-36475: If Py_Finalize() has been called and tstate is not |
| 295 | the thread which called Py_Finalize(), exit immediately the |
| 296 | thread. |
| 297 | |
| 298 | This code path can be reached by a daemon thread which was waiting |
| 299 | in take_gil() while the main thread called |
| 300 | wait_for_thread_shutdown() from Py_Finalize(). */ |
| 301 | MUTEX_UNLOCK(gil->mutex); |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 302 | drop_gil(ceval, ceval2, tstate); |
Victor Stinner | a36adfa | 2020-03-19 19:48:25 +0100 | [diff] [blame] | 303 | PyThread_exit_thread(); |
| 304 | } |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 305 | assert(is_tstate_valid(tstate)); |
Victor Stinner | a36adfa | 2020-03-19 19:48:25 +0100 | [diff] [blame] | 306 | |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 307 | if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 308 | RESET_GIL_DROP_REQUEST(interp); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 309 | } |
Victor Stinner | 5a3a71d | 2020-03-19 17:40:12 +0100 | [diff] [blame] | 310 | else { |
| 311 | /* bpo-40010: eval_breaker should be recomputed to be set to 1 if there |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 312 | is a pending signal: signal received by another thread which cannot |
Victor Stinner | 5a3a71d | 2020-03-19 17:40:12 +0100 | [diff] [blame] | 313 | handle signals. |
| 314 | |
| 315 | Note: RESET_GIL_DROP_REQUEST() calls COMPUTE_EVAL_BREAKER(). */ |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 316 | COMPUTE_EVAL_BREAKER(interp, ceval, ceval2); |
Victor Stinner | 5a3a71d | 2020-03-19 17:40:12 +0100 | [diff] [blame] | 317 | } |
Victor Stinner | 29356e0 | 2020-03-18 03:04:33 +0100 | [diff] [blame] | 318 | |
Victor Stinner | 29356e0 | 2020-03-18 03:04:33 +0100 | [diff] [blame] | 319 | /* Don't access tstate if the thread must exit */ |
Victor Stinner | a36adfa | 2020-03-19 19:48:25 +0100 | [diff] [blame] | 320 | if (tstate->async_exc != NULL) { |
Victor Stinner | 56bfdeb | 2020-03-18 09:26:25 +0100 | [diff] [blame] | 321 | _PyEval_SignalAsyncExc(tstate); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 322 | } |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 323 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 324 | MUTEX_UNLOCK(gil->mutex); |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 325 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 326 | errno = err; |
| 327 | } |
| 328 | |
| 329 | void _PyEval_SetSwitchInterval(unsigned long microseconds) |
| 330 | { |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 331 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 332 | PyInterpreterState *interp = PyInterpreterState_Get(); |
| 333 | struct _gil_runtime_state *gil = &interp->ceval.gil; |
| 334 | #else |
| 335 | struct _gil_runtime_state *gil = &_PyRuntime.ceval.gil; |
| 336 | #endif |
| 337 | gil->interval = microseconds; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | unsigned long _PyEval_GetSwitchInterval() |
| 341 | { |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 342 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 343 | PyInterpreterState *interp = PyInterpreterState_Get(); |
| 344 | struct _gil_runtime_state *gil = &interp->ceval.gil; |
| 345 | #else |
| 346 | struct _gil_runtime_state *gil = &_PyRuntime.ceval.gil; |
| 347 | #endif |
| 348 | return gil->interval; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 349 | } |