@@ -198,16 +198,6 @@ ensure_tstate_not_null(const char *func, PyThreadState *tstate)
198
198
}
199
199
200
200
201
- #ifndef NDEBUG
202
- static int is_tstate_valid (PyThreadState * tstate )
203
- {
204
- assert (!_PyMem_IsPtrFreed (tstate ));
205
- assert (!_PyMem_IsPtrFreed (tstate -> interp ));
206
- return 1 ;
207
- }
208
- #endif
209
-
210
-
211
201
int
212
202
PyEval_ThreadsInitialized (void )
213
203
{
@@ -230,13 +220,15 @@ _PyEval_InitThreads(PyThreadState *tstate)
230
220
231
221
PyThread_init_thread ();
232
222
create_gil (gil );
233
- take_gil (ceval , tstate );
223
+
224
+ take_gil (tstate );
234
225
235
226
struct _pending_calls * pending = & ceval -> pending ;
236
227
pending -> lock = PyThread_allocate_lock ();
237
228
if (pending -> lock == NULL ) {
238
229
return _PyStatus_NO_MEMORY ();
239
230
}
231
+
240
232
return _PyStatus_OK ();
241
233
}
242
234
@@ -269,30 +261,6 @@ _PyEval_FiniThreads(struct _ceval_runtime_state *ceval)
269
261
}
270
262
}
271
263
272
- /* This function is designed to exit daemon threads immediately rather than
273
- taking the GIL if Py_Finalize() has been called.
274
-
275
- The caller must *not* hold the GIL, since this function does not release
276
- the GIL before exiting the thread.
277
-
278
- When this function is called by a daemon thread after Py_Finalize() has been
279
- called, the GIL does no longer exist.
280
-
281
- tstate must be non-NULL. */
282
- static inline void
283
- exit_thread_if_finalizing (PyThreadState * tstate )
284
- {
285
- /* bpo-39877: Access _PyRuntime directly rather than using
286
- tstate->interp->runtime to support calls from Python daemon threads.
287
- After Py_Finalize() has been called, tstate can be a dangling pointer:
288
- point to PyThreadState freed memory. */
289
- _PyRuntimeState * runtime = & _PyRuntime ;
290
- PyThreadState * finalizing = _PyRuntimeState_GetFinalizing (runtime );
291
- if (finalizing != NULL && finalizing != tstate ) {
292
- PyThread_exit_thread ();
293
- }
294
- }
295
-
296
264
void
297
265
_PyEval_Fini (void )
298
266
{
@@ -329,11 +297,7 @@ PyEval_AcquireLock(void)
329
297
PyThreadState * tstate = _PyRuntimeState_GetThreadState (runtime );
330
298
ensure_tstate_not_null (__func__ , tstate );
331
299
332
- exit_thread_if_finalizing (tstate );
333
- assert (is_tstate_valid (tstate ));
334
-
335
- struct _ceval_runtime_state * ceval = & runtime -> ceval ;
336
- take_gil (ceval , tstate );
300
+ take_gil (tstate );
337
301
}
338
302
339
303
void
@@ -353,17 +317,10 @@ PyEval_AcquireThread(PyThreadState *tstate)
353
317
{
354
318
ensure_tstate_not_null (__func__ , tstate );
355
319
356
- exit_thread_if_finalizing (tstate );
357
- assert (is_tstate_valid (tstate ));
358
-
359
- _PyRuntimeState * runtime = tstate -> interp -> runtime ;
360
- struct _ceval_runtime_state * ceval = & runtime -> ceval ;
361
-
362
- /* Check that _PyEval_InitThreads() was called to create the lock */
363
- assert (gil_created (& ceval -> gil ));
320
+ take_gil (tstate );
364
321
365
- take_gil ( ceval , tstate ) ;
366
- if (_PyThreadState_Swap (& runtime -> gilstate , tstate ) != NULL ) {
322
+ struct _gilstate_runtime_state * gilstate = & tstate -> interp -> runtime -> gilstate ;
323
+ if (_PyThreadState_Swap (gilstate , tstate ) != NULL ) {
367
324
Py_FatalError ("non-NULL old thread state" );
368
325
}
369
326
}
@@ -396,7 +353,8 @@ _PyEval_ReInitThreads(_PyRuntimeState *runtime)
396
353
recreate_gil (& ceval -> gil );
397
354
PyThreadState * tstate = _PyRuntimeState_GetThreadState (runtime );
398
355
ensure_tstate_not_null (__func__ , tstate );
399
- take_gil (ceval , tstate );
356
+
357
+ take_gil (tstate );
400
358
401
359
struct _pending_calls * pending = & ceval -> pending ;
402
360
pending -> lock = PyThread_allocate_lock ();
@@ -436,16 +394,10 @@ PyEval_RestoreThread(PyThreadState *tstate)
436
394
{
437
395
ensure_tstate_not_null (__func__ , tstate );
438
396
439
- exit_thread_if_finalizing (tstate );
440
- assert (is_tstate_valid (tstate ));
397
+ take_gil (tstate );
441
398
442
- _PyRuntimeState * runtime = tstate -> interp -> runtime ;
443
- struct _ceval_runtime_state * ceval = & runtime -> ceval ;
444
- assert (gil_created (& ceval -> gil ));
445
-
446
- take_gil (ceval , tstate );
447
-
448
- _PyThreadState_Swap (& runtime -> gilstate , tstate );
399
+ struct _gilstate_runtime_state * gilstate = & tstate -> interp -> runtime -> gilstate ;
400
+ _PyThreadState_Swap (gilstate , tstate );
449
401
}
450
402
451
403
@@ -805,7 +757,6 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
805
757
806
758
PyThreadState * const tstate = _PyRuntimeState_GetThreadState (runtime );
807
759
ensure_tstate_not_null (__func__ , tstate );
808
- assert (is_tstate_valid (tstate ));
809
760
810
761
/* when tracing we set things up so that
811
762
@@ -1294,10 +1245,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
1294
1245
1295
1246
/* Other threads may run now */
1296
1247
1297
- /* Check if we should make a quick exit. */
1298
- exit_thread_if_finalizing (tstate );
1299
-
1300
- take_gil (ceval , tstate );
1248
+ take_gil (tstate );
1301
1249
1302
1250
if (_PyThreadState_Swap (& runtime -> gilstate , tstate ) != NULL ) {
1303
1251
Py_FatalError ("orphan tstate" );
0 commit comments