Skip to content
Merged
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
Next Next commit
_PyCrossInterpreterData -> _PyXIData_t
  • Loading branch information
ericsnowcurrently committed Nov 5, 2024
commit f513a3c1d321df52e54135b0d1dc5432a883c2cd
26 changes: 13 additions & 13 deletions Include/internal/pycore_crossinterp.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ extern int _Py_CallInInterpreterAndRawFree(
/* cross-interpreter data */
/**************************/

typedef struct _xid _PyCrossInterpreterData;
typedef PyObject *(*xid_newobjectfunc)(_PyCrossInterpreterData *);
typedef struct _xid _PyXIData_t;
typedef PyObject *(*xid_newobjectfunc)(_PyXIData_t *);
typedef void (*xid_freefunc)(void *);

// _PyCrossInterpreterData is similar to Py_buffer as an effectively
// _PyXIData_t is similar to Py_buffer as an effectively
// opaque struct that holds data outside the object machinery. This
// is necessary to pass safely between interpreters in the same process.
struct _xid {
Expand Down Expand Up @@ -84,8 +84,8 @@ struct _xid {
xid_freefunc free;
};

PyAPI_FUNC(_PyCrossInterpreterData *) _PyCrossInterpreterData_New(void);
PyAPI_FUNC(void) _PyCrossInterpreterData_Free(_PyCrossInterpreterData *data);
PyAPI_FUNC(_PyXIData_t *) _PyCrossInterpreterData_New(void);
PyAPI_FUNC(void) _PyCrossInterpreterData_Free(_PyXIData_t *data);

#define _PyCrossInterpreterData_DATA(DATA) ((DATA)->data)
#define _PyCrossInterpreterData_OBJ(DATA) ((DATA)->obj)
Expand All @@ -96,15 +96,15 @@ PyAPI_FUNC(void) _PyCrossInterpreterData_Free(_PyCrossInterpreterData *data);
/* defining cross-interpreter data */

PyAPI_FUNC(void) _PyCrossInterpreterData_Init(
_PyCrossInterpreterData *data,
_PyXIData_t *data,
PyInterpreterState *interp, void *shared, PyObject *obj,
xid_newobjectfunc new_object);
PyAPI_FUNC(int) _PyCrossInterpreterData_InitWithSize(
_PyCrossInterpreterData *,
_PyXIData_t *,
PyInterpreterState *interp, const size_t, PyObject *,
xid_newobjectfunc);
PyAPI_FUNC(void) _PyCrossInterpreterData_Clear(
PyInterpreterState *, _PyCrossInterpreterData *);
PyInterpreterState *, _PyXIData_t *);

// Normally the Init* functions are sufficient. The only time
// additional initialization might be needed is to set the "free" func,
Expand All @@ -129,10 +129,10 @@ PyAPI_FUNC(void) _PyCrossInterpreterData_Clear(
/* using cross-interpreter data */

PyAPI_FUNC(int) _PyObject_CheckCrossInterpreterData(PyObject *);
PyAPI_FUNC(int) _PyObject_GetCrossInterpreterData(PyObject *, _PyCrossInterpreterData *);
PyAPI_FUNC(PyObject *) _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *);
PyAPI_FUNC(int) _PyCrossInterpreterData_Release(_PyCrossInterpreterData *);
PyAPI_FUNC(int) _PyCrossInterpreterData_ReleaseAndRawFree(_PyCrossInterpreterData *);
PyAPI_FUNC(int) _PyObject_GetCrossInterpreterData(PyObject *, _PyXIData_t *);
PyAPI_FUNC(PyObject *) _PyCrossInterpreterData_NewObject(_PyXIData_t *);
PyAPI_FUNC(int) _PyCrossInterpreterData_Release(_PyXIData_t *);
PyAPI_FUNC(int) _PyCrossInterpreterData_ReleaseAndRawFree(_PyXIData_t *);


/* cross-interpreter data registry */
Expand All @@ -142,7 +142,7 @@ PyAPI_FUNC(int) _PyCrossInterpreterData_ReleaseAndRawFree(_PyCrossInterpreterDat
// crossinterpdatafunc. It would be simpler and more efficient.

typedef int (*crossinterpdatafunc)(PyThreadState *tstate, PyObject *,
_PyCrossInterpreterData *);
_PyXIData_t *);

struct _xidregitem;

Expand Down
44 changes: 20 additions & 24 deletions Modules/_interpchannelsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#endif

#include "Python.h"
#include "pycore_crossinterp.h" // struct _xid
#include "pycore_crossinterp.h" // _PyXIData_t
#include "pycore_interp.h" // _PyInterpreterState_LookUpID()
#include "pycore_pystate.h" // _PyInterpreterState_GetIDObject()

Expand Down Expand Up @@ -59,7 +59,7 @@ _globals (static struct globals):
first (struct _channelitem *):
next (struct _channelitem *):
...
data (_PyCrossInterpreterData *):
data (_PyXIData_t *):
data (void *)
obj (PyObject *)
interpid (int64_t)
Expand All @@ -80,7 +80,7 @@ The above state includes the following allocations by the module:
* 1 struct _channelqueue
* for each item in each channel:
* 1 struct _channelitem
* 1 _PyCrossInterpreterData
* 1 _PyXIData_t

The only objects in that global state are the references held by each
channel's queue, which are safely managed via the _PyCrossInterpreterData_*()
Expand All @@ -102,7 +102,7 @@ API.. The module does not create any objects that are shared globally.
#define XID_FREE 2

static int
_release_xid_data(_PyCrossInterpreterData *data, int flags)
_release_xid_data(_PyXIData_t *data, int flags)
{
int ignoreexc = flags & XID_IGNORE_EXC;
PyObject *exc;
Expand Down Expand Up @@ -519,7 +519,7 @@ typedef struct _channelitem {
This is necessary because item->data might be NULL,
meaning the interpreter has been destroyed. */
int64_t interpid;
_PyCrossInterpreterData *data;
_PyXIData_t *data;
_waiting_t *waiting;
int unboundop;
struct _channelitem *next;
Expand All @@ -533,7 +533,7 @@ _channelitem_ID(_channelitem *item)

static void
_channelitem_init(_channelitem *item,
int64_t interpid, _PyCrossInterpreterData *data,
int64_t interpid, _PyXIData_t *data,
_waiting_t *waiting, int unboundop)
{
if (interpid < 0) {
Expand Down Expand Up @@ -580,7 +580,7 @@ _channelitem_clear(_channelitem *item)
}

static _channelitem *
_channelitem_new(int64_t interpid, _PyCrossInterpreterData *data,
_channelitem_new(int64_t interpid, _PyXIData_t *data,
_waiting_t *waiting, int unboundop)
{
_channelitem *item = GLOBAL_MALLOC(_channelitem);
Expand Down Expand Up @@ -611,7 +611,7 @@ _channelitem_free_all(_channelitem *item)

static void
_channelitem_popped(_channelitem *item,
_PyCrossInterpreterData **p_data, _waiting_t **p_waiting,
_PyXIData_t **p_data, _waiting_t **p_waiting,
int *p_unboundop)
{
assert(item->waiting == NULL || item->waiting->status == WAITING_ACQUIRED);
Expand Down Expand Up @@ -691,7 +691,7 @@ _channelqueue_free(_channelqueue *queue)

static int
_channelqueue_put(_channelqueue *queue,
int64_t interpid, _PyCrossInterpreterData *data,
int64_t interpid, _PyXIData_t *data,
_waiting_t *waiting, int unboundop)
{
_channelitem *item = _channelitem_new(interpid, data, waiting, unboundop);
Expand All @@ -717,7 +717,7 @@ _channelqueue_put(_channelqueue *queue,

static int
_channelqueue_get(_channelqueue *queue,
_PyCrossInterpreterData **p_data, _waiting_t **p_waiting,
_PyXIData_t **p_data, _waiting_t **p_waiting,
int *p_unboundop)
{
_channelitem *item = queue->first;
Expand Down Expand Up @@ -769,7 +769,7 @@ _channelqueue_find(_channelqueue *queue, _channelitem_id_t itemid,

static void
_channelqueue_remove(_channelqueue *queue, _channelitem_id_t itemid,
_PyCrossInterpreterData **p_data, _waiting_t **p_waiting)
_PyXIData_t **p_data, _waiting_t **p_waiting)
{
_channelitem *prev = NULL;
_channelitem *item = NULL;
Expand Down Expand Up @@ -1128,8 +1128,7 @@ _channel_free(_channel_state *chan)

static int
_channel_add(_channel_state *chan, int64_t interpid,
_PyCrossInterpreterData *data, _waiting_t *waiting,
int unboundop)
_PyXIData_t *data, _waiting_t *waiting, int unboundop)
{
int res = -1;
PyThread_acquire_lock(chan->mutex, WAIT_LOCK);
Expand All @@ -1156,8 +1155,7 @@ _channel_add(_channel_state *chan, int64_t interpid,

static int
_channel_next(_channel_state *chan, int64_t interpid,
_PyCrossInterpreterData **p_data, _waiting_t **p_waiting,
int *p_unboundop)
_PyXIData_t **p_data, _waiting_t **p_waiting, int *p_unboundop)
{
int err = 0;
PyThread_acquire_lock(chan->mutex, WAIT_LOCK);
Expand Down Expand Up @@ -1193,7 +1191,7 @@ _channel_next(_channel_state *chan, int64_t interpid,
static void
_channel_remove(_channel_state *chan, _channelitem_id_t itemid)
{
_PyCrossInterpreterData *data = NULL;
_PyXIData_t *data = NULL;
_waiting_t *waiting = NULL;

PyThread_acquire_lock(chan->mutex, WAIT_LOCK);
Expand Down Expand Up @@ -1776,7 +1774,7 @@ channel_send(_channels *channels, int64_t cid, PyObject *obj,
}

// Convert the object to cross-interpreter data.
_PyCrossInterpreterData *data = GLOBAL_MALLOC(_PyCrossInterpreterData);
_PyXIData_t *data = GLOBAL_MALLOC(_PyXIData_t);
if (data == NULL) {
PyThread_release_lock(mutex);
return -1;
Expand Down Expand Up @@ -1904,7 +1902,7 @@ channel_recv(_channels *channels, int64_t cid, PyObject **res, int *p_unboundop)
// Past this point we are responsible for releasing the mutex.

// Pop off the next item from the channel.
_PyCrossInterpreterData *data = NULL;
_PyXIData_t *data = NULL;
_waiting_t *waiting = NULL;
err = _channel_next(chan, interpid, &data, &waiting, p_unboundop);
PyThread_release_lock(mutex);
Expand Down Expand Up @@ -2545,7 +2543,7 @@ struct _channelid_xid {
};

static PyObject *
_channelid_from_xid(_PyCrossInterpreterData *data)
_channelid_from_xid(_PyXIData_t *data)
{
struct _channelid_xid *xid = \
(struct _channelid_xid *)_PyCrossInterpreterData_DATA(data);
Expand Down Expand Up @@ -2594,8 +2592,7 @@ _channelid_from_xid(_PyCrossInterpreterData *data)
}

static int
_channelid_shared(PyThreadState *tstate, PyObject *obj,
_PyCrossInterpreterData *data)
_channelid_shared(PyThreadState *tstate, PyObject *obj, _PyXIData_t *data)
{
if (_PyCrossInterpreterData_InitWithSize(
data, tstate->interp, sizeof(struct _channelid_xid), obj,
Expand Down Expand Up @@ -2745,7 +2742,7 @@ _get_current_channelend_type(int end)
}

static PyObject *
_channelend_from_xid(_PyCrossInterpreterData *data)
_channelend_from_xid(_PyXIData_t *data)
{
channelid *cidobj = (channelid *)_channelid_from_xid(data);
if (cidobj == NULL) {
Expand All @@ -2762,8 +2759,7 @@ _channelend_from_xid(_PyCrossInterpreterData *data)
}

static int
_channelend_shared(PyThreadState *tstate, PyObject *obj,
_PyCrossInterpreterData *data)
_channelend_shared(PyThreadState *tstate, PyObject *obj, _PyXIData_t *data)
{
PyObject *cidobj = PyObject_GetAttrString(obj, "_id");
if (cidobj == NULL) {
Expand Down
30 changes: 13 additions & 17 deletions Modules/_interpqueuesmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#endif

#include "Python.h"
#include "pycore_crossinterp.h" // struct _xid
#include "pycore_crossinterp.h" // _PyXIData_t

#define REGISTERS_HEAP_TYPES
#define HAS_UNBOUND_ITEMS
Expand All @@ -30,7 +30,7 @@
#define XID_FREE 2

static int
_release_xid_data(_PyCrossInterpreterData *data, int flags)
_release_xid_data(_PyXIData_t *data, int flags)
{
int ignoreexc = flags & XID_IGNORE_EXC;
PyObject *exc;
Expand Down Expand Up @@ -400,16 +400,15 @@ typedef struct _queueitem {
This is necessary because item->data might be NULL,
meaning the interpreter has been destroyed. */
int64_t interpid;
_PyCrossInterpreterData *data;
_PyXIData_t *data;
int fmt;
int unboundop;
struct _queueitem *next;
} _queueitem;

static void
_queueitem_init(_queueitem *item,
int64_t interpid, _PyCrossInterpreterData *data,
int fmt, int unboundop)
int64_t interpid, _PyXIData_t *data, int fmt, int unboundop)
{
if (interpid < 0) {
interpid = _get_interpid(data);
Expand Down Expand Up @@ -447,8 +446,7 @@ _queueitem_clear(_queueitem *item)
}

static _queueitem *
_queueitem_new(int64_t interpid, _PyCrossInterpreterData *data,
int fmt, int unboundop)
_queueitem_new(int64_t interpid, _PyXIData_t *data, int fmt, int unboundop)
{
_queueitem *item = GLOBAL_MALLOC(_queueitem);
if (item == NULL) {
Expand Down Expand Up @@ -478,7 +476,7 @@ _queueitem_free_all(_queueitem *item)

static void
_queueitem_popped(_queueitem *item,
_PyCrossInterpreterData **p_data, int *p_fmt, int *p_unboundop)
_PyXIData_t **p_data, int *p_fmt, int *p_unboundop)
{
*p_data = item->data;
*p_fmt = item->fmt;
Expand Down Expand Up @@ -633,7 +631,7 @@ _queue_unlock(_queue *queue)
}

static int
_queue_add(_queue *queue, int64_t interpid, _PyCrossInterpreterData *data,
_queue_add(_queue *queue, int64_t interpid, _PyXIData_t *data,
int fmt, int unboundop)
{
int err = _queue_lock(queue);
Expand Down Expand Up @@ -671,7 +669,7 @@ _queue_add(_queue *queue, int64_t interpid, _PyCrossInterpreterData *data,

static int
_queue_next(_queue *queue,
_PyCrossInterpreterData **p_data, int *p_fmt, int *p_unboundop)
_PyXIData_t **p_data, int *p_fmt, int *p_unboundop)
{
int err = _queue_lock(queue);
if (err < 0) {
Expand Down Expand Up @@ -1138,7 +1136,7 @@ queue_put(_queues *queues, int64_t qid, PyObject *obj, int fmt, int unboundop)
assert(queue != NULL);

// Convert the object to cross-interpreter data.
_PyCrossInterpreterData *data = GLOBAL_MALLOC(_PyCrossInterpreterData);
_PyXIData_t *data = GLOBAL_MALLOC(_PyXIData_t);
if (data == NULL) {
_queue_unmark_waiter(queue, queues->mutex);
return -1;
Expand Down Expand Up @@ -1184,7 +1182,7 @@ queue_get(_queues *queues, int64_t qid,
assert(queue != NULL);

// Pop off the next item from the queue.
_PyCrossInterpreterData *data = NULL;
_PyXIData_t *data = NULL;
err = _queue_next(queue, &data, p_fmt, p_unboundop);
_queue_unmark_waiter(queue, queues->mutex);
if (err != 0) {
Expand Down Expand Up @@ -1258,8 +1256,7 @@ queue_get_count(_queues *queues, int64_t qid, Py_ssize_t *p_count)

/* external Queue objects ***************************************************/

static int _queueobj_shared(PyThreadState *,
PyObject *, _PyCrossInterpreterData *);
static int _queueobj_shared(PyThreadState *, PyObject *, _PyXIData_t *);

static int
set_external_queue_type(module_state *state, PyTypeObject *queue_type)
Expand Down Expand Up @@ -1339,7 +1336,7 @@ _queueid_xid_free(void *data)
}

static PyObject *
_queueobj_from_xid(_PyCrossInterpreterData *data)
_queueobj_from_xid(_PyXIData_t *data)
{
int64_t qid = *(int64_t *)_PyCrossInterpreterData_DATA(data);
PyObject *qidobj = PyLong_FromLongLong(qid);
Expand Down Expand Up @@ -1367,8 +1364,7 @@ _queueobj_from_xid(_PyCrossInterpreterData *data)
}

static int
_queueobj_shared(PyThreadState *tstate, PyObject *queueobj,
_PyCrossInterpreterData *data)
_queueobj_shared(PyThreadState *tstate, PyObject *queueobj, _PyXIData_t *data)
{
PyObject *qidobj = PyObject_GetAttrString(queueobj, "_id");
if (qidobj == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion Modules/_interpreters_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ clear_xid_class(PyTypeObject *cls)


static inline int64_t
_get_interpid(_PyCrossInterpreterData *data)
_get_interpid(_PyXIData_t *data)
{
int64_t interpid;
if (data != NULL) {
Expand Down
Loading