gh-76785: Minor Cleanup of "Cross-interpreter" Code (gh-126457)

The primary objective here is to allow some later changes to be cleaner. Mostly this involves renaming things and moving a few things around.

* CrossInterpreterData -> XIData
* crossinterpdatafunc -> xidatafunc
* split out pycore_crossinterp_data_registry.h
* add _PyXIData_lookup_t
This commit is contained in:
Eric Snow 2024-11-07 09:32:42 -07:00 committed by GitHub
parent 3d9f9ae5a7
commit 9357fdcaf0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 343 additions and 337 deletions

View file

@ -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
@ -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;
@ -39,10 +39,10 @@ _release_xid_data(_PyCrossInterpreterData *data, int flags)
}
int res;
if (flags & XID_FREE) {
res = _PyCrossInterpreterData_ReleaseAndRawFree(data);
res = _PyXIData_ReleaseAndRawFree(data);
}
else {
res = _PyCrossInterpreterData_Release(data);
res = _PyXIData_Release(data);
}
if (res < 0) {
/* The owning interpreter is already destroyed. */
@ -400,7 +400,7 @@ 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;
@ -408,16 +408,15 @@ typedef struct _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);
}
else {
assert(data == NULL
|| _PyCrossInterpreterData_INTERPID(data) < 0
|| interpid == _PyCrossInterpreterData_INTERPID(data));
|| _PyXIData_INTERPID(data) < 0
|| interpid == _PyXIData_INTERPID(data));
}
assert(check_unbound(unboundop));
*item = (_queueitem){
@ -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) {
@ -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;
@ -498,7 +496,7 @@ _queueitem_clear_interpreter(_queueitem *item)
assert(item->unboundop != UNBOUND_REMOVE);
return 0;
}
assert(_PyCrossInterpreterData_INTERPID(item->data) == item->interpid);
assert(_PyXIData_INTERPID(item->data) == item->interpid);
switch (item->unboundop) {
case UNBOUND_REMOVE:
@ -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);
@ -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) {
@ -1138,17 +1136,17 @@ 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;
}
if (_PyObject_GetCrossInterpreterData(obj, data) != 0) {
if (_PyObject_GetXIData(obj, data) != 0) {
_queue_unmark_waiter(queue, queues->mutex);
GLOBAL_FREE(data);
return -1;
}
assert(_PyCrossInterpreterData_INTERPID(data) == \
assert(_PyXIData_INTERPID(data) == \
PyInterpreterState_GetID(PyInterpreterState_Get()));
// Add the data to the queue.
@ -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) {
@ -1196,7 +1194,7 @@ queue_get(_queues *queues, int64_t qid,
}
// Convert the data back to an object.
PyObject *obj = _PyCrossInterpreterData_NewObject(data);
PyObject *obj = _PyXIData_NewObject(data);
if (obj == NULL) {
assert(PyErr_Occurred());
// It was allocated in queue_put(), so we free it.
@ -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)
@ -1339,9 +1336,9 @@ _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);
int64_t qid = *(int64_t *)_PyXIData_DATA(data);
PyObject *qidobj = PyLong_FromLongLong(qid);
if (qidobj == NULL) {
return NULL;
@ -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) {
@ -1388,9 +1384,8 @@ _queueobj_shared(PyThreadState *tstate, PyObject *queueobj,
if (raw == NULL) {
return -1;
}
_PyCrossInterpreterData_Init(data, tstate->interp, raw, NULL,
_queueobj_from_xid);
_PyCrossInterpreterData_SET_FREE(data, _queueid_xid_free);
_PyXIData_Init(data, tstate->interp, raw, NULL, _queueobj_from_xid);
_PyXIData_SET_FREE(data, _queueid_xid_free);
return 0;
}