blob: ef83af59323e1ee735dac52b1c72a1cbb7754429 [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 Stinnerb1147e42018-07-21 02:06:16 +020040typedef struct _PyPathConfig {
Victor Stinnerb64de462017-12-01 18:27:09 +010041 /* 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 Stinnerd19d8d52018-07-24 13:55:48 +020055 /* isolated and site_import are used to set Py_IsolatedFlag and
Victor Stinnerf2626ce2018-07-21 03:54:20 +020056 Py_NoSiteFlag flags on Windows in read_pth_file(). These fields
57 are ignored when their value are equal to -1 (unset). */
58 int isolated;
Victor Stinnerd19d8d52018-07-24 13:55:48 +020059 int site_import;
Victor Stinnerb64de462017-12-01 18:27:09 +010060} _PyPathConfig;
61
Victor Stinnerf2626ce2018-07-21 03:54:20 +020062#define _PyPathConfig_INIT \
63 {.module_search_path = NULL, \
64 .isolated = -1, \
Victor Stinnerd19d8d52018-07-24 13:55:48 +020065 .site_import = -1}
Victor Stinner33c377e2017-12-05 15:12:41 +010066/* Note: _PyPathConfig_INIT sets other fields to 0/NULL */
Victor Stinner31a83932017-12-04 13:39:15 +010067
68PyAPI_DATA(_PyPathConfig) _Py_path_config;
69
Victor Stinnerb1147e42018-07-21 02:06:16 +020070PyAPI_FUNC(_PyInitError) _PyPathConfig_Calculate_impl(
Victor Stinner31a83932017-12-04 13:39:15 +010071 _PyPathConfig *config,
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +010072 const _PyCoreConfig *core_config);
Victor Stinnerb1147e42018-07-21 02:06:16 +020073PyAPI_FUNC(void) _PyPathConfig_ClearGlobal(void);
Victor Stinner0ea395a2017-12-01 20:50:58 +010074
Victor Stinner6c785c02018-08-01 17:56:14 +020075PyAPI_FUNC(void) _Py_wstrlist_clear(
76 int len,
77 wchar_t **list);
78PyAPI_FUNC(wchar_t**) _Py_wstrlist_copy(
79 int len,
80 wchar_t **list);
Victor Stinnerb1147e42018-07-21 02:06:16 +020081PyAPI_FUNC(_PyInitError) _Py_wstrlist_append(
82 int *len,
83 wchar_t ***list,
84 const wchar_t *str);
Victor Stinnerb64de462017-12-01 18:27:09 +010085
Eric Snow7f8bfc92018-01-29 18:23:44 -070086/* interpreter state */
87
88PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_LookUpID(PY_INT64_T);
89
Eric Snow4c6955e2018-02-16 18:53:40 -070090PyAPI_FUNC(int) _PyInterpreterState_IDInitref(PyInterpreterState *);
91PyAPI_FUNC(void) _PyInterpreterState_IDIncref(PyInterpreterState *);
92PyAPI_FUNC(void) _PyInterpreterState_IDDecref(PyInterpreterState *);
93
Eric Snow7f8bfc92018-01-29 18:23:44 -070094
95/* cross-interpreter data */
96
97struct _xid;
98
99// _PyCrossInterpreterData is similar to Py_buffer as an effectively
100// opaque struct that holds data outside the object machinery. This
Eric Snow63799132018-06-01 18:45:20 -0600101// is necessary to pass safely between interpreters in the same process.
Eric Snow7f8bfc92018-01-29 18:23:44 -0700102typedef struct _xid {
103 // data is the cross-interpreter-safe derivation of a Python object
104 // (see _PyObject_GetCrossInterpreterData). It will be NULL if the
105 // new_object func (below) encodes the data.
106 void *data;
107 // obj is the Python object from which the data was derived. This
108 // is non-NULL only if the data remains bound to the object in some
109 // way, such that the object must be "released" (via a decref) when
Eric Snow63799132018-06-01 18:45:20 -0600110 // the data is released. In that case the code that sets the field,
111 // likely a registered "crossinterpdatafunc", is responsible for
112 // ensuring it owns the reference (i.e. incref).
Eric Snow7f8bfc92018-01-29 18:23:44 -0700113 PyObject *obj;
114 // interp is the ID of the owning interpreter of the original
115 // object. It corresponds to the active interpreter when
116 // _PyObject_GetCrossInterpreterData() was called. This should only
117 // be set by the cross-interpreter machinery.
118 //
119 // We use the ID rather than the PyInterpreterState to avoid issues
120 // with deleted interpreters.
121 int64_t interp;
122 // new_object is a function that returns a new object in the current
123 // interpreter given the data. The resulting object (a new
124 // reference) will be equivalent to the original object. This field
125 // is required.
126 PyObject *(*new_object)(struct _xid *);
127 // free is called when the data is released. If it is NULL then
128 // nothing will be done to free the data. For some types this is
129 // okay (e.g. bytes) and for those types this field should be set
130 // to NULL. However, for most the data was allocated just for
131 // cross-interpreter use, so it must be freed when
132 // _PyCrossInterpreterData_Release is called or the memory will
133 // leak. In that case, at the very least this field should be set
134 // to PyMem_RawFree (the default if not explicitly set to NULL).
135 // The call will happen with the original interpreter activated.
136 void (*free)(void *);
137} _PyCrossInterpreterData;
138
139typedef int (*crossinterpdatafunc)(PyObject *, _PyCrossInterpreterData *);
140PyAPI_FUNC(int) _PyObject_CheckCrossInterpreterData(PyObject *);
141
142PyAPI_FUNC(int) _PyObject_GetCrossInterpreterData(PyObject *, _PyCrossInterpreterData *);
143PyAPI_FUNC(PyObject *) _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *);
144PyAPI_FUNC(void) _PyCrossInterpreterData_Release(_PyCrossInterpreterData *);
145
146/* cross-interpreter data registry */
147
148/* For now we use a global registry of shareable classes. An
149 alternative would be to add a tp_* slot for a class's
150 crossinterpdatafunc. It would be simpler and more efficient. */
151
152PyAPI_FUNC(int) _PyCrossInterpreterData_Register_Class(PyTypeObject *, crossinterpdatafunc);
153PyAPI_FUNC(crossinterpdatafunc) _PyCrossInterpreterData_Lookup(PyObject *);
154
155struct _xidregitem;
156
157struct _xidregitem {
158 PyTypeObject *cls;
159 crossinterpdatafunc getdata;
160 struct _xidregitem *next;
161};
162
163
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600164/* Full Python runtime state */
165
166typedef struct pyruntimestate {
167 int initialized;
168 int core_initialized;
169 PyThreadState *finalizing;
170
171 struct pyinterpreters {
172 PyThread_type_lock mutex;
173 PyInterpreterState *head;
174 PyInterpreterState *main;
175 /* _next_interp_id is an auto-numbered sequence of small
176 integers. It gets initialized in _PyInterpreterState_Init(),
177 which is called in Py_Initialize(), and used in
178 PyInterpreterState_New(). A negative interpreter ID
179 indicates an error occurred. The main interpreter will
180 always have an ID of 0. Overflow results in a RuntimeError.
181 If that becomes a problem later then we can adjust, e.g. by
182 using a Python int. */
183 int64_t next_id;
184 } interpreters;
Eric Snow7f8bfc92018-01-29 18:23:44 -0700185 // XXX Remove this field once we have a tp_* slot.
186 struct _xidregistry {
187 PyThread_type_lock mutex;
188 struct _xidregitem *head;
189 } xidregistry;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600190
191#define NEXITFUNCS 32
192 void (*exitfuncs[NEXITFUNCS])(void);
193 int nexitfuncs;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600194
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600195 struct _gc_runtime_state gc;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600196 struct _warnings_runtime_state warnings;
197 struct _ceval_runtime_state ceval;
198 struct _gilstate_runtime_state gilstate;
199
200 // XXX Consolidate globals found via the check-c-globals script.
201} _PyRuntimeState;
202
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800203#define _PyRuntimeState_INIT {.initialized = 0, .core_initialized = 0}
Victor Stinner33c377e2017-12-05 15:12:41 +0100204/* Note: _PyRuntimeState_INIT sets other fields to 0/NULL */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800205
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600206PyAPI_DATA(_PyRuntimeState) _PyRuntime;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800207PyAPI_FUNC(_PyInitError) _PyRuntimeState_Init(_PyRuntimeState *);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600208PyAPI_FUNC(void) _PyRuntimeState_Fini(_PyRuntimeState *);
209
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800210/* Initialize _PyRuntimeState.
211 Return NULL on success, or return an error message on failure. */
212PyAPI_FUNC(_PyInitError) _PyRuntime_Initialize(void);
213
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600214#define _Py_CURRENTLY_FINALIZING(tstate) \
215 (_PyRuntime.finalizing == tstate)
216
217
218/* Other */
219
Victor Stinnera7368ac2017-11-15 18:11:45 -0800220PyAPI_FUNC(_PyInitError) _PyInterpreterState_Enable(_PyRuntimeState *);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600221
222#ifdef __cplusplus
223}
224#endif
225#endif /* !Py_INTERNAL_PYSTATE_H */