Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1 | #ifndef Py_INTERNAL_PYSTATE_H |
| 2 | #define Py_INTERNAL_PYSTATE_H |
| 3 | #ifdef __cplusplus |
| 4 | extern "C" { |
| 5 | #endif |
| 6 | |
| 7 | #include "pystate.h" |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 8 | #include "pythread.h" |
| 9 | |
| 10 | #include "internal/mem.h" |
| 11 | #include "internal/ceval.h" |
| 12 | #include "internal/warnings.h" |
| 13 | |
| 14 | |
| 15 | /* GIL state */ |
| 16 | |
| 17 | struct _gilstate_runtime_state { |
| 18 | int check_enabled; |
| 19 | /* Assuming the current thread holds the GIL, this is the |
| 20 | PyThreadState for the current thread. */ |
| 21 | _Py_atomic_address tstate_current; |
| 22 | PyThreadFrameGetter getframe; |
| 23 | /* The single PyInterpreterState used by this process' |
| 24 | GILState implementation |
| 25 | */ |
| 26 | /* TODO: Given interp_main, it may be possible to kill this ref */ |
| 27 | PyInterpreterState *autoInterpreterState; |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 28 | Py_tss_t autoTSSkey; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | /* hook for PyEval_GetFrame(), requested for Psyco */ |
| 32 | #define _PyThreadState_GetFrame _PyRuntime.gilstate.getframe |
| 33 | |
| 34 | /* Issue #26558: Flag to disable PyGILState_Check(). |
| 35 | If set to non-zero, PyGILState_Check() always return 1. */ |
| 36 | #define _PyGILState_check_enabled _PyRuntime.gilstate.check_enabled |
| 37 | |
| 38 | |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 39 | typedef struct _PyPathConfig { |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 40 | /* Full path to the Python program */ |
| 41 | wchar_t *program_full_path; |
| 42 | wchar_t *prefix; |
| 43 | #ifdef MS_WINDOWS |
| 44 | wchar_t *dll_path; |
| 45 | #else |
| 46 | wchar_t *exec_prefix; |
| 47 | #endif |
| 48 | /* Set by Py_SetPath(), or computed by _PyPathConfig_Init() */ |
| 49 | wchar_t *module_search_path; |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 50 | /* Python program name */ |
| 51 | wchar_t *program_name; |
| 52 | /* Set by Py_SetPythonHome() or PYTHONHOME environment variable */ |
| 53 | wchar_t *home; |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 54 | /* isolated and site_import are used to set Py_IsolatedFlag and |
Victor Stinner | f2626ce | 2018-07-21 03:54:20 +0200 | [diff] [blame] | 55 | Py_NoSiteFlag flags on Windows in read_pth_file(). These fields |
| 56 | are ignored when their value are equal to -1 (unset). */ |
| 57 | int isolated; |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 58 | int site_import; |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 59 | } _PyPathConfig; |
| 60 | |
Victor Stinner | f2626ce | 2018-07-21 03:54:20 +0200 | [diff] [blame] | 61 | #define _PyPathConfig_INIT \ |
| 62 | {.module_search_path = NULL, \ |
| 63 | .isolated = -1, \ |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 64 | .site_import = -1} |
Victor Stinner | 33c377e | 2017-12-05 15:12:41 +0100 | [diff] [blame] | 65 | /* Note: _PyPathConfig_INIT sets other fields to 0/NULL */ |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 66 | |
| 67 | PyAPI_DATA(_PyPathConfig) _Py_path_config; |
| 68 | |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 69 | PyAPI_FUNC(_PyInitError) _PyPathConfig_Calculate_impl( |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 70 | _PyPathConfig *config, |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 71 | const _PyCoreConfig *core_config); |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 72 | PyAPI_FUNC(void) _PyPathConfig_ClearGlobal(void); |
Victor Stinner | 0ea395a | 2017-12-01 20:50:58 +0100 | [diff] [blame] | 73 | |
Victor Stinner | 6c785c0 | 2018-08-01 17:56:14 +0200 | [diff] [blame] | 74 | PyAPI_FUNC(void) _Py_wstrlist_clear( |
| 75 | int len, |
| 76 | wchar_t **list); |
| 77 | PyAPI_FUNC(wchar_t**) _Py_wstrlist_copy( |
| 78 | int len, |
| 79 | wchar_t **list); |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 80 | PyAPI_FUNC(_PyInitError) _Py_wstrlist_append( |
| 81 | int *len, |
| 82 | wchar_t ***list, |
| 83 | const wchar_t *str); |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 84 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 85 | /* interpreter state */ |
| 86 | |
| 87 | PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_LookUpID(PY_INT64_T); |
| 88 | |
Eric Snow | 4c6955e | 2018-02-16 18:53:40 -0700 | [diff] [blame] | 89 | PyAPI_FUNC(int) _PyInterpreterState_IDInitref(PyInterpreterState *); |
| 90 | PyAPI_FUNC(void) _PyInterpreterState_IDIncref(PyInterpreterState *); |
| 91 | PyAPI_FUNC(void) _PyInterpreterState_IDDecref(PyInterpreterState *); |
| 92 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 93 | |
| 94 | /* cross-interpreter data */ |
| 95 | |
| 96 | struct _xid; |
| 97 | |
| 98 | // _PyCrossInterpreterData is similar to Py_buffer as an effectively |
| 99 | // opaque struct that holds data outside the object machinery. This |
Eric Snow | 6379913 | 2018-06-01 18:45:20 -0600 | [diff] [blame] | 100 | // is necessary to pass safely between interpreters in the same process. |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 101 | typedef struct _xid { |
| 102 | // data is the cross-interpreter-safe derivation of a Python object |
| 103 | // (see _PyObject_GetCrossInterpreterData). It will be NULL if the |
| 104 | // new_object func (below) encodes the data. |
| 105 | void *data; |
| 106 | // obj is the Python object from which the data was derived. This |
| 107 | // is non-NULL only if the data remains bound to the object in some |
| 108 | // way, such that the object must be "released" (via a decref) when |
Eric Snow | 6379913 | 2018-06-01 18:45:20 -0600 | [diff] [blame] | 109 | // the data is released. In that case the code that sets the field, |
| 110 | // likely a registered "crossinterpdatafunc", is responsible for |
| 111 | // ensuring it owns the reference (i.e. incref). |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 112 | PyObject *obj; |
| 113 | // interp is the ID of the owning interpreter of the original |
| 114 | // object. It corresponds to the active interpreter when |
| 115 | // _PyObject_GetCrossInterpreterData() was called. This should only |
| 116 | // be set by the cross-interpreter machinery. |
| 117 | // |
| 118 | // We use the ID rather than the PyInterpreterState to avoid issues |
| 119 | // with deleted interpreters. |
| 120 | int64_t interp; |
| 121 | // new_object is a function that returns a new object in the current |
| 122 | // interpreter given the data. The resulting object (a new |
| 123 | // reference) will be equivalent to the original object. This field |
| 124 | // is required. |
| 125 | PyObject *(*new_object)(struct _xid *); |
| 126 | // free is called when the data is released. If it is NULL then |
| 127 | // nothing will be done to free the data. For some types this is |
| 128 | // okay (e.g. bytes) and for those types this field should be set |
| 129 | // to NULL. However, for most the data was allocated just for |
| 130 | // cross-interpreter use, so it must be freed when |
| 131 | // _PyCrossInterpreterData_Release is called or the memory will |
| 132 | // leak. In that case, at the very least this field should be set |
| 133 | // to PyMem_RawFree (the default if not explicitly set to NULL). |
| 134 | // The call will happen with the original interpreter activated. |
| 135 | void (*free)(void *); |
| 136 | } _PyCrossInterpreterData; |
| 137 | |
| 138 | typedef int (*crossinterpdatafunc)(PyObject *, _PyCrossInterpreterData *); |
| 139 | PyAPI_FUNC(int) _PyObject_CheckCrossInterpreterData(PyObject *); |
| 140 | |
| 141 | PyAPI_FUNC(int) _PyObject_GetCrossInterpreterData(PyObject *, _PyCrossInterpreterData *); |
| 142 | PyAPI_FUNC(PyObject *) _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *); |
| 143 | PyAPI_FUNC(void) _PyCrossInterpreterData_Release(_PyCrossInterpreterData *); |
| 144 | |
| 145 | /* cross-interpreter data registry */ |
| 146 | |
| 147 | /* For now we use a global registry of shareable classes. An |
| 148 | alternative would be to add a tp_* slot for a class's |
| 149 | crossinterpdatafunc. It would be simpler and more efficient. */ |
| 150 | |
| 151 | PyAPI_FUNC(int) _PyCrossInterpreterData_Register_Class(PyTypeObject *, crossinterpdatafunc); |
| 152 | PyAPI_FUNC(crossinterpdatafunc) _PyCrossInterpreterData_Lookup(PyObject *); |
| 153 | |
| 154 | struct _xidregitem; |
| 155 | |
| 156 | struct _xidregitem { |
| 157 | PyTypeObject *cls; |
| 158 | crossinterpdatafunc getdata; |
| 159 | struct _xidregitem *next; |
| 160 | }; |
| 161 | |
| 162 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 163 | /* Full Python runtime state */ |
| 164 | |
| 165 | typedef struct pyruntimestate { |
| 166 | int initialized; |
| 167 | int core_initialized; |
| 168 | PyThreadState *finalizing; |
| 169 | |
| 170 | struct pyinterpreters { |
| 171 | PyThread_type_lock mutex; |
| 172 | PyInterpreterState *head; |
| 173 | PyInterpreterState *main; |
| 174 | /* _next_interp_id is an auto-numbered sequence of small |
| 175 | integers. It gets initialized in _PyInterpreterState_Init(), |
| 176 | which is called in Py_Initialize(), and used in |
| 177 | PyInterpreterState_New(). A negative interpreter ID |
| 178 | indicates an error occurred. The main interpreter will |
| 179 | always have an ID of 0. Overflow results in a RuntimeError. |
| 180 | If that becomes a problem later then we can adjust, e.g. by |
| 181 | using a Python int. */ |
| 182 | int64_t next_id; |
| 183 | } interpreters; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 184 | // XXX Remove this field once we have a tp_* slot. |
| 185 | struct _xidregistry { |
| 186 | PyThread_type_lock mutex; |
| 187 | struct _xidregitem *head; |
| 188 | } xidregistry; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 189 | |
| 190 | #define NEXITFUNCS 32 |
| 191 | void (*exitfuncs[NEXITFUNCS])(void); |
| 192 | int nexitfuncs; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 193 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 194 | struct _gc_runtime_state gc; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 195 | struct _warnings_runtime_state warnings; |
| 196 | struct _ceval_runtime_state ceval; |
| 197 | struct _gilstate_runtime_state gilstate; |
| 198 | |
| 199 | // XXX Consolidate globals found via the check-c-globals script. |
| 200 | } _PyRuntimeState; |
| 201 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 202 | #define _PyRuntimeState_INIT {.initialized = 0, .core_initialized = 0} |
Victor Stinner | 33c377e | 2017-12-05 15:12:41 +0100 | [diff] [blame] | 203 | /* Note: _PyRuntimeState_INIT sets other fields to 0/NULL */ |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 204 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 205 | PyAPI_DATA(_PyRuntimeState) _PyRuntime; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 206 | PyAPI_FUNC(_PyInitError) _PyRuntimeState_Init(_PyRuntimeState *); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 207 | PyAPI_FUNC(void) _PyRuntimeState_Fini(_PyRuntimeState *); |
| 208 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 209 | /* Initialize _PyRuntimeState. |
| 210 | Return NULL on success, or return an error message on failure. */ |
| 211 | PyAPI_FUNC(_PyInitError) _PyRuntime_Initialize(void); |
| 212 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 213 | #define _Py_CURRENTLY_FINALIZING(tstate) \ |
| 214 | (_PyRuntime.finalizing == tstate) |
| 215 | |
| 216 | |
| 217 | /* Other */ |
| 218 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 219 | PyAPI_FUNC(_PyInitError) _PyInterpreterState_Enable(_PyRuntimeState *); |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 220 | PyAPI_FUNC(void) _PyInterpreterState_DeleteExceptMain(void); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 221 | |
| 222 | #ifdef __cplusplus |
| 223 | } |
| 224 | #endif |
| 225 | #endif /* !Py_INTERNAL_PYSTATE_H */ |