@@ -106,7 +106,7 @@ do { \
106106#define COND_INIT (cond ) \
107107 if (pthread_cond_init(&cond, NULL)) { \
108108 Py_FatalError("pthread_cond_init(" #cond ") failed"); };
109- #define COND_PREPARE (cond )
109+ #define COND_RESET (cond )
110110#define COND_SIGNAL (cond ) \
111111 if (pthread_cond_signal(&cond)) { \
112112 Py_FatalError("pthread_cond_signal(" #cond ") failed"); };
@@ -172,7 +172,7 @@ do { \
172172 /* auto-reset, non-signalled */ \
173173 if (!(cond = CreateEvent(NULL, FALSE, FALSE, NULL))) { \
174174 Py_FatalError("CreateMutex(" #cond ") failed"); };
175- #define COND_PREPARE (cond ) \
175+ #define COND_RESET (cond ) \
176176 if (!ResetEvent(cond)) { \
177177 Py_FatalError("ResetEvent(" #cond ") failed"); };
178178#define COND_SIGNAL (cond ) \
@@ -265,23 +265,21 @@ static void drop_gil(PyThreadState *tstate)
265265 MUTEX_LOCK (gil_mutex );
266266 gil_locked = 0 ;
267267 COND_SIGNAL (gil_cond );
268- #ifdef FORCE_SWITCHING
269- if (gil_drop_request )
270- COND_PREPARE (switch_cond );
271- #endif
272268 MUTEX_UNLOCK (gil_mutex );
273269
274270#ifdef FORCE_SWITCHING
275- if (gil_drop_request ) {
271+ if (gil_drop_request && tstate != NULL ) {
276272 MUTEX_LOCK (switch_mutex );
277273 /* Not switched yet => wait */
278- if (gil_last_holder == tstate )
274+ if (gil_last_holder == tstate ) {
275+ RESET_GIL_DROP_REQUEST ();
279276 /* NOTE: if COND_WAIT does not atomically start waiting when
280277 releasing the mutex, another thread can run through, take
281278 the GIL and drop it again, and reset the condition
282- (COND_PREPARE above) before we even had a chance to wait
283- for it. */
279+ before we even had a chance to wait for it. */
284280 COND_WAIT (switch_cond , switch_mutex );
281+ COND_RESET (switch_cond );
282+ }
285283 MUTEX_UNLOCK (switch_mutex );
286284 }
287285#endif
@@ -299,7 +297,7 @@ static void take_gil(PyThreadState *tstate)
299297 if (!gil_locked )
300298 goto _ready ;
301299
302- COND_PREPARE (gil_cond );
300+ COND_RESET (gil_cond );
303301 while (gil_locked ) {
304302 int timed_out = 0 ;
305303 unsigned long saved_switchnum ;
0 commit comments