2020-04-13 06:45:21 -03:00
|
|
|
#ifndef Py_INTERNAL_RUNTIME_H
|
|
|
|
#define Py_INTERNAL_RUNTIME_H
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef Py_BUILD_CORE
|
|
|
|
# error "this header requires Py_BUILD_CORE define"
|
|
|
|
#endif
|
|
|
|
|
2023-08-21 15:05:59 -03:00
|
|
|
#include "pycore_atexit.h" // struct _atexit_runtime_state
|
2022-12-08 20:17:20 -04:00
|
|
|
#include "pycore_ceval_state.h" // struct _ceval_runtime_state
|
2023-10-30 19:53:10 -03:00
|
|
|
#include "pycore_crossinterp.h" // struct _xidregistry
|
2022-12-12 12:58:46 -04:00
|
|
|
#include "pycore_faulthandler.h" // struct _faulthandler_runtime_state
|
2023-08-21 15:05:59 -03:00
|
|
|
#include "pycore_floatobject.h" // struct _Py_float_runtime_state
|
2022-11-11 17:16:28 -04:00
|
|
|
#include "pycore_import.h" // struct _import_runtime_state
|
2022-03-03 18:08:07 -04:00
|
|
|
#include "pycore_interp.h" // PyInterpreterState
|
2023-03-20 13:03:04 -03:00
|
|
|
#include "pycore_object_state.h" // struct _py_object_runtime_state
|
2022-12-07 18:56:31 -04:00
|
|
|
#include "pycore_parser.h" // struct _parser_runtime_state
|
2022-11-16 12:37:14 -04:00
|
|
|
#include "pycore_pyhash.h" // struct pyhash_runtime_state
|
2023-08-21 15:05:59 -03:00
|
|
|
#include "pycore_pymem.h" // struct _pymem_allocators
|
2022-12-08 20:50:58 -04:00
|
|
|
#include "pycore_pythread.h" // struct _pythread_runtime_state
|
2022-12-12 19:50:19 -04:00
|
|
|
#include "pycore_signal.h" // struct _signals_runtime_state
|
2022-12-12 11:44:23 -04:00
|
|
|
#include "pycore_tracemalloc.h" // struct _tracemalloc_runtime_state
|
2023-08-21 15:05:59 -03:00
|
|
|
#include "pycore_typeobject.h" // struct _types_runtime_state
|
|
|
|
#include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_state
|
2021-11-03 13:22:32 -03:00
|
|
|
|
2022-08-16 15:22:14 -03:00
|
|
|
struct _getargs_runtime_state {
|
2022-11-15 12:45:11 -04:00
|
|
|
struct _PyArg_Parser *static_parsers;
|
2022-08-16 15:22:14 -03:00
|
|
|
};
|
2022-01-12 19:28:46 -04:00
|
|
|
|
2020-04-13 06:45:21 -03:00
|
|
|
/* GIL state */
|
|
|
|
|
|
|
|
struct _gilstate_runtime_state {
|
|
|
|
/* bpo-26558: Flag to disable PyGILState_Check().
|
|
|
|
If set to non-zero, PyGILState_Check() always return 1. */
|
|
|
|
int check_enabled;
|
|
|
|
/* The single PyInterpreterState used by this process'
|
|
|
|
GILState implementation
|
|
|
|
*/
|
|
|
|
/* TODO: Given interp_main, it may be possible to kill this ref */
|
|
|
|
PyInterpreterState *autoInterpreterState;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Runtime audit hook state */
|
|
|
|
|
|
|
|
typedef struct _Py_AuditHookEntry {
|
|
|
|
struct _Py_AuditHookEntry *next;
|
|
|
|
Py_AuditHookFunction hookCFunction;
|
|
|
|
void *userData;
|
|
|
|
} _Py_AuditHookEntry;
|
|
|
|
|
2023-07-11 16:35:41 -03:00
|
|
|
typedef struct _Py_DebugOffsets {
|
|
|
|
char cookie[8];
|
|
|
|
uint64_t version;
|
|
|
|
// Runtime state offset;
|
|
|
|
struct _runtime_state {
|
2024-02-28 06:17:34 -04:00
|
|
|
uint64_t finalizing;
|
|
|
|
uint64_t interpreters_head;
|
2023-07-11 16:35:41 -03:00
|
|
|
} runtime_state;
|
|
|
|
|
|
|
|
// Interpreter state offset;
|
|
|
|
struct _interpreter_state {
|
2024-02-28 06:17:34 -04:00
|
|
|
uint64_t next;
|
|
|
|
uint64_t threads_head;
|
|
|
|
uint64_t gc;
|
|
|
|
uint64_t imports_modules;
|
|
|
|
uint64_t sysdict;
|
|
|
|
uint64_t builtins;
|
|
|
|
uint64_t ceval_gil;
|
|
|
|
uint64_t gil_runtime_state_locked;
|
|
|
|
uint64_t gil_runtime_state_holder;
|
2023-07-11 16:35:41 -03:00
|
|
|
} interpreter_state;
|
|
|
|
|
|
|
|
// Thread state offset;
|
|
|
|
struct _thread_state{
|
2024-02-28 06:17:34 -04:00
|
|
|
uint64_t prev;
|
|
|
|
uint64_t next;
|
|
|
|
uint64_t interp;
|
|
|
|
uint64_t current_frame;
|
|
|
|
uint64_t thread_id;
|
|
|
|
uint64_t native_thread_id;
|
2023-07-11 16:35:41 -03:00
|
|
|
} thread_state;
|
|
|
|
|
|
|
|
// InterpreterFrame offset;
|
|
|
|
struct _interpreter_frame {
|
2024-02-28 06:17:34 -04:00
|
|
|
uint64_t previous;
|
|
|
|
uint64_t executable;
|
|
|
|
uint64_t instr_ptr;
|
|
|
|
uint64_t localsplus;
|
|
|
|
uint64_t owner;
|
2023-07-11 16:35:41 -03:00
|
|
|
} interpreter_frame;
|
|
|
|
|
|
|
|
// CFrame offset;
|
|
|
|
struct _cframe {
|
2024-02-28 06:17:34 -04:00
|
|
|
uint64_t current_frame;
|
|
|
|
uint64_t previous;
|
2023-07-11 16:35:41 -03:00
|
|
|
} cframe;
|
|
|
|
|
|
|
|
// Code object offset;
|
|
|
|
struct _code_object {
|
2024-02-28 06:17:34 -04:00
|
|
|
uint64_t filename;
|
|
|
|
uint64_t name;
|
|
|
|
uint64_t linetable;
|
|
|
|
uint64_t firstlineno;
|
|
|
|
uint64_t argcount;
|
|
|
|
uint64_t localsplusnames;
|
|
|
|
uint64_t localspluskinds;
|
|
|
|
uint64_t co_code_adaptive;
|
2023-07-11 16:35:41 -03:00
|
|
|
} code_object;
|
|
|
|
|
|
|
|
// PyObject offset;
|
|
|
|
struct _pyobject {
|
2024-02-28 06:17:34 -04:00
|
|
|
uint64_t ob_type;
|
2023-07-11 16:35:41 -03:00
|
|
|
} pyobject;
|
|
|
|
|
|
|
|
// PyTypeObject object offset;
|
|
|
|
struct _type_object {
|
2024-02-28 06:17:34 -04:00
|
|
|
uint64_t tp_name;
|
2023-07-11 16:35:41 -03:00
|
|
|
} type_object;
|
|
|
|
|
|
|
|
// PyTuple object offset;
|
|
|
|
struct _tuple_object {
|
2024-02-28 06:17:34 -04:00
|
|
|
uint64_t ob_item;
|
2023-07-11 16:35:41 -03:00
|
|
|
} tuple_object;
|
2024-02-28 06:17:34 -04:00
|
|
|
|
|
|
|
// Unicode object offset;
|
|
|
|
struct _unicode_object {
|
|
|
|
uint64_t state;
|
|
|
|
uint64_t length;
|
|
|
|
size_t asciiobject_size;
|
|
|
|
} unicode_object;
|
2023-07-11 16:35:41 -03:00
|
|
|
} _Py_DebugOffsets;
|
|
|
|
|
2020-04-13 06:45:21 -03:00
|
|
|
/* Full Python runtime state */
|
|
|
|
|
2022-01-12 19:28:46 -04:00
|
|
|
/* _PyRuntimeState holds the global state for the CPython runtime.
|
|
|
|
That data is exposed in the internal API as a static variable (_PyRuntime).
|
|
|
|
*/
|
2020-04-13 06:45:21 -03:00
|
|
|
typedef struct pyruntimestate {
|
2023-07-11 16:35:41 -03:00
|
|
|
/* This field must be first to facilitate locating it by out of process
|
|
|
|
* debuggers. Out of process debuggers will use the offsets contained in this
|
|
|
|
* field to be able to locate other fields in several interpreter structures
|
|
|
|
* in a way that doesn't require them to know the exact layout of those
|
|
|
|
* structures.
|
|
|
|
*
|
|
|
|
* IMPORTANT:
|
|
|
|
* This struct is **NOT** backwards compatible between minor version of the
|
|
|
|
* interpreter and the members, order of members and size can change between
|
|
|
|
* minor versions. This struct is only guaranteed to be stable between patch
|
|
|
|
* versions for a given minor version of the interpreter.
|
|
|
|
*/
|
|
|
|
_Py_DebugOffsets debug_offsets;
|
|
|
|
|
2021-12-07 21:59:49 -04:00
|
|
|
/* Has been initialized to a safe state.
|
|
|
|
|
|
|
|
In order to be effective, this must be set to 0 during or right
|
|
|
|
after allocation. */
|
|
|
|
int _initialized;
|
|
|
|
|
2020-04-13 06:45:21 -03:00
|
|
|
/* Is running Py_PreInitialize()? */
|
|
|
|
int preinitializing;
|
|
|
|
|
|
|
|
/* Is Python preinitialized? Set to 1 by Py_PreInitialize() */
|
|
|
|
int preinitialized;
|
|
|
|
|
|
|
|
/* Is Python core initialized? Set to 1 by _Py_InitializeCore() */
|
|
|
|
int core_initialized;
|
|
|
|
|
|
|
|
/* Is Python fully initialized? Set to 1 by Py_Initialize() */
|
|
|
|
int initialized;
|
|
|
|
|
|
|
|
/* Set by Py_FinalizeEx(). Only reset to NULL if Py_Initialize()
|
|
|
|
is called again.
|
|
|
|
|
|
|
|
Use _PyRuntimeState_GetFinalizing() and _PyRuntimeState_SetFinalizing()
|
|
|
|
to access it, don't access it directly. */
|
2023-10-13 13:40:15 -03:00
|
|
|
PyThreadState *_finalizing;
|
2023-09-27 16:41:06 -03:00
|
|
|
/* The ID of the OS thread in which we are finalizing. */
|
|
|
|
unsigned long _finalizing_id;
|
2020-04-13 06:45:21 -03:00
|
|
|
|
|
|
|
struct pyinterpreters {
|
2023-12-07 15:33:40 -04:00
|
|
|
PyMutex mutex;
|
2022-01-12 19:28:46 -04:00
|
|
|
/* The linked list of interpreters, newest first. */
|
2020-04-13 06:45:21 -03:00
|
|
|
PyInterpreterState *head;
|
2022-01-12 19:28:46 -04:00
|
|
|
/* The runtime's initial interpreter, which has a special role
|
|
|
|
in the operation of the runtime. It is also often the only
|
|
|
|
interpreter. */
|
2020-04-13 06:45:21 -03:00
|
|
|
PyInterpreterState *main;
|
2022-09-20 16:54:33 -03:00
|
|
|
/* next_id is an auto-numbered sequence of small
|
|
|
|
integers. It gets initialized in _PyInterpreterState_Enable(),
|
2020-04-13 06:45:21 -03:00
|
|
|
which is called in Py_Initialize(), and used in
|
|
|
|
PyInterpreterState_New(). A negative interpreter ID
|
|
|
|
indicates an error occurred. The main interpreter will
|
|
|
|
always have an ID of 0. Overflow results in a RuntimeError.
|
|
|
|
If that becomes a problem later then we can adjust, e.g. by
|
|
|
|
using a Python int. */
|
|
|
|
int64_t next_id;
|
|
|
|
} interpreters;
|
2023-06-27 12:26:53 -03:00
|
|
|
|
2024-02-20 10:57:48 -04:00
|
|
|
/* Platform-specific identifier and PyThreadState, respectively, for the
|
|
|
|
main thread in the main interpreter. */
|
2023-06-27 12:26:53 -03:00
|
|
|
unsigned long main_thread;
|
2024-02-20 10:57:48 -04:00
|
|
|
PyThreadState *main_tstate;
|
2023-06-27 12:26:53 -03:00
|
|
|
|
|
|
|
/* ---------- IMPORTANT ---------------------------
|
|
|
|
The fields above this line are declared as early as
|
|
|
|
possible to facilitate out-of-process observability
|
|
|
|
tools. */
|
|
|
|
|
2023-11-01 20:36:40 -03:00
|
|
|
/* cross-interpreter data and utils */
|
|
|
|
struct _xi_runtime_state xi;
|
2020-04-13 06:45:21 -03:00
|
|
|
|
2023-06-27 12:26:53 -03:00
|
|
|
struct _pymem_allocators allocators;
|
|
|
|
struct _obmalloc_global_state obmalloc;
|
|
|
|
struct pyhash_runtime_state pyhash_state;
|
|
|
|
struct _pythread_runtime_state threads;
|
|
|
|
struct _signals_runtime_state signals;
|
2020-04-13 06:45:21 -03:00
|
|
|
|
2023-01-19 19:04:14 -04:00
|
|
|
/* Used for the thread state bound to the current thread. */
|
|
|
|
Py_tss_t autoTSSkey;
|
|
|
|
|
2023-01-23 11:30:20 -04:00
|
|
|
/* Used instead of PyThreadState.trash when there is not current tstate. */
|
|
|
|
Py_tss_t trashTSSkey;
|
|
|
|
|
2022-12-07 18:56:31 -04:00
|
|
|
PyWideStringList orig_argv;
|
|
|
|
|
|
|
|
struct _parser_runtime_state parser;
|
|
|
|
|
2023-04-05 21:42:02 -03:00
|
|
|
struct _atexit_runtime_state atexit;
|
2020-04-13 06:45:21 -03:00
|
|
|
|
2022-11-11 17:16:28 -04:00
|
|
|
struct _import_runtime_state imports;
|
2020-04-13 06:45:21 -03:00
|
|
|
struct _ceval_runtime_state ceval;
|
|
|
|
struct _gilstate_runtime_state gilstate;
|
2022-08-16 15:22:14 -03:00
|
|
|
struct _getargs_runtime_state getargs;
|
2022-11-16 12:37:14 -04:00
|
|
|
struct _fileutils_state fileutils;
|
2022-12-12 12:58:46 -04:00
|
|
|
struct _faulthandler_runtime_state faulthandler;
|
2022-12-12 11:44:23 -04:00
|
|
|
struct _tracemalloc_runtime_state tracemalloc;
|
2020-04-13 06:45:21 -03:00
|
|
|
|
2024-01-23 14:08:23 -04:00
|
|
|
// The rwmutex is used to prevent overlapping global and per-interpreter
|
|
|
|
// stop-the-world events. Global stop-the-world events lock the mutex
|
|
|
|
// exclusively (as a "writer"), while per-interpreter stop-the-world events
|
|
|
|
// lock it non-exclusively (as "readers").
|
|
|
|
_PyRWMutex stoptheworld_mutex;
|
|
|
|
struct _stoptheworld_state stoptheworld;
|
|
|
|
|
2020-04-13 06:45:21 -03:00
|
|
|
PyPreConfig preconfig;
|
|
|
|
|
2021-01-12 05:29:45 -04:00
|
|
|
// Audit values must be preserved when Py_Initialize()/Py_Finalize()
|
|
|
|
// is called multiple times.
|
2020-04-13 06:45:21 -03:00
|
|
|
Py_OpenCodeHookFunction open_code_hook;
|
|
|
|
void *open_code_userdata;
|
2023-06-08 15:38:15 -03:00
|
|
|
struct {
|
2023-12-07 15:33:40 -04:00
|
|
|
PyMutex mutex;
|
2023-06-08 15:38:15 -03:00
|
|
|
_Py_AuditHookEntry *head;
|
|
|
|
} audit_hooks;
|
2020-04-13 06:45:21 -03:00
|
|
|
|
2023-03-20 13:03:04 -03:00
|
|
|
struct _py_object_runtime_state object_state;
|
2022-11-16 12:37:14 -04:00
|
|
|
struct _Py_float_runtime_state float_state;
|
|
|
|
struct _Py_unicode_runtime_state unicode_state;
|
2023-04-24 19:30:13 -03:00
|
|
|
struct _types_runtime_state types;
|
2022-11-14 16:50:56 -04:00
|
|
|
|
2022-01-12 19:28:46 -04:00
|
|
|
/* All the objects that are shared by the runtime's interpreters. */
|
2023-07-27 16:56:59 -03:00
|
|
|
struct _Py_cached_objects cached_objects;
|
2022-12-14 14:53:57 -04:00
|
|
|
struct _Py_static_objects static_objects;
|
2022-01-12 19:28:46 -04:00
|
|
|
|
|
|
|
/* The following fields are here to avoid allocation during init.
|
|
|
|
The data is exposed through _PyRuntimeState pointer fields.
|
|
|
|
These fields should not be accessed directly outside of init.
|
|
|
|
|
|
|
|
All other _PyRuntimeState pointer fields are populated when
|
|
|
|
needed and default to NULL.
|
|
|
|
|
|
|
|
For now there are some exceptions to that rule, which require
|
|
|
|
allocation during init. These will be addressed on a case-by-case
|
|
|
|
basis. Most notably, we don't pre-allocated the several mutex
|
|
|
|
(PyThread_type_lock) fields, because on Windows we only ever get
|
|
|
|
a pointer type.
|
|
|
|
*/
|
|
|
|
|
2024-02-01 15:27:53 -04:00
|
|
|
/* _PyRuntimeState.interpreters.main */
|
2022-01-12 19:28:46 -04:00
|
|
|
PyInterpreterState _main_interpreter;
|
2023-09-15 19:04:21 -03:00
|
|
|
|
|
|
|
#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE)
|
|
|
|
// Used in "Python/emscripten_trampoline.c" to choose between type
|
|
|
|
// reflection trampoline and EM_JS trampoline.
|
|
|
|
bool wasm_type_reflection_available;
|
|
|
|
#endif
|
|
|
|
|
2020-04-13 06:45:21 -03:00
|
|
|
} _PyRuntimeState;
|
|
|
|
|
2021-12-07 21:59:49 -04:00
|
|
|
|
2022-01-12 19:28:46 -04:00
|
|
|
/* other API */
|
2020-04-13 06:45:21 -03:00
|
|
|
|
2023-08-24 12:40:56 -03:00
|
|
|
// Export _PyRuntime for shared extensions which use it in static inline
|
|
|
|
// functions for best performance, like _Py_IsMainThread() or _Py_ID().
|
|
|
|
// It's also made accessible for debuggers and profilers.
|
2020-04-13 06:45:21 -03:00
|
|
|
PyAPI_DATA(_PyRuntimeState) _PyRuntime;
|
|
|
|
|
2023-07-24 22:49:28 -03:00
|
|
|
extern PyStatus _PyRuntimeState_Init(_PyRuntimeState *runtime);
|
|
|
|
extern void _PyRuntimeState_Fini(_PyRuntimeState *runtime);
|
2020-04-13 06:45:21 -03:00
|
|
|
|
2020-04-14 13:16:24 -03:00
|
|
|
#ifdef HAVE_FORK
|
2020-06-02 10:51:37 -03:00
|
|
|
extern PyStatus _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime);
|
2020-04-14 13:16:24 -03:00
|
|
|
#endif
|
2020-04-13 06:45:21 -03:00
|
|
|
|
|
|
|
/* Initialize _PyRuntimeState.
|
|
|
|
Return NULL on success, or return an error message on failure. */
|
2023-07-24 22:49:28 -03:00
|
|
|
extern PyStatus _PyRuntime_Initialize(void);
|
2020-04-13 06:45:21 -03:00
|
|
|
|
2023-07-24 22:49:28 -03:00
|
|
|
extern void _PyRuntime_Finalize(void);
|
2020-04-13 06:45:21 -03:00
|
|
|
|
|
|
|
|
|
|
|
static inline PyThreadState*
|
|
|
|
_PyRuntimeState_GetFinalizing(_PyRuntimeState *runtime) {
|
2023-10-13 13:40:15 -03:00
|
|
|
return (PyThreadState*)_Py_atomic_load_ptr_relaxed(&runtime->_finalizing);
|
2020-04-13 06:45:21 -03:00
|
|
|
}
|
|
|
|
|
2023-09-27 16:41:06 -03:00
|
|
|
static inline unsigned long
|
|
|
|
_PyRuntimeState_GetFinalizingID(_PyRuntimeState *runtime) {
|
|
|
|
return _Py_atomic_load_ulong_relaxed(&runtime->_finalizing_id);
|
|
|
|
}
|
|
|
|
|
2020-04-13 06:45:21 -03:00
|
|
|
static inline void
|
|
|
|
_PyRuntimeState_SetFinalizing(_PyRuntimeState *runtime, PyThreadState *tstate) {
|
2023-10-13 13:40:15 -03:00
|
|
|
_Py_atomic_store_ptr_relaxed(&runtime->_finalizing, tstate);
|
2023-09-27 16:41:06 -03:00
|
|
|
if (tstate == NULL) {
|
|
|
|
_Py_atomic_store_ulong_relaxed(&runtime->_finalizing_id, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// XXX Re-enable this assert once gh-109860 is fixed.
|
|
|
|
//assert(tstate->thread_id == PyThread_get_thread_ident());
|
|
|
|
_Py_atomic_store_ulong_relaxed(&runtime->_finalizing_id,
|
|
|
|
tstate->thread_id);
|
|
|
|
}
|
2020-04-13 06:45:21 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* !Py_INTERNAL_RUNTIME_H */
|