| 
13 | 13 | #include "pycore_call.h"  | 
14 | 14 | #include "pycore_ceval.h"  | 
15 | 15 | #include "pycore_code.h"  | 
 | 16 | +#include "pycore_initconfig.h"  | 
16 | 17 | #include "pycore_object.h"  | 
17 | 18 | #include "pycore_pyerrors.h"  | 
18 | 19 | #include "pycore_pylifecycle.h"  | 
@@ -214,26 +215,39 @@ PyEval_ThreadsInitialized(void)  | 
214 | 215 |     return gil_created(&runtime->ceval.gil);  | 
215 | 216 | }  | 
216 | 217 | 
 
  | 
217 |  | -void  | 
218 |  | -PyEval_InitThreads(void)  | 
 | 218 | +PyStatus  | 
 | 219 | +_PyEval_InitThreads(PyThreadState *tstate)  | 
219 | 220 | {  | 
220 |  | -    _PyRuntimeState *runtime = &_PyRuntime;  | 
221 |  | -    struct _ceval_runtime_state *ceval = &runtime->ceval;  | 
 | 221 | +    if (tstate == NULL) {  | 
 | 222 | +        return _PyStatus_ERR("tstate is NULL");  | 
 | 223 | +    }  | 
 | 224 | + | 
 | 225 | +    struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;  | 
222 | 226 |     struct _gil_runtime_state *gil = &ceval->gil;  | 
223 | 227 |     if (gil_created(gil)) {  | 
224 |  | -        return;  | 
 | 228 | +        return _PyStatus_OK();  | 
225 | 229 |     }  | 
226 | 230 | 
 
  | 
227 | 231 |     PyThread_init_thread();  | 
228 | 232 |     create_gil(gil);  | 
229 |  | -    PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);  | 
230 |  | -    ensure_tstate_not_null(__func__, tstate);  | 
231 | 233 |     take_gil(ceval, tstate);  | 
232 | 234 | 
 
  | 
233 | 235 |     struct _pending_calls *pending = &ceval->pending;  | 
234 | 236 |     pending->lock = PyThread_allocate_lock();  | 
235 | 237 |     if (pending->lock == NULL) {  | 
236 |  | -        Py_FatalError("Can't initialize threads for pending calls");  | 
 | 238 | +        return _PyStatus_NO_MEMORY();  | 
 | 239 | +    }  | 
 | 240 | +    return _PyStatus_OK();  | 
 | 241 | +}  | 
 | 242 | + | 
 | 243 | +void  | 
 | 244 | +PyEval_InitThreads(void)  | 
 | 245 | +{  | 
 | 246 | +    PyThreadState *tstate = _PyThreadState_GET();  | 
 | 247 | + | 
 | 248 | +    PyStatus status = _PyEval_InitThreads(tstate);  | 
 | 249 | +    if (_PyStatus_EXCEPTION(status)) {  | 
 | 250 | +        Py_ExitStatusException(status);  | 
237 | 251 |     }  | 
238 | 252 | }  | 
239 | 253 | 
 
  | 
 | 
0 commit comments