8000 GH-131238: Core header refactor by markshannon · Pull Request #131250 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-131238: Core header refactor #131250

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 12 commits into from
Mar 17, 2025
Merged
Prev Previous commit
Next Next commit
Refactor no-gil structs and update test
  • Loading branch information
markshannon committed Mar 14, 2025
commit 80fbe3ba5f58abc6cee2e94314f39da29be48d17
24 changes: 2 additions & 22 deletions Include/internal/pycore_index_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,12 @@ extern "C" {

#ifdef Py_GIL_DISABLED

#include "pycore_interp_structs.h"

// This contains code for allocating unique indices in an array. It is used by
// the free-threaded build to assign each thread a globally unique index into
// each code object's thread-local bytecode array.

// A min-heap of indices
typedef struct _PyIndexHeap {
int32_t *values;

// Number of items stored in values
Py_ssize_t size;

// Maximum number of items that can be stored in values
Py_ssize_t capacity;
} _PyIndexHeap;

// An unbounded pool of indices. Indices are allocated starting from 0. They
// may be released back to the pool once they are no longer in use.
typedef struct _PyIndexPool {
PyMutex mutex;

// Min heap of indices available for allocation
_PyIndexHeap free_indices;

// Next index to allocate if no free indices are available
int32_t next_index;
} _PyIndexPool;

// Allocate the smallest available index. Returns -1 on error.
extern int32_t _PyIndexPool_AllocIndex(_PyIndexPool *indices);
Expand Down
6 changes: 0 additions & 6 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ extern "C" {



#ifdef Py_GIL_DISABLED
// This should be prime but otherwise the choice is arbitrary. A larger value
// increases concurrency at the expense of memory.
# define NUM_WEAKREF_LIST_LOCKS 127
#endif

/* interpreter state */

#define _PyInterpreterState_WHENCE_NOTSET -1
Expand Down
56 changes: 56 additions & 0 deletions Include/internal/pycore_interp_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ extern "C" {
#define TYPE_MAX_WATCHERS 8


#ifdef Py_GIL_DISABLED
// This should be prime but otherwise the choice is arbitrary. A larger value
// increases concurrency at the expense of memory.
# define NUM_WEAKREF_LIST_LOCKS 127
#endif

typedef int (*_Py_pending_call_func)(void *);

struct _pending_call {
Expand Down Expand Up @@ -697,6 +703,56 @@ struct _Py_interp_static_objects {

#include "pycore_instruments.h"


#ifdef Py_GIL_DISABLED

// A min-heap of indices
typedef struct _PyIndexHeap {
int32_t *values;

// Number of items stored in values
Py_ssize_t size;

// Maximum number of items that can be stored in values
Py_ssize_t capacity;
} _PyIndexHeap;

// An unbounded pool of indices. Indices are allocated starting from 0. They
// may be released back to the pool once they are no longer in use.
typedef struct _PyIndexPool {
PyMutex mutex;

// Min heap of indices available for allocation
_PyIndexHeap free_indices;

// Next index to allocate if no free indices are available
int32_t next_index;
} _PyIndexPool;

typedef union _Py_unique_id_entry {
// Points to the next free type id, when part of the freelist
union _Py_unique_id_entry *next;

// Stores the object when the id is assigned
PyObject *obj;
} _Py_unique_id_entry;

struct _Py_unique_id_pool {
PyMutex mutex;

// combined table of object with allocated unique ids and unallocated ids.
_Py_unique_id_entry *table;

// Next entry to allocate inside 'table' or NULL
_Py_unique_id_entry *freelist;

// size of 'table'
Py_ssize_t size;
};

#endif


/* PyInterpreterState holds the global state for one of the runtime's
interpreters. Typically the initial (main) interpreter is the only one.

Expand Down
20 changes: 0 additions & 20 deletions Include/internal/pycore_uniqueid.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,6 @@ extern "C" {
// Each entry implicitly represents a unique id based on its offset in the
// table. Non-allocated entries form a free-list via the 'next' pointer.
// Allocated entries store the corresponding PyObject.
typedef union _Py_unique_id_entry {
// Points to the next free type id, when part of the freelist
union _Py_unique_id_entry *next;

// Stores the object when the id is assigned
PyObject *obj;
} _Py_unique_id_entry;

struct _Py_unique_id_pool {
PyMutex mutex;

// combined table of object with allocated unique ids and unallocated ids.
_Py_unique_id_entry *table;

// Next entry to allocate inside 'table' or NULL
_Py_unique_id_entry *freelist;

// size of 'table'
Py_ssize_t size;
};

#define _Py_INVALID_UNIQUE_ID 0

Expand Down
14 changes: 7 additions & 7 deletions Programs/test_frozenmain.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
0