Skip to content
Closed
Show file tree
Hide file tree
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
Add _Py_DECREF_in_interpreter().
  • Loading branch information
ericsnowcurrently committed May 1, 2020
commit 7a80a2914617d0d7ed1885877038553cc93d0be6
4 changes: 4 additions & 0 deletions Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ PyAPI_FUNC(const PyConfig*) _PyInterpreterState_GetConfig(PyInterpreterState *in
PyAPI_FUNC(const PyConfig*) _Py_GetConfig(void);


/* cross-interpreter operations */

PyAPI_FUNC(int) _Py_DECREF_in_interpreter(PyInterpreterState *, PyObject *);

/* cross-interpreter data */

struct _xid;
Expand Down
69 changes: 44 additions & 25 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,46 @@ PyGILState_Release(PyGILState_STATE oldstate)
/* cross-interpreter data */
/**************************/

/* cross-interpreter operations */

static void
_call_in_interpreter(struct _gilstate_runtime_state *gilstate,
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 != _PyRuntimeGILState_GetThreadState(gilstate)->interp) {
// XXX Using the "head" thread isn't strictly correct.
PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
// XXX Possible GILState issues?
save_tstate = _PyThreadState_Swap(gilstate, tstate);
}

func(arg);

// Switch back.
if (save_tstate != NULL) {
_PyThreadState_Swap(gilstate, save_tstate);
}
}

static int
_decref_pyobj(void *obj)
{
Py_DECREF(obj);
return 0;
}

int
_Py_DECREF_in_interpreter(PyInterpreterState *interp, PyObject *obj)
{
return _PyEval_AddPendingCall(interp, _decref_pyobj, obj);
}

/* cross-interpreter data */

crossinterpdatafunc _PyCrossInterpreterData_Lookup(PyObject *);
Expand Down Expand Up @@ -1539,31 +1579,6 @@ _release_xidata(void *arg)
Py_XDECREF(data->obj);
}

static void
_call_in_interpreter(struct _gilstate_runtime_state *gilstate,
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 != _PyRuntimeGILState_GetThreadState(gilstate)->interp) {
// XXX Using the "head" thread isn't strictly correct.
PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
// XXX Possible GILState issues?
save_tstate = _PyThreadState_Swap(gilstate, tstate);
}

func(arg);

// Switch back.
if (save_tstate != NULL) {
_PyThreadState_Swap(gilstate, save_tstate);
}
}

void
_PyCrossInterpreterData_Release(_PyCrossInterpreterData *data)
{
Expand Down Expand Up @@ -1796,6 +1811,10 @@ _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry)
}
}

/******************************/
/* END cross-interpreter data */
/******************************/


_PyFrameEvalFunction
_PyInterpreterState_GetEvalFrameFunc(PyInterpreterState *interp)
Expand Down