Skip to content

bpo-39877: Py_Initialize() pass tstate to PyEval_InitThreads() #18884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ extern PyObject *_PyEval_EvalCode(
PyObject *kwdefs, PyObject *closure,
PyObject *name, PyObject *qualname);

extern PyStatus _PyEval_InitThreads(PyThreadState *tstate);

#ifdef __cplusplus
}
#endif
Expand Down
30 changes: 22 additions & 8 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "pycore_call.h"
#include "pycore_ceval.h"
#include "pycore_code.h"
#include "pycore_initconfig.h"
#include "pycore_object.h"
#include "pycore_pyerrors.h"
#include "pycore_pylifecycle.h"
Expand Down Expand Up @@ -214,26 +215,39 @@ PyEval_ThreadsInitialized(void)
return gil_created(&runtime->ceval.gil);
}

void
PyEval_InitThreads(void)
PyStatus
_PyEval_InitThreads(PyThreadState *tstate)
{
_PyRuntimeState *runtime = &_PyRuntime;
struct _ceval_runtime_state *ceval = &runtime->ceval;
if (tstate == NULL) {
return _PyStatus_ERR("tstate is NULL");
}

struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
struct _gil_runtime_state *gil = &ceval->gil;
if (gil_created(gil)) {
return;
return _PyStatus_OK();
}

PyThread_init_thread();
create_gil(gil);
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
ensure_tstate_not_null(__func__, tstate);
take_gil(ceval, tstate);

struct _pending_calls *pending = &ceval->pending;
pending->lock = PyThread_allocate_lock();
if (pending->lock == NULL) {
Py_FatalError("Can't initialize threads for pending calls");
return _PyStatus_NO_MEMORY();
}
return _PyStatus_OK();
}

void
PyEval_InitThreads(void)
{
PyThreadState *tstate = _PyThreadState_GET();

PyStatus status = _PyEval_InitThreads(tstate);
if (_PyStatus_EXCEPTION(status)) {
Py_ExitStatusException(status);
}
}

Expand Down
5 changes: 4 additions & 1 deletion Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,10 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
_PyGILState_Init(tstate);

/* Create the GIL */
PyEval_InitThreads();
status = _PyEval_InitThreads(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}

*tstate_p = tstate;
return _PyStatus_OK();
Expand Down