8000 Revert: bpo-33608: Factor out a private, per-interpreter _Py_AddPendingCall(). (GH-11617) by vstinner · Pull Request #12159 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Revert: bpo-33608: Factor out a private, per-interpreter _Py_AddPendingCall(). (GH-11617) #12159

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 4 commits into from
Mar 4, 2019
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Revert "bpo-33608: Use _Py_AddPendingCall() in _PyCrossInterpreterDat…
…a_Release(). (gh-12024)"

This reverts commit b05b711.
  • Loading branch information
vstinner committed Mar 4, 2019
commit d161eac47e8231ff549f0aab783e9f11b52ef339
81 changes: 39 additions & 42 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,37 +328,26 @@ PyInterpreterState_GetID(PyInterpreterState *interp)
}


static PyInterpreterState *
interp_look_up_id(PY_INT64_T requested_id)
PyInterpreterState *
_PyInterpreterState_LookUpID(PY_INT64_T requested_id)
{
if (requested_id < 0)
goto error;

PyInterpreterState *interp = PyInterpreterState_Head();
while (interp != NULL) {
PY_INT64_T id = PyInterpreterState_GetID(interp);
if (id < 0) {
if (id < 0)
return NULL;
}
if (requested_id == id) {
if (requested_id == id)
return interp;
}
interp = PyInterpreterState_Next(interp);
}
return NULL;
}

PyInterpreterState *
_PyInterpreterState_LookUpID(PY_INT64_T requested_id)
{
PyInterpreterState *interp = NULL;
if (requested_id >= 0) {
HEAD_UNLOCK();
interp = interp_look_up_id(requested_id);
HEAD_UNLOCK();
}
if (interp == NULL && !PyErr_Occurred()) {
PyErr_Format(PyExc_RuntimeError,
"unrecognized interpreter ID %lld", requested_id);
}
return interp;
error:
PyErr_Format(PyExc_RuntimeError,
"unrecognized interpreter ID %lld", requested_id);
return NULL;
}


Expand Down Expand Up @@ -1291,16 +1280,38 @@ _PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
return 0;
}

static int
static void
_release_xidata(void *arg)
{
_PyCrossInterpreterData *data = (_PyCrossInterpreterData *)arg;
if (data->free != NULL) {
data->free(data->data);
}
Py_XDECREF(data->obj);
PyMem_Free(data);
return 0;
}

static void
_call_in_interpreter(PyInterpreterState *interp,
void (*func)(void *), void *arg)
{
/* We would use Py_AddPendingCall() if it weren't specific to the
* main interpreter (see bpo-33608). In the meantime we take a
* naive approach.
*/
PyThreadState *save_tstate = NULL;
if (interp != _PyInterpreterState_Get()) {
// XXX Using the "head" thread isn't strictly correct.
PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
// XXX Possible GILState issues?
save_tstate = PyThreadState_Swap(tstate);
}

func(arg);

// Switch back.
if (save_tstate != NULL) {
PyThreadState_Swap(save_tstate);
}
}

void
Expand All @@ -1311,7 +1322,7 @@ _PyCrossInterpreterData_Release(_PyCrossInterpreterData *data)
return;
}

// Get the original interpreter.
// Switch to the original interpreter.
PyInterpreterState *interp = _PyInterpreterState_LookUpID(data->interp);
if (interp == NULL) {
// The intepreter was already destroyed.
Expand All @@ -1320,24 +1331,10 @@ _PyCrossInterpreterData_Release(_PyCrossInterpreterData *data)
}
return;
}
// XXX There's an ever-so-slight race here...
if (interp->finalizing) {
// XXX Someone leaked some memory...
return;
}

// "Release" the data and/or the object.
_PyCrossInterpreterData *copied = PyMem_Malloc(sizeof(_PyCrossInterpreterData));
if (copied == NULL) {
PyErr_SetString(PyExc_MemoryError,
"Not enough memory to preserve cross-interpreter data");
PyErr_Print();
return;
}
memcpy(copied, data, sizeof(_PyCrossInterpreterData));
if (_Py_AddPendingCall(interp, 0, _release_xidata, copied) != 0) {
// XXX Queue full or couldn't get lock. Try again somehow?
}
// XXX Use _Py_AddPendingCall().
_call_in_interpreter(interp, _release_xidata, data);
}

PyObject *
Expand Down
0