bpo-36475: Finalize PyEval_AcquireLock() and PyEval_AcquireThread() p… · python/cpython@f781d20 (original) (raw)
`@@ -76,6 +76,7 @@ static PyObject * special_lookup(PyObject *, _Py_Identifier *);
`
76
76
`static int check_args_iterable(PyObject *func, PyObject *vararg);
`
77
77
`static void format_kwargs_error(PyObject *func, PyObject *kwargs);
`
78
78
`static void format_awaitable_error(PyTypeObject *, int);
`
``
79
`+
static inline void exit_thread_if_finalizing(PyThreadState *);
`
79
80
``
80
81
`#define NAME_ERROR_MSG \
`
81
82
` "name '%.200s' is not defined"
`
`@@ -203,13 +204,25 @@ _PyEval_FiniThreads(void)
`
203
204
` }
`
204
205
`}
`
205
206
``
``
207
`+
static inline void
`
``
208
`+
exit_thread_if_finalizing(PyThreadState *tstate)
`
``
209
`+
{
`
``
210
`+
/* _Py_Finalizing is protected by the GIL */
`
``
211
`+
if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
`
``
212
`+
drop_gil(tstate);
`
``
213
`+
PyThread_exit_thread();
`
``
214
`+
Py_UNREACHABLE();
`
``
215
`+
}
`
``
216
`+
}
`
``
217
+
206
218
`void
`
207
219
`PyEval_AcquireLock(void)
`
208
220
`{
`
209
221
`PyThreadState *tstate = _PyThreadState_GET();
`
210
222
`if (tstate == NULL)
`
211
223
`Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
`
212
224
`take_gil(tstate);
`
``
225
`+
exit_thread_if_finalizing(tstate);
`
213
226
`}
`
214
227
``
215
228
`void
`
`@@ -230,6 +243,7 @@ PyEval_AcquireThread(PyThreadState *tstate)
`
230
243
`/* Check someone has called PyEval_InitThreads() to create the lock */
`
231
244
`assert(gil_created());
`
232
245
`take_gil(tstate);
`
``
246
`+
exit_thread_if_finalizing(tstate);
`
233
247
`if (PyThreadState_Swap(tstate) != NULL)
`
234
248
`Py_FatalError(
`
235
249
`"PyEval_AcquireThread: non-NULL old thread state");
`
`@@ -298,12 +312,7 @@ PyEval_RestoreThread(PyThreadState *tstate)
`
298
312
``
299
313
`int err = errno;
`
300
314
`take_gil(tstate);
`
301
``
`-
/* _Py_Finalizing is protected by the GIL */
`
302
``
`-
if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
`
303
``
`-
drop_gil(tstate);
`
304
``
`-
PyThread_exit_thread();
`
305
``
`-
Py_UNREACHABLE();
`
306
``
`-
}
`
``
315
`+
exit_thread_if_finalizing(tstate);
`
307
316
`errno = err;
`
308
317
``
309
318
`PyThreadState_Swap(tstate);
`
`@@ -1083,12 +1092,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
`
1083
1092
`take_gil(tstate);
`
1084
1093
``
1085
1094
`/* Check if we should make a quick exit. */
`
1086
``
`-
if (_Py_IsFinalizing() &&
`
1087
``
`-
!_Py_CURRENTLY_FINALIZING(tstate))
`
1088
``
`-
{
`
1089
``
`-
drop_gil(tstate);
`
1090
``
`-
PyThread_exit_thread();
`
1091
``
`-
}
`
``
1095
`+
exit_thread_if_finalizing(tstate);
`
1092
1096
``
1093
1097
`if (PyThreadState_Swap(tstate) != NULL)
`
1094
1098
`Py_FatalError("ceval: orphan tstate");
`