Mercurial > cpython-lru_cache-weakref
annotate Python/ceval_gil.h @ 64289:325d016c19f7
#9689: add links from overview to in-depth class API descriptions.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Thu, 26 Aug 2010 14:30:15 +0000 |
| parents | 39092f86b0af |
| children | 557e2537c9ae |
| rev | line source |
|---|---|
| 57175 | 1 /* |
| 2 * Implementation of the Global Interpreter Lock (GIL). | |
| 3 */ | |
| 4 | |
| 5 #include <stdlib.h> | |
| 6 #include <errno.h> | |
| 7 | |
| 8 | |
| 9 /* First some general settings */ | |
| 10 | |
| 11 /* microseconds (the Python API uses seconds, though) */ | |
| 12 #define DEFAULT_INTERVAL 5000 | |
| 13 static unsigned long gil_interval = DEFAULT_INTERVAL; | |
| 14 #define INTERVAL (gil_interval >= 1 ? gil_interval : 1) | |
| 15 | |
| 16 /* Enable if you want to force the switching of threads at least every `gil_interval` */ | |
| 17 #undef FORCE_SWITCHING | |
| 18 #define FORCE_SWITCHING | |
| 19 | |
| 20 | |
| 21 /* | |
| 22 Notes about the implementation: | |
| 23 | |
| 24 - The GIL is just a boolean variable (gil_locked) whose access is protected | |
| 25 by a mutex (gil_mutex), and whose changes are signalled by a condition | |
| 26 variable (gil_cond). gil_mutex is taken for short periods of time, | |
| 27 and therefore mostly uncontended. | |
| 28 | |
| 29 - In the GIL-holding thread, the main loop (PyEval_EvalFrameEx) must be | |
| 30 able to release the GIL on demand by another thread. A volatile boolean | |
| 31 variable (gil_drop_request) is used for that purpose, which is checked | |
| 32 at every turn of the eval loop. That variable is set after a wait of | |
| 33 `interval` microseconds on `gil_cond` has timed out. | |
| 34 | |
| 35 [Actually, another volatile boolean variable (eval_breaker) is used | |
| 36 which ORs several conditions into one. Volatile booleans are | |
| 37 sufficient as inter-thread signalling means since Python is run | |
| 38 on cache-coherent architectures only.] | |
| 39 | |
| 40 - A thread wanting to take the GIL will first let pass a given amount of | |
| 41 time (`interval` microseconds) before setting gil_drop_request. This | |
| 42 encourages a defined switching period, but doesn't enforce it since | |
| 43 opcodes can take an arbitrary time to execute. | |
| 44 | |
| 45 The `interval` value is available for the user to read and modify | |
| 46 using the Python API `sys.{get,set}switchinterval()`. | |
| 47 | |
| 48 - When a thread releases the GIL and gil_drop_request is set, that thread | |
| 49 ensures that another GIL-awaiting thread gets scheduled. | |
| 50 It does so by waiting on a condition variable (switch_cond) until | |
| 51 the value of gil_last_holder is changed to something else than its | |
| 52 own thread state pointer, indicating that another thread was able to | |
| 53 take the GIL. | |
| 54 | |
| 55 This is meant to prohibit the latency-adverse behaviour on multi-core | |
| 56 machines where one thread would speculatively release the GIL, but still | |
| 57 run and end up being the first to re-acquire it, making the "timeslices" | |
| 58 much longer than expected. | |
| 59 (Note: this mechanism is enabled with FORCE_SWITCHING above) | |
| 60 */ | |
| 61 | |
| 62 #ifndef _POSIX_THREADS | |
| 63 /* This means pthreads are not implemented in libc headers, hence the macro | |
| 64 not present in unistd.h. But they still can be implemented as an external | |
| 65 library (e.g. gnu pth in pthread emulation) */ | |
| 66 # ifdef HAVE_PTHREAD_H | |
| 67 # include <pthread.h> /* _POSIX_THREADS */ | |
| 68 # endif | |
| 69 #endif | |
| 70 | |
| 71 | |
| 72 #ifdef _POSIX_THREADS | |
| 73 | |
| 74 /* | |
| 75 * POSIX support | |
| 76 */ | |
| 77 | |
| 78 #include <pthread.h> | |
| 79 | |
| 80 #define ADD_MICROSECONDS(tv, interval) \ | |
| 81 do { \ | |
| 82 tv.tv_usec += (long) interval; \ | |
| 83 tv.tv_sec += tv.tv_usec / 1000000; \ | |
| 84 tv.tv_usec %= 1000000; \ | |
| 85 } while (0) | |
| 86 | |
| 87 /* We assume all modern POSIX systems have gettimeofday() */ | |
| 88 #ifdef GETTIMEOFDAY_NO_TZ | |
| 89 #define GETTIMEOFDAY(ptv) gettimeofday(ptv) | |
| 90 #else | |
| 91 #define GETTIMEOFDAY(ptv) gettimeofday(ptv, (struct timezone *)NULL) | |
| 92 #endif | |
| 93 | |
| 94 #define MUTEX_T pthread_mutex_t | |
| 95 #define MUTEX_INIT(mut) \ | |
| 96 if (pthread_mutex_init(&mut, NULL)) { \ | |
| 97 Py_FatalError("pthread_mutex_init(" #mut ") failed"); }; | |
| 98 #define MUTEX_LOCK(mut) \ | |
| 99 if (pthread_mutex_lock(&mut)) { \ | |
| 100 Py_FatalError("pthread_mutex_lock(" #mut ") failed"); }; | |
| 101 #define MUTEX_UNLOCK(mut) \ | |
| 102 if (pthread_mutex_unlock(&mut)) { \ | |
| 103 Py_FatalError("pthread_mutex_unlock(" #mut ") failed"); }; | |
| 104 | |
| 105 #define COND_T pthread_cond_t | |
| 106 #define COND_INIT(cond) \ | |
| 107 if (pthread_cond_init(&cond, NULL)) { \ | |
| 108 Py_FatalError("pthread_cond_init(" #cond ") failed"); }; | |
| 109 #define COND_SIGNAL(cond) \ | |
| 110 if (pthread_cond_signal(&cond)) { \ | |
| 111 Py_FatalError("pthread_cond_signal(" #cond ") failed"); }; | |
| 112 #define COND_WAIT(cond, mut) \ | |
| 113 if (pthread_cond_wait(&cond, &mut)) { \ | |
| 114 Py_FatalError("pthread_cond_wait(" #cond ") failed"); }; | |
| 115 #define COND_TIMED_WAIT(cond, mut, microseconds, timeout_result) \ | |
| 116 { \ | |
| 117 int r; \ | |
| 118 struct timespec ts; \ | |
| 119 struct timeval deadline; \ | |
| 120 \ | |
| 121 GETTIMEOFDAY(&deadline); \ | |
| 122 ADD_MICROSECONDS(deadline, microseconds); \ | |
| 123 ts.tv_sec = deadline.tv_sec; \ | |
| 124 ts.tv_nsec = deadline.tv_usec * 1000; \ | |
| 125 \ | |
| 126 r = pthread_cond_timedwait(&cond, &mut, &ts); \ | |
| 127 if (r == ETIMEDOUT) \ | |
| 128 timeout_result = 1; \ | |
| 129 else if (r) \ | |
| 130 Py_FatalError("pthread_cond_timedwait(" #cond ") failed"); \ | |
| 131 else \ | |
| 132 timeout_result = 0; \ | |
| 133 } \ | |
| 134 | |
| 135 #elif defined(NT_THREADS) | |
| 136 | |
| 137 /* | |
| 138 * Windows (2000 and later, as well as (hopefully) CE) support | |
| 139 */ | |
| 140 | |
| 141 #include <windows.h> | |
| 142 | |
|
63911
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
143 #define MUTEX_T CRITICAL_SECTION |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
144 #define MUTEX_INIT(mut) do { \ |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
145 if (!(InitializeCriticalSectionAndSpinCount(&(mut), 4000))) \ |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
146 Py_FatalError("CreateMutex(" #mut ") failed"); \ |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
147 } while (0) |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
148 #define MUTEX_FINI(mut) \ |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
149 DeleteCriticalSection(&(mut)) |
| 57175 | 150 #define MUTEX_LOCK(mut) \ |
|
63911
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
151 EnterCriticalSection(&(mut)) |
| 57175 | 152 #define MUTEX_UNLOCK(mut) \ |
|
63911
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
153 LeaveCriticalSection(&(mut)) |
| 57175 | 154 |
|
63911
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
155 /* We emulate condition variables with a semaphore. |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
156 We use a Semaphore rather than an auto-reset event, because although |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
157 an auto-resent event might appear to solve the lost-wakeup bug (race |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
158 condition between releasing the outer lock and waiting) because it |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
159 maintains state even though a wait hasn't happened, there is still |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
160 a lost wakeup problem if more than one thread are interrupted in the |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
161 critical place. A semaphore solves that. |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
162 Because it is ok to signal a condition variable with no one |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
163 waiting, we need to keep track of the number of |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
164 waiting threads. Otherwise, the semaphore's state could rise |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
165 without bound. |
|
57191
c717fe993085
Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents:
57175
diff
changeset
|
166 |
|
63911
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
167 Generic emulations of the pthread_cond_* API using |
|
57191
c717fe993085
Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents:
57175
diff
changeset
|
168 Win32 functions can be found on the Web. |
|
c717fe993085
Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents:
57175
diff
changeset
|
169 The following read can be edificating (or not): |
|
c717fe993085
Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents:
57175
diff
changeset
|
170 http://www.cse.wustl.edu/~schmidt/win32-cv-1.html |
|
c717fe993085
Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents:
57175
diff
changeset
|
171 */ |
|
63911
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
172 typedef struct COND_T |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
173 { |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
174 HANDLE sem; /* the semaphore */ |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
175 int n_waiting; /* how many are unreleased */ |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
176 } COND_T; |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
177 |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
178 __inline static void _cond_init(COND_T *cond) |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
179 { |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
180 /* A semaphore with a large max value, The positive value |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
181 * is only needed to catch those "lost wakeup" events and |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
182 * race conditions when a timed wait elapses. |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
183 */ |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
184 if (!(cond->sem = CreateSemaphore(NULL, 0, 1000, NULL))) |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
185 Py_FatalError("CreateSemaphore() failed"); |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
186 cond->n_waiting = 0; |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
187 } |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
188 |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
189 __inline static void _cond_fini(COND_T *cond) |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
190 { |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
191 BOOL ok = CloseHandle(cond->sem); |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
192 if (!ok) |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
193 Py_FatalError("CloseHandle() failed"); |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
194 } |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
195 |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
196 __inline static void _cond_wait(COND_T *cond, MUTEX_T *mut) |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
197 { |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
198 ++cond->n_waiting; |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
199 MUTEX_UNLOCK(*mut); |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
200 /* "lost wakeup bug" would occur if the caller were interrupted here, |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
201 * but we are safe because we are using a semaphore wich has an internal |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
202 * count. |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
203 */ |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
204 if (WaitForSingleObject(cond->sem, INFINITE) == WAIT_FAILED) |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
205 Py_FatalError("WaitForSingleObject() failed"); |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
206 MUTEX_LOCK(*mut); |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
207 } |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
208 |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
209 __inline static int _cond_timed_wait(COND_T *cond, MUTEX_T *mut, |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
210 int us) |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
211 { |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
212 DWORD r; |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
213 ++cond->n_waiting; |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
214 MUTEX_UNLOCK(*mut); |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
215 r = WaitForSingleObject(cond->sem, us / 1000); |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
216 if (r == WAIT_FAILED) |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
217 Py_FatalError("WaitForSingleObject() failed"); |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
218 MUTEX_LOCK(*mut); |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
219 if (r == WAIT_TIMEOUT) |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
220 --cond->n_waiting; |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
221 /* Here we have a benign race condition with _cond_signal. If the |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
222 * wait operation has timed out, but before we can acquire the |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
223 * mutex again to decrement n_waiting, a thread holding the mutex |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
224 * still sees a positive n_waiting value and may call |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
225 * ReleaseSemaphore and decrement n_waiting. |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
226 * This will cause n_waiting to be decremented twice. |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
227 * This is benign, though, because ReleaseSemaphore will also have |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
228 * been called, leaving the semaphore state positive. We may |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
229 * thus end up with semaphore in state 1, and n_waiting == -1, and |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
230 * the next time someone calls _cond_wait(), that thread will |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
231 * pass right through, decrementing the semaphore state and |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
232 * incrementing n_waiting, thus correcting the extra _cond_signal. |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
233 */ |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
234 return r == WAIT_TIMEOUT; |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
235 } |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
236 |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
237 __inline static void _cond_signal(COND_T *cond) { |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
238 /* NOTE: This must be called with the mutex held */ |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
239 if (cond->n_waiting > 0) { |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
240 if (!ReleaseSemaphore(cond->sem, 1, NULL)) |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
241 Py_FatalError("ReleaseSemaphore() failed"); |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
242 --cond->n_waiting; |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
243 } |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
244 } |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
245 |
| 57175 | 246 #define COND_INIT(cond) \ |
|
63911
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
247 _cond_init(&(cond)) |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
248 #define COND_FINI(cond) \ |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
249 _cond_fini(&(cond)) |
| 57175 | 250 #define COND_SIGNAL(cond) \ |
|
63911
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
251 _cond_signal(&(cond)) |
| 57175 | 252 #define COND_WAIT(cond, mut) \ |
|
63911
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
253 _cond_wait(&(cond), &(mut)) |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
254 #define COND_TIMED_WAIT(cond, mut, us, timeout_result) do { \ |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
255 (timeout_result) = _cond_timed_wait(&(cond), &(mut), us); \ |
|
39092f86b0af
Issue #8411: new condition variable emulation under Windows for the new GIL,
Antoine Pitrou <solipsis@pitrou.net>
parents:
61184
diff
changeset
|
256 } while (0) |
| 57175 | 257 |
| 258 #else | |
| 259 | |
| 260 #error You need either a POSIX-compatible or a Windows system! | |
| 261 | |
| 262 #endif /* _POSIX_THREADS, NT_THREADS */ | |
| 263 | |
| 264 | |
|
61184
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
265 /* Whether the GIL is already taken (-1 if uninitialized). This is atomic |
| 57175 | 266 because it can be read without any lock taken in ceval.c. */ |
|
61184
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
267 static _Py_atomic_int gil_locked = {-1}; |
| 57175 | 268 /* Number of GIL switches since the beginning. */ |
| 269 static unsigned long gil_switch_number = 0; | |
|
61184
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
270 /* Last PyThreadState holding / having held the GIL. This helps us know |
|
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
271 whether anyone else was scheduled after we dropped the GIL. */ |
|
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
272 static _Py_atomic_address gil_last_holder = {NULL}; |
| 57175 | 273 |
| 274 /* This condition variable allows one or several threads to wait until | |
| 275 the GIL is released. In addition, the mutex also protects the above | |
| 276 variables. */ | |
| 277 static COND_T gil_cond; | |
| 278 static MUTEX_T gil_mutex; | |
| 279 | |
| 280 #ifdef FORCE_SWITCHING | |
| 281 /* This condition variable helps the GIL-releasing thread wait for | |
| 282 a GIL-awaiting thread to be scheduled and take the GIL. */ | |
| 283 static COND_T switch_cond; | |
| 284 static MUTEX_T switch_mutex; | |
| 285 #endif | |
| 286 | |
| 287 | |
| 288 static int gil_created(void) | |
| 289 { | |
|
61184
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
290 return _Py_atomic_load_explicit(&gil_locked, _Py_memory_order_acquire) >= 0; |
| 57175 | 291 } |
| 292 | |
| 293 static void create_gil(void) | |
| 294 { | |
| 295 MUTEX_INIT(gil_mutex); | |
| 296 #ifdef FORCE_SWITCHING | |
| 297 MUTEX_INIT(switch_mutex); | |
| 298 #endif | |
| 299 COND_INIT(gil_cond); | |
| 300 #ifdef FORCE_SWITCHING | |
| 301 COND_INIT(switch_cond); | |
| 302 #endif | |
|
61184
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
303 _Py_atomic_store_relaxed(&gil_last_holder, NULL); |
|
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
304 _Py_ANNOTATE_RWLOCK_CREATE(&gil_locked); |
|
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
305 _Py_atomic_store_explicit(&gil_locked, 0, _Py_memory_order_release); |
| 57175 | 306 } |
| 307 | |
| 308 static void recreate_gil(void) | |
| 309 { | |
|
61184
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
310 _Py_ANNOTATE_RWLOCK_DESTROY(&gil_locked); |
| 57175 | 311 create_gil(); |
| 312 } | |
| 313 | |
| 314 static void drop_gil(PyThreadState *tstate) | |
| 315 { | |
| 316 /* NOTE: tstate is allowed to be NULL. */ | |
|
61184
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
317 if (!_Py_atomic_load_relaxed(&gil_locked)) |
| 57175 | 318 Py_FatalError("drop_gil: GIL is not locked"); |
|
61184
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
319 if (tstate != NULL && |
|
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
320 tstate != _Py_atomic_load_relaxed(&gil_last_holder)) |
| 57175 | 321 Py_FatalError("drop_gil: wrong thread state"); |
| 322 | |
| 323 MUTEX_LOCK(gil_mutex); | |
|
61184
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
324 _Py_ANNOTATE_RWLOCK_RELEASED(&gil_locked, /*is_write=*/1); |
|
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
325 _Py_atomic_store_relaxed(&gil_locked, 0); |
| 57175 | 326 COND_SIGNAL(gil_cond); |
| 327 MUTEX_UNLOCK(gil_mutex); | |
| 328 | |
| 329 #ifdef FORCE_SWITCHING | |
|
61184
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
330 if (_Py_atomic_load_relaxed(&gil_drop_request) && tstate != NULL) { |
| 57175 | 331 MUTEX_LOCK(switch_mutex); |
| 332 /* Not switched yet => wait */ | |
|
61184
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
333 if (_Py_atomic_load_relaxed(&gil_last_holder) == tstate) { |
|
57203
f9a50cede6a9
Try to strengthen condition-waiting under Windows.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57191
diff
changeset
|
334 RESET_GIL_DROP_REQUEST(); |
|
57191
c717fe993085
Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents:
57175
diff
changeset
|
335 /* NOTE: if COND_WAIT does not atomically start waiting when |
|
c717fe993085
Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents:
57175
diff
changeset
|
336 releasing the mutex, another thread can run through, take |
|
c717fe993085
Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents:
57175
diff
changeset
|
337 the GIL and drop it again, and reset the condition |
|
57203
f9a50cede6a9
Try to strengthen condition-waiting under Windows.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57191
diff
changeset
|
338 before we even had a chance to wait for it. */ |
| 57175 | 339 COND_WAIT(switch_cond, switch_mutex); |
|
57203
f9a50cede6a9
Try to strengthen condition-waiting under Windows.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57191
diff
changeset
|
340 } |
| 57175 | 341 MUTEX_UNLOCK(switch_mutex); |
| 342 } | |
| 343 #endif | |
| 344 } | |
| 345 | |
| 346 static void take_gil(PyThreadState *tstate) | |
| 347 { | |
| 348 int err; | |
| 349 if (tstate == NULL) | |
| 350 Py_FatalError("take_gil: NULL tstate"); | |
| 351 | |
| 352 err = errno; | |
| 353 MUTEX_LOCK(gil_mutex); | |
| 354 | |
|
61184
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
355 if (!_Py_atomic_load_relaxed(&gil_locked)) |
| 57175 | 356 goto _ready; |
| 357 | |
|
61184
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
358 while (_Py_atomic_load_relaxed(&gil_locked)) { |
| 57175 | 359 int timed_out = 0; |
| 360 unsigned long saved_switchnum; | |
| 361 | |
| 362 saved_switchnum = gil_switch_number; | |
| 363 COND_TIMED_WAIT(gil_cond, gil_mutex, INTERVAL, timed_out); | |
| 364 /* If we timed out and no switch occurred in the meantime, it is time | |
| 365 to ask the GIL-holding thread to drop it. */ | |
|
61184
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
366 if (timed_out && |
|
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
367 _Py_atomic_load_relaxed(&gil_locked) && |
|
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
368 gil_switch_number == saved_switchnum) { |
| 57175 | 369 SET_GIL_DROP_REQUEST(); |
| 370 } | |
| 371 } | |
| 372 _ready: | |
| 373 #ifdef FORCE_SWITCHING | |
| 374 /* This mutex must be taken before modifying gil_last_holder (see drop_gil()). */ | |
| 375 MUTEX_LOCK(switch_mutex); | |
| 376 #endif | |
| 377 /* We now hold the GIL */ | |
|
61184
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
378 _Py_atomic_store_relaxed(&gil_locked, 1); |
|
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
379 _Py_ANNOTATE_RWLOCK_ACQUIRED(&gil_locked, /*is_write=*/1); |
| 57175 | 380 |
|
61184
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
381 if (tstate != _Py_atomic_load_relaxed(&gil_last_holder)) { |
|
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
382 _Py_atomic_store_relaxed(&gil_last_holder, tstate); |
| 57175 | 383 ++gil_switch_number; |
| 384 } | |
|
61184
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
385 |
| 57175 | 386 #ifdef FORCE_SWITCHING |
| 387 COND_SIGNAL(switch_cond); | |
| 388 MUTEX_UNLOCK(switch_mutex); | |
| 389 #endif | |
|
61184
4fd3ab5fa71d
Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
57203
diff
changeset
|
390 if (_Py_atomic_load_relaxed(&gil_drop_request)) { |
| 57175 | 391 RESET_GIL_DROP_REQUEST(); |
| 392 } | |
| 393 if (tstate->async_exc != NULL) { | |
| 394 _PyEval_SignalAsyncExc(); | |
| 395 } | |
| 396 | |
| 397 MUTEX_UNLOCK(gil_mutex); | |
| 398 errno = err; | |
| 399 } | |
| 400 | |
| 401 void _PyEval_SetSwitchInterval(unsigned long microseconds) | |
| 402 { | |
| 403 gil_interval = microseconds; | |
| 404 } | |
| 405 | |
| 406 unsigned long _PyEval_GetSwitchInterval() | |
| 407 { | |
| 408 return gil_interval; | |
| 409 } |
