Content-Length: 521589 | pFad | http://github.com/python/cpython/pull/126457/commits/5eb7adb8377d33fa5d818b505929ced2bb2c73e5

71 gh-76785: Minor Cleanup of "Cross-interpreter" Code by ericsnowcurrently · Pull Request #126457 · python/cpython · GitHub
Skip to content

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

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
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
Wrap _PyXIData_registry_t with a new _PyXIData_lookup_t.
  • Loading branch information
ericsnowcurrently committed Nov 5, 2024
commit 5eb7adb8377d33fa5d818b505929ced2bb2c73e5
9 changes: 4 additions & 5 deletions Include/internal/pycore_crossinterp.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ PyAPI_FUNC(void) _PyXIData_Free(_PyXIData_t *data);

typedef int (*xidatafunc)(PyThreadState *tstate, PyObject *, _PyXIData_t *);

typedef struct _xid_lookup_state _PyXIData_lookup_t;

PyAPI_FUNC(xidatafunc) _PyXIData_Lookup(PyObject *);
PyAPI_FUNC(int) _PyObject_CheckXIData(PyObject *);
PyAPI_FUNC(int) _PyObject_GetXIData(PyObject *, _PyXIData_t *);
Expand Down Expand Up @@ -154,22 +156,19 @@ PyAPI_FUNC(void) _PyXIData_Clear( PyInterpreterState *, _PyXIData_t *);

struct _xi_runtime_state {
// builtin types
// XXX Remove this field once we have a tp_* slot.
struct _xidregistry registry;
_PyXIData_lookup_t data_lookup;
};

struct _xi_state {
// heap types
// XXX Remove this field once we have a tp_* slot.
struct _xidregistry registry;
_PyXIData_lookup_t data_lookup;

// heap types
PyObject *PyExc_NotShareableError;
};

extern PyStatus _PyXI_Init(PyInterpreterState *interp);
extern void _PyXI_Fini(PyInterpreterState *interp);

extern PyStatus _PyXI_InitTypes(PyInterpreterState *interp);
extern void _PyXI_FiniTypes(PyInterpreterState *interp);

Expand Down
5 changes: 5 additions & 0 deletions Include/internal/pycore_crossinterp_data_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ struct _xidregistry {

PyAPI_FUNC(int) _PyXIData_RegisterClass(PyTypeObject *, xidatafunc);
PyAPI_FUNC(int) _PyXIData_UnregisterClass(PyTypeObject *);

struct _xid_lookup_state {
// XXX Remove this field once we have a tp_* slot.
struct _xidregistry registry;
};
6 changes: 4 additions & 2 deletions Include/internal/pycore_runtime_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ extern PyTypeObject _PyExc_MemoryError;
.next_id = -1, \
}, \
.xi = { \
.registry = { \
.global = 1, \
.data_lookup = { \
.registry = { \
.global = 1, \
}, \
}, \
}, \
/* A TSS key must be initialized with Py_tss_NEEDS_INIT \
Expand Down
18 changes: 14 additions & 4 deletions Python/crossinterp.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include "pycore_pyerrors.h" // _PyErr_Clear()


#define _PyXI_GET_GLOBAL_STATE(interp) (&(interp)->runtime->xi)
#define _PyXI_GET_STATE(interp) (&(interp)->xi)


/**************/
/* exceptions */
/**************/
Expand Down Expand Up @@ -62,8 +66,8 @@ _Py_CallInInterpreterAndRawFree(PyInterpreterState *interp,
alternative would be to add a tp_* slot for a class's
xidatafunc. It would be simpler and more efficient. */

static void xid_lookup_init(PyInterpreterState *);
static void xid_lookup_fini(PyInterpreterState *);
static void xid_lookup_init(_PyXIData_lookup_t *);
static void xid_lookup_fini(_PyXIData_lookup_t *);
static xidatafunc lookup_getdata(PyInterpreterState *, PyObject *);
#include "crossinterp_data_lookup.h"

Expand Down Expand Up @@ -1773,7 +1777,10 @@ PyStatus
_PyXI_Init(PyInterpreterState *interp)
{
// Initialize the XID lookup state (e.g. registry).
xid_lookup_init(interp);
if (_Py_IsMainInterpreter(interp)) {
xid_lookup_init(&_PyXI_GET_GLOBAL_STATE(interp)->data_lookup);
}
xid_lookup_init(&_PyXI_GET_STATE(interp)->data_lookup);

// Initialize exceptions (heap types).
if (_init_not_shareable_error_type(interp) < 0) {
Expand All @@ -1793,7 +1800,10 @@ _PyXI_Fini(PyInterpreterState *interp)
_fini_not_shareable_error_type(interp);

// Finalize the XID lookup state (e.g. registry).
xid_lookup_fini(interp);
xid_lookup_fini(&_PyXI_GET_STATE(interp)->data_lookup);
if (_Py_IsMainInterpreter(interp)) {
xid_lookup_fini(&_PyXI_GET_GLOBAL_STATE(interp)->data_lookup);
}
}

PyStatus
Expand Down
46 changes: 22 additions & 24 deletions Python/crossinterp_data_lookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,25 @@ typedef struct _xidregitem dlregitem_t;


// forward

static void _xidregistry_init(dlregistry_t *);
static void _xidregistry_fini(dlregistry_t *);
static xidatafunc _lookup_getdata_from_registry(PyInterpreterState *, PyObject *);


/* used in crossinterp.c */

static void
xid_lookup_init(_PyXIData_lookup_t *state)
{
_xidregistry_init(&state->registry);
}

static void
xid_lookup_fini(_PyXIData_lookup_t *state)
{
_xidregistry_fini(&state->registry);
}

static xidatafunc
lookup_getdata(PyInterpreterState *interp, PyObject *obj)
{
Expand All @@ -18,6 +34,9 @@ lookup_getdata(PyInterpreterState *interp, PyObject *obj)
return _lookup_getdata_from_registry(interp, obj);
}


/* exported API */

xidatafunc
_PyXIData_Lookup(PyObject *obj)
{
Expand Down Expand Up @@ -67,27 +86,6 @@ _xidregistry_fini(dlregistry_t *registry)
_xidregistry_clear(registry);
}

static inline struct _xidregistry * _get_global_xidregistry(_PyRuntimeState *);
static inline struct _xidregistry * _get_xidregistry(PyInterpreterState *);

static void
xid_lookup_init(PyInterpreterState *interp)
{
if (_Py_IsMainInterpreter(interp)) {
_xidregistry_init(_get_global_xidregistry(interp->runtime));
}
_xidregistry_init(_get_xidregistry(interp));
}

static void
xid_lookup_fini(PyInterpreterState *interp)
{
_xidregistry_fini(_get_xidregistry(interp));
if (_Py_IsMainInterpreter(interp)) {
_xidregistry_fini(_get_global_xidregistry(interp->runtime));
}
}


/* registry thread safety */

Expand All @@ -114,13 +112,13 @@ _xidregistry_unlock(dlregistry_t *registry)
static inline dlregistry_t *
_get_global_xidregistry(_PyRuntimeState *runtime)
{
return &runtime->xi.registry;
return &runtime->xi.data_lookup.registry;
}

static inline dlregistry_t *
_get_xidregistry(PyInterpreterState *interp)
{
return &interp->xi.registry;
return &interp->xi.data_lookup.registry;
}

static inline dlregistry_t *
Expand Down
2 changes: 1 addition & 1 deletion Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ _Py_COMP_DIAG_POP
#define LOCKS_INIT(runtime) \
{ \
&(runtime)->interpreters.mutex, \
&(runtime)->xi.registry.mutex, \
&(runtime)->xi.data_lookup.registry.mutex, \
&(runtime)->unicode_state.ids.mutex, \
&(runtime)->imports.extensions.mutex, \
&(runtime)->ceval.pending_mainthread.mutex, \
Expand Down
Loading








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/python/cpython/pull/126457/commits/5eb7adb8377d33fa5d818b505929ced2bb2c73e5

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy