blob: da642c6fd007948c334614fdfdd37e4ee1930259 [file] [log] [blame]
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001#ifndef Py_INTERNAL_PYSTATE_H
2#define Py_INTERNAL_PYSTATE_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
7#include "pystate.h"
8#include "pyatomic.h"
9#include "pythread.h"
10
11#include "internal/mem.h"
12#include "internal/ceval.h"
13#include "internal/warnings.h"
14
15
16/* GIL state */
17
18struct _gilstate_runtime_state {
19 int check_enabled;
20 /* Assuming the current thread holds the GIL, this is the
21 PyThreadState for the current thread. */
22 _Py_atomic_address tstate_current;
23 PyThreadFrameGetter getframe;
24 /* The single PyInterpreterState used by this process'
25 GILState implementation
26 */
27 /* TODO: Given interp_main, it may be possible to kill this ref */
28 PyInterpreterState *autoInterpreterState;
Masayuki Yamamoto731e1892017-10-06 19:41:34 +090029 Py_tss_t autoTSSkey;
Eric Snow2ebc5ce2017-09-07 23:51:28 -060030};
31
32/* hook for PyEval_GetFrame(), requested for Psyco */
33#define _PyThreadState_GetFrame _PyRuntime.gilstate.getframe
34
35/* Issue #26558: Flag to disable PyGILState_Check().
36 If set to non-zero, PyGILState_Check() always return 1. */
37#define _PyGILState_check_enabled _PyRuntime.gilstate.check_enabled
38
39
Victor Stinnerb64de462017-12-01 18:27:09 +010040typedef struct {
41 /* Full path to the Python program */
42 wchar_t *program_full_path;
43 wchar_t *prefix;
44#ifdef MS_WINDOWS
45 wchar_t *dll_path;
46#else
47 wchar_t *exec_prefix;
48#endif
49 /* Set by Py_SetPath(), or computed by _PyPathConfig_Init() */
50 wchar_t *module_search_path;
Victor Stinner31a83932017-12-04 13:39:15 +010051 /* Python program name */
52 wchar_t *program_name;
53 /* Set by Py_SetPythonHome() or PYTHONHOME environment variable */
54 wchar_t *home;
Victor Stinnerb64de462017-12-01 18:27:09 +010055} _PyPathConfig;
56
Victor Stinner33c377e2017-12-05 15:12:41 +010057#define _PyPathConfig_INIT {.module_search_path = NULL}
58/* Note: _PyPathConfig_INIT sets other fields to 0/NULL */
Victor Stinner31a83932017-12-04 13:39:15 +010059
60PyAPI_DATA(_PyPathConfig) _Py_path_config;
61
62PyAPI_FUNC(_PyInitError) _PyPathConfig_Calculate(
63 _PyPathConfig *config,
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +010064 const _PyCoreConfig *core_config);
Victor Stinner31a83932017-12-04 13:39:15 +010065PyAPI_FUNC(void) _PyPathConfig_Clear(_PyPathConfig *config);
Victor Stinner0ea395a2017-12-01 20:50:58 +010066
Victor Stinnerb64de462017-12-01 18:27:09 +010067
Eric Snow7f8bfc92018-01-29 18:23:44 -070068/* interpreter state */
69
70PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_LookUpID(PY_INT64_T);
71
Miss Islington (bot)3db05a32018-02-16 18:15:24 -080072PyAPI_FUNC(int) _PyInterpreterState_IDInitref(PyInterpreterState *);
73PyAPI_FUNC(void) _PyInterpreterState_IDIncref(PyInterpreterState *);
74PyAPI_FUNC(void) _PyInterpreterState_IDDecref(PyInterpreterState *);
75
Eric Snow7f8bfc92018-01-29 18:23:44 -070076
77/* cross-interpreter data */
78
79struct _xid;
80
81// _PyCrossInterpreterData is similar to Py_buffer as an effectively
82// opaque struct that holds data outside the object machinery. This
83// is necessary to pass between interpreters in the same process.
84typedef struct _xid {
85 // data is the cross-interpreter-safe derivation of a Python object
86 // (see _PyObject_GetCrossInterpreterData). It will be NULL if the
87 // new_object func (below) encodes the data.
88 void *data;
89 // obj is the Python object from which the data was derived. This
90 // is non-NULL only if the data remains bound to the object in some
91 // way, such that the object must be "released" (via a decref) when
92 // the data is released. In that case it is automatically
93 // incref'ed (to match the automatic decref when releaed).
94 PyObject *obj;
95 // interp is the ID of the owning interpreter of the original
96 // object. It corresponds to the active interpreter when
97 // _PyObject_GetCrossInterpreterData() was called. This should only
98 // be set by the cross-interpreter machinery.
99 //
100 // We use the ID rather than the PyInterpreterState to avoid issues
101 // with deleted interpreters.
102 int64_t interp;
103 // new_object is a function that returns a new object in the current
104 // interpreter given the data. The resulting object (a new
105 // reference) will be equivalent to the original object. This field
106 // is required.
107 PyObject *(*new_object)(struct _xid *);
108 // free is called when the data is released. If it is NULL then
109 // nothing will be done to free the data. For some types this is
110 // okay (e.g. bytes) and for those types this field should be set
111 // to NULL. However, for most the data was allocated just for
112 // cross-interpreter use, so it must be freed when
113 // _PyCrossInterpreterData_Release is called or the memory will
114 // leak. In that case, at the very least this field should be set
115 // to PyMem_RawFree (the default if not explicitly set to NULL).
116 // The call will happen with the original interpreter activated.
117 void (*free)(void *);
118} _PyCrossInterpreterData;
119
120typedef int (*crossinterpdatafunc)(PyObject *, _PyCrossInterpreterData *);
121PyAPI_FUNC(int) _PyObject_CheckCrossInterpreterData(PyObject *);
122
123PyAPI_FUNC(int) _PyObject_GetCrossInterpreterData(PyObject *, _PyCrossInterpreterData *);
124PyAPI_FUNC(PyObject *) _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *);
125PyAPI_FUNC(void) _PyCrossInterpreterData_Release(_PyCrossInterpreterData *);
126
127/* cross-interpreter data registry */
128
129/* For now we use a global registry of shareable classes. An
130 alternative would be to add a tp_* slot for a class's
131 crossinterpdatafunc. It would be simpler and more efficient. */
132
133PyAPI_FUNC(int) _PyCrossInterpreterData_Register_Class(PyTypeObject *, crossinterpdatafunc);
134PyAPI_FUNC(crossinterpdatafunc) _PyCrossInterpreterData_Lookup(PyObject *);
135
136struct _xidregitem;
137
138struct _xidregitem {
139 PyTypeObject *cls;
140 crossinterpdatafunc getdata;
141 struct _xidregitem *next;
142};
143
144
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600145/* Full Python runtime state */
146
147typedef struct pyruntimestate {
148 int initialized;
149 int core_initialized;
150 PyThreadState *finalizing;
151
152 struct pyinterpreters {
153 PyThread_type_lock mutex;
154 PyInterpreterState *head;
155 PyInterpreterState *main;
156 /* _next_interp_id is an auto-numbered sequence of small
157 integers. It gets initialized in _PyInterpreterState_Init(),
158 which is called in Py_Initialize(), and used in
159 PyInterpreterState_New(). A negative interpreter ID
160 indicates an error occurred. The main interpreter will
161 always have an ID of 0. Overflow results in a RuntimeError.
162 If that becomes a problem later then we can adjust, e.g. by
163 using a Python int. */
164 int64_t next_id;
165 } interpreters;
Eric Snow7f8bfc92018-01-29 18:23:44 -0700166 // XXX Remove this field once we have a tp_* slot.
167 struct _xidregistry {
168 PyThread_type_lock mutex;
169 struct _xidregitem *head;
170 } xidregistry;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600171
172#define NEXITFUNCS 32
173 void (*exitfuncs[NEXITFUNCS])(void);
174 int nexitfuncs;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600175
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600176 struct _gc_runtime_state gc;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600177 struct _warnings_runtime_state warnings;
178 struct _ceval_runtime_state ceval;
179 struct _gilstate_runtime_state gilstate;
180
181 // XXX Consolidate globals found via the check-c-globals script.
182} _PyRuntimeState;
183
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800184#define _PyRuntimeState_INIT {.initialized = 0, .core_initialized = 0}
Victor Stinner33c377e2017-12-05 15:12:41 +0100185/* Note: _PyRuntimeState_INIT sets other fields to 0/NULL */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800186
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600187PyAPI_DATA(_PyRuntimeState) _PyRuntime;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800188PyAPI_FUNC(_PyInitError) _PyRuntimeState_Init(_PyRuntimeState *);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600189PyAPI_FUNC(void) _PyRuntimeState_Fini(_PyRuntimeState *);
190
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800191/* Initialize _PyRuntimeState.
192 Return NULL on success, or return an error message on failure. */
193PyAPI_FUNC(_PyInitError) _PyRuntime_Initialize(void);
194
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600195#define _Py_CURRENTLY_FINALIZING(tstate) \
196 (_PyRuntime.finalizing == tstate)
197
198
199/* Other */
200
Victor Stinnera7368ac2017-11-15 18:11:45 -0800201PyAPI_FUNC(_PyInitError) _PyInterpreterState_Enable(_PyRuntimeState *);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600202
203#ifdef __cplusplus
204}
205#endif
206#endif /* !Py_INTERNAL_PYSTATE_H */