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 | |
| 10 | /* First some general settings */ |
| 11 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 12 | #define INTERVAL (_PyRuntime.ceval.gil.interval >= 1 ? _PyRuntime.ceval.gil.interval : 1) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 13 | |
| 14 | |
| 15 | /* |
| 16 | Notes about the implementation: |
| 17 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 18 | - The GIL is just a boolean variable (locked) whose access is protected |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 19 | by a mutex (gil_mutex), and whose changes are signalled by a condition |
| 20 | variable (gil_cond). gil_mutex is taken for short periods of time, |
| 21 | and therefore mostly uncontended. |
| 22 | |
| 23 | - In the GIL-holding thread, the main loop (PyEval_EvalFrameEx) must be |
| 24 | able to release the GIL on demand by another thread. A volatile boolean |
| 25 | variable (gil_drop_request) is used for that purpose, which is checked |
| 26 | at every turn of the eval loop. That variable is set after a wait of |
| 27 | `interval` microseconds on `gil_cond` has timed out. |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 28 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 29 | [Actually, another volatile boolean variable (eval_breaker) is used |
| 30 | which ORs several conditions into one. Volatile booleans are |
| 31 | sufficient as inter-thread signalling means since Python is run |
| 32 | on cache-coherent architectures only.] |
| 33 | |
| 34 | - A thread wanting to take the GIL will first let pass a given amount of |
| 35 | time (`interval` microseconds) before setting gil_drop_request. This |
| 36 | encourages a defined switching period, but doesn't enforce it since |
| 37 | opcodes can take an arbitrary time to execute. |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 38 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 39 | The `interval` value is available for the user to read and modify |
| 40 | using the Python API `sys.{get,set}switchinterval()`. |
| 41 | |
| 42 | - When a thread releases the GIL and gil_drop_request is set, that thread |
| 43 | ensures that another GIL-awaiting thread gets scheduled. |
| 44 | It does so by waiting on a condition variable (switch_cond) until |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 45 | the value of last_holder is changed to something else than its |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 46 | own thread state pointer, indicating that another thread was able to |
| 47 | take the GIL. |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 48 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 49 | This is meant to prohibit the latency-adverse behaviour on multi-core |
| 50 | machines where one thread would speculatively release the GIL, but still |
| 51 | run and end up being the first to re-acquire it, making the "timeslices" |
| 52 | much longer than expected. |
| 53 | (Note: this mechanism is enabled with FORCE_SWITCHING above) |
| 54 | */ |
| 55 | |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 56 | #include "condvar.h" |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 57 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 58 | #define MUTEX_INIT(mut) \ |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 59 | if (PyMUTEX_INIT(&(mut))) { \ |
| 60 | Py_FatalError("PyMUTEX_INIT(" #mut ") failed"); }; |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 61 | #define MUTEX_FINI(mut) \ |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 62 | if (PyMUTEX_FINI(&(mut))) { \ |
| 63 | Py_FatalError("PyMUTEX_FINI(" #mut ") failed"); }; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 64 | #define MUTEX_LOCK(mut) \ |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 65 | if (PyMUTEX_LOCK(&(mut))) { \ |
| 66 | Py_FatalError("PyMUTEX_LOCK(" #mut ") failed"); }; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 67 | #define MUTEX_UNLOCK(mut) \ |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 68 | if (PyMUTEX_UNLOCK(&(mut))) { \ |
| 69 | Py_FatalError("PyMUTEX_UNLOCK(" #mut ") failed"); }; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 70 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 71 | #define COND_INIT(cond) \ |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 72 | if (PyCOND_INIT(&(cond))) { \ |
| 73 | Py_FatalError("PyCOND_INIT(" #cond ") failed"); }; |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 74 | #define COND_FINI(cond) \ |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 75 | if (PyCOND_FINI(&(cond))) { \ |
| 76 | Py_FatalError("PyCOND_FINI(" #cond ") failed"); }; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 77 | #define COND_SIGNAL(cond) \ |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 78 | if (PyCOND_SIGNAL(&(cond))) { \ |
| 79 | Py_FatalError("PyCOND_SIGNAL(" #cond ") failed"); }; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 80 | #define COND_WAIT(cond, mut) \ |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 81 | if (PyCOND_WAIT(&(cond), &(mut))) { \ |
| 82 | Py_FatalError("PyCOND_WAIT(" #cond ") failed"); }; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 83 | #define COND_TIMED_WAIT(cond, mut, microseconds, timeout_result) \ |
| 84 | { \ |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 85 | int r = PyCOND_TIMEDWAIT(&(cond), &(mut), (microseconds)); \ |
| 86 | if (r < 0) \ |
| 87 | Py_FatalError("PyCOND_WAIT(" #cond ") failed"); \ |
| 88 | if (r) /* 1 == timeout, 2 == impl. can't say, so assume timeout */ \ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 89 | timeout_result = 1; \ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 90 | else \ |
| 91 | timeout_result = 0; \ |
| 92 | } \ |
| 93 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 94 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 95 | #define DEFAULT_INTERVAL 5000 |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 96 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 97 | static void _gil_initialize(struct _gil_runtime_state *state) |
| 98 | { |
| 99 | _Py_atomic_int uninitialized = {-1}; |
| 100 | state->locked = uninitialized; |
| 101 | state->interval = DEFAULT_INTERVAL; |
| 102 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 103 | |
| 104 | static int gil_created(void) |
| 105 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 106 | return (_Py_atomic_load_explicit(&_PyRuntime.ceval.gil.locked, |
| 107 | _Py_memory_order_acquire) |
| 108 | ) >= 0; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static void create_gil(void) |
| 112 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 113 | MUTEX_INIT(_PyRuntime.ceval.gil.mutex); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 114 | #ifdef FORCE_SWITCHING |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 115 | MUTEX_INIT(_PyRuntime.ceval.gil.switch_mutex); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 116 | #endif |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 117 | COND_INIT(_PyRuntime.ceval.gil.cond); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 118 | #ifdef FORCE_SWITCHING |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 119 | COND_INIT(_PyRuntime.ceval.gil.switch_cond); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 120 | #endif |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 121 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil.last_holder, 0); |
| 122 | _Py_ANNOTATE_RWLOCK_CREATE(&_PyRuntime.ceval.gil.locked); |
| 123 | _Py_atomic_store_explicit(&_PyRuntime.ceval.gil.locked, 0, |
| 124 | _Py_memory_order_release); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 127 | static void destroy_gil(void) |
| 128 | { |
Kristján Valur Jónsson | 187aa54 | 2012-06-05 22:17:42 +0000 | [diff] [blame] | 129 | /* some pthread-like implementations tie the mutex to the cond |
| 130 | * and must have the cond destroyed first. |
| 131 | */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 132 | COND_FINI(_PyRuntime.ceval.gil.cond); |
| 133 | MUTEX_FINI(_PyRuntime.ceval.gil.mutex); |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 134 | #ifdef FORCE_SWITCHING |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 135 | COND_FINI(_PyRuntime.ceval.gil.switch_cond); |
| 136 | MUTEX_FINI(_PyRuntime.ceval.gil.switch_mutex); |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 137 | #endif |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 138 | _Py_atomic_store_explicit(&_PyRuntime.ceval.gil.locked, -1, |
| 139 | _Py_memory_order_release); |
| 140 | _Py_ANNOTATE_RWLOCK_DESTROY(&_PyRuntime.ceval.gil.locked); |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 143 | static void recreate_gil(void) |
| 144 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 145 | _Py_ANNOTATE_RWLOCK_DESTROY(&_PyRuntime.ceval.gil.locked); |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 146 | /* XXX should we destroy the old OS resources here? */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 147 | create_gil(); |
| 148 | } |
| 149 | |
| 150 | static void drop_gil(PyThreadState *tstate) |
| 151 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 152 | if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.gil.locked)) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 153 | Py_FatalError("drop_gil: GIL is not locked"); |
Antoine Pitrou | 6a002c0 | 2011-01-15 11:37:11 +0000 | [diff] [blame] | 154 | /* tstate is allowed to be NULL (early interpreter init) */ |
| 155 | if (tstate != NULL) { |
| 156 | /* Sub-interpreter support: threads might have been switched |
| 157 | under our feet using PyThreadState_Swap(). Fix the GIL last |
| 158 | holder variable so that our heuristics work. */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 159 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil.last_holder, |
| 160 | (uintptr_t)tstate); |
Antoine Pitrou | 6a002c0 | 2011-01-15 11:37:11 +0000 | [diff] [blame] | 161 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 162 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 163 | MUTEX_LOCK(_PyRuntime.ceval.gil.mutex); |
| 164 | _Py_ANNOTATE_RWLOCK_RELEASED(&_PyRuntime.ceval.gil.locked, /*is_write=*/1); |
| 165 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil.locked, 0); |
| 166 | COND_SIGNAL(_PyRuntime.ceval.gil.cond); |
| 167 | MUTEX_UNLOCK(_PyRuntime.ceval.gil.mutex); |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 168 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 169 | #ifdef FORCE_SWITCHING |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 170 | if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.gil_drop_request) && |
| 171 | tstate != NULL) |
| 172 | { |
| 173 | MUTEX_LOCK(_PyRuntime.ceval.gil.switch_mutex); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 174 | /* Not switched yet => wait */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 175 | if (((PyThreadState*)_Py_atomic_load_relaxed( |
| 176 | &_PyRuntime.ceval.gil.last_holder) |
| 177 | ) == tstate) |
| 178 | { |
Eric Snow | ef4ac96 | 2019-02-24 15:40:47 -0800 | [diff] [blame] | 179 | RESET_GIL_DROP_REQUEST(tstate->interp); |
Antoine Pitrou | cf4cabb | 2009-11-11 18:11:36 +0000 | [diff] [blame] | 180 | /* NOTE: if COND_WAIT does not atomically start waiting when |
| 181 | releasing the mutex, another thread can run through, take |
| 182 | the GIL and drop it again, and reset the condition |
Antoine Pitrou | a1d2332 | 2009-11-12 22:56:02 +0000 | [diff] [blame] | 183 | before we even had a chance to wait for it. */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 184 | COND_WAIT(_PyRuntime.ceval.gil.switch_cond, |
| 185 | _PyRuntime.ceval.gil.switch_mutex); |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 186 | } |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 187 | MUTEX_UNLOCK(_PyRuntime.ceval.gil.switch_mutex); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 188 | } |
| 189 | #endif |
| 190 | } |
| 191 | |
| 192 | static void take_gil(PyThreadState *tstate) |
| 193 | { |
| 194 | int err; |
| 195 | if (tstate == NULL) |
| 196 | Py_FatalError("take_gil: NULL tstate"); |
| 197 | |
| 198 | err = errno; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 199 | MUTEX_LOCK(_PyRuntime.ceval.gil.mutex); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 200 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 201 | if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.gil.locked)) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 202 | goto _ready; |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 203 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 204 | while (_Py_atomic_load_relaxed(&_PyRuntime.ceval.gil.locked)) { |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 205 | int timed_out = 0; |
| 206 | unsigned long saved_switchnum; |
| 207 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 208 | saved_switchnum = _PyRuntime.ceval.gil.switch_number; |
| 209 | COND_TIMED_WAIT(_PyRuntime.ceval.gil.cond, _PyRuntime.ceval.gil.mutex, |
| 210 | INTERVAL, timed_out); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 211 | /* If we timed out and no switch occurred in the meantime, it is time |
| 212 | to ask the GIL-holding thread to drop it. */ |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 213 | if (timed_out && |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 214 | _Py_atomic_load_relaxed(&_PyRuntime.ceval.gil.locked) && |
| 215 | _PyRuntime.ceval.gil.switch_number == saved_switchnum) { |
Eric Snow | ef4ac96 | 2019-02-24 15:40:47 -0800 | [diff] [blame] | 216 | SET_GIL_DROP_REQUEST(tstate->interp); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | _ready: |
| 220 | #ifdef FORCE_SWITCHING |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 221 | /* This mutex must be taken before modifying |
| 222 | _PyRuntime.ceval.gil.last_holder (see drop_gil()). */ |
| 223 | MUTEX_LOCK(_PyRuntime.ceval.gil.switch_mutex); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 224 | #endif |
| 225 | /* We now hold the GIL */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 226 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil.locked, 1); |
| 227 | _Py_ANNOTATE_RWLOCK_ACQUIRED(&_PyRuntime.ceval.gil.locked, /*is_write=*/1); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 228 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 229 | if (tstate != (PyThreadState*)_Py_atomic_load_relaxed( |
| 230 | &_PyRuntime.ceval.gil.last_holder)) |
| 231 | { |
| 232 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil.last_holder, |
| 233 | (uintptr_t)tstate); |
| 234 | ++_PyRuntime.ceval.gil.switch_number; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 235 | } |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 236 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 237 | #ifdef FORCE_SWITCHING |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 238 | COND_SIGNAL(_PyRuntime.ceval.gil.switch_cond); |
| 239 | MUTEX_UNLOCK(_PyRuntime.ceval.gil.switch_mutex); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 240 | #endif |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 241 | if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.gil_drop_request)) { |
Eric Snow | ef4ac96 | 2019-02-24 15:40:47 -0800 | [diff] [blame] | 242 | RESET_GIL_DROP_REQUEST(tstate->interp); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 243 | } |
| 244 | if (tstate->async_exc != NULL) { |
Eric Snow | ef4ac96 | 2019-02-24 15:40:47 -0800 | [diff] [blame] | 245 | _PyEval_SignalAsyncExc(tstate->interp); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 246 | } |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 247 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 248 | MUTEX_UNLOCK(_PyRuntime.ceval.gil.mutex); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 249 | errno = err; |
| 250 | } |
| 251 | |
| 252 | void _PyEval_SetSwitchInterval(unsigned long microseconds) |
| 253 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 254 | _PyRuntime.ceval.gil.interval = microseconds; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | unsigned long _PyEval_GetSwitchInterval() |
| 258 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 259 | return _PyRuntime.ceval.gil.interval; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 260 | } |