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 | |
Victor Stinner | 5c75f37 | 2019-04-17 23:02:26 +0200 | [diff] [blame] | 7 | #ifndef Py_BUILD_CORE |
| 8 | # error "this header requires Py_BUILD_CORE define" |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 9 | #endif |
| 10 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 11 | #include "cpython/initconfig.h" |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 12 | #include "fileobject.h" |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 13 | #include "pystate.h" |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 14 | #include "pythread.h" |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 15 | #include "sysmodule.h" |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 16 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 17 | #include "pycore_gil.h" /* _gil_runtime_state */ |
Victor Stinner | a1c249c | 2018-11-01 03:15:58 +0100 | [diff] [blame] | 18 | #include "pycore_pathconfig.h" |
Victor Stinner | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 19 | #include "pycore_pymem.h" |
Victor Stinner | 27e2d1f | 2018-11-01 00:52:28 +0100 | [diff] [blame] | 20 | #include "pycore_warnings.h" |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 21 | |
| 22 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 23 | /* ceval state */ |
| 24 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 25 | struct _pending_calls { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 26 | int finishing; |
| 27 | PyThread_type_lock lock; |
| 28 | /* Request for running pending calls. */ |
| 29 | _Py_atomic_int calls_to_do; |
| 30 | /* Request for looking at the `async_exc` field of the current |
| 31 | thread state. |
| 32 | Guarded by the GIL. */ |
| 33 | int async_exc; |
| 34 | #define NPENDINGCALLS 32 |
| 35 | struct { |
| 36 | int (*func)(void *); |
| 37 | void *arg; |
| 38 | } calls[NPENDINGCALLS]; |
| 39 | int first; |
| 40 | int last; |
| 41 | }; |
| 42 | |
| 43 | struct _ceval_runtime_state { |
| 44 | int recursion_limit; |
| 45 | /* Records whether tracing is on for any thread. Counts the number |
| 46 | of threads for which tstate->c_tracefunc is non-NULL, so if the |
| 47 | value is 0, we know we don't have to check this thread's |
| 48 | c_tracefunc. This speeds up the if statement in |
| 49 | PyEval_EvalFrameEx() after fast_next_opcode. */ |
| 50 | int tracing_possible; |
| 51 | /* This single variable consolidates all requests to break out of |
| 52 | the fast path in the eval loop. */ |
| 53 | _Py_atomic_int eval_breaker; |
| 54 | /* Request for dropping the GIL */ |
| 55 | _Py_atomic_int gil_drop_request; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 56 | struct _pending_calls pending; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 57 | /* Request for checking signals. */ |
| 58 | _Py_atomic_int signals_pending; |
| 59 | struct _gil_runtime_state gil; |
| 60 | }; |
| 61 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 62 | /* interpreter state */ |
| 63 | |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 64 | typedef PyObject* (*_PyFrameEvalFunction)(struct _frame *, int); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 65 | |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 66 | // The PyInterpreterState typedef is in Include/pystate.h. |
| 67 | struct _is { |
| 68 | |
| 69 | struct _is *next; |
| 70 | struct _ts *tstate_head; |
| 71 | |
| 72 | int64_t id; |
| 73 | int64_t id_refcount; |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 74 | int requires_idref; |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 75 | PyThread_type_lock id_mutex; |
| 76 | |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 77 | int finalizing; |
| 78 | |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 79 | PyObject *modules; |
| 80 | PyObject *modules_by_index; |
| 81 | PyObject *sysdict; |
| 82 | PyObject *builtins; |
| 83 | PyObject *importlib; |
| 84 | |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 85 | /* Used in Modules/_threadmodule.c. */ |
| 86 | long num_threads; |
| 87 | /* Support for runtime thread stack size tuning. |
| 88 | A value of 0 means using the platform's default stack size |
| 89 | or the size specified by the THREAD_STACK_SIZE macro. */ |
| 90 | /* Used in Python/thread.c. */ |
| 91 | size_t pythread_stacksize; |
| 92 | |
| 93 | PyObject *codec_search_path; |
| 94 | PyObject *codec_search_cache; |
| 95 | PyObject *codec_error_registry; |
| 96 | int codecs_initialized; |
Victor Stinner | 709d23d | 2019-05-02 14:56:30 -0400 | [diff] [blame] | 97 | |
| 98 | /* fs_codec.encoding is initialized to NULL. |
| 99 | Later, it is set to a non-NULL string by _PyUnicode_InitEncodings(). */ |
| 100 | struct { |
| 101 | char *encoding; /* Filesystem encoding (encoded to UTF-8) */ |
| 102 | char *errors; /* Filesystem errors (encoded to UTF-8) */ |
| 103 | _Py_error_handler error_handler; |
| 104 | } fs_codec; |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 105 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 106 | PyConfig config; |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 107 | #ifdef HAVE_DLOPEN |
| 108 | int dlopenflags; |
| 109 | #endif |
| 110 | |
Eric Snow | d2fdd1f | 2019-03-15 17:47:43 -0600 | [diff] [blame] | 111 | PyObject *dict; /* Stores per-interpreter state */ |
| 112 | |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 113 | PyObject *builtins_copy; |
| 114 | PyObject *import_func; |
| 115 | /* Initialized to PyEval_EvalFrameDefault(). */ |
| 116 | _PyFrameEvalFunction eval_frame; |
| 117 | |
| 118 | Py_ssize_t co_extra_user_count; |
| 119 | freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS]; |
| 120 | |
| 121 | #ifdef HAVE_FORK |
| 122 | PyObject *before_forkers; |
| 123 | PyObject *after_forkers_parent; |
| 124 | PyObject *after_forkers_child; |
| 125 | #endif |
| 126 | /* AtExit module */ |
| 127 | void (*pyexitfunc)(PyObject *); |
| 128 | PyObject *pyexitmodule; |
| 129 | |
| 130 | uint64_t tstate_next_unique_id; |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 131 | |
| 132 | struct _warnings_runtime_state warnings; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 133 | |
| 134 | PyObject *audit_hooks; |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | PyAPI_FUNC(struct _is*) _PyInterpreterState_LookUpID(PY_INT64_T); |
| 138 | |
| 139 | PyAPI_FUNC(int) _PyInterpreterState_IDInitref(struct _is *); |
| 140 | PyAPI_FUNC(void) _PyInterpreterState_IDIncref(struct _is *); |
| 141 | PyAPI_FUNC(void) _PyInterpreterState_IDDecref(struct _is *); |
Eric Snow | 4c6955e | 2018-02-16 18:53:40 -0700 | [diff] [blame] | 142 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 143 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 144 | /* cross-interpreter data registry */ |
| 145 | |
| 146 | /* For now we use a global registry of shareable classes. An |
| 147 | alternative would be to add a tp_* slot for a class's |
| 148 | crossinterpdatafunc. It would be simpler and more efficient. */ |
| 149 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 150 | struct _xidregitem; |
| 151 | |
| 152 | struct _xidregitem { |
| 153 | PyTypeObject *cls; |
| 154 | crossinterpdatafunc getdata; |
| 155 | struct _xidregitem *next; |
| 156 | }; |
| 157 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 158 | /* runtime audit hook state */ |
| 159 | |
| 160 | typedef struct _Py_AuditHookEntry { |
| 161 | struct _Py_AuditHookEntry *next; |
| 162 | Py_AuditHookFunction hookCFunction; |
| 163 | void *userData; |
| 164 | } _Py_AuditHookEntry; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 165 | |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 166 | /* GIL state */ |
| 167 | |
| 168 | struct _gilstate_runtime_state { |
| 169 | int check_enabled; |
| 170 | /* Assuming the current thread holds the GIL, this is the |
| 171 | PyThreadState for the current thread. */ |
| 172 | _Py_atomic_address tstate_current; |
| 173 | PyThreadFrameGetter getframe; |
| 174 | /* The single PyInterpreterState used by this process' |
| 175 | GILState implementation |
| 176 | */ |
| 177 | /* TODO: Given interp_main, it may be possible to kill this ref */ |
| 178 | PyInterpreterState *autoInterpreterState; |
| 179 | Py_tss_t autoTSSkey; |
| 180 | }; |
| 181 | |
| 182 | /* hook for PyEval_GetFrame(), requested for Psyco */ |
| 183 | #define _PyThreadState_GetFrame _PyRuntime.gilstate.getframe |
| 184 | |
| 185 | /* Issue #26558: Flag to disable PyGILState_Check(). |
| 186 | If set to non-zero, PyGILState_Check() always return 1. */ |
| 187 | #define _PyGILState_check_enabled _PyRuntime.gilstate.check_enabled |
| 188 | |
| 189 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 190 | /* Full Python runtime state */ |
| 191 | |
| 192 | typedef struct pyruntimestate { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 193 | /* Is Python pre-initialized? Set to 1 by Py_PreInitialize() */ |
Victor Stinner | f29084d | 2019-03-20 02:20:13 +0100 | [diff] [blame] | 194 | int pre_initialized; |
| 195 | |
| 196 | /* Is Python core initialized? Set to 1 by _Py_InitializeCore() */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 197 | int core_initialized; |
Victor Stinner | f29084d | 2019-03-20 02:20:13 +0100 | [diff] [blame] | 198 | |
| 199 | /* Is Python fully initialized? Set to 1 by Py_Initialize() */ |
| 200 | int initialized; |
| 201 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 202 | PyThreadState *finalizing; |
| 203 | |
| 204 | struct pyinterpreters { |
| 205 | PyThread_type_lock mutex; |
| 206 | PyInterpreterState *head; |
| 207 | PyInterpreterState *main; |
| 208 | /* _next_interp_id is an auto-numbered sequence of small |
| 209 | integers. It gets initialized in _PyInterpreterState_Init(), |
| 210 | which is called in Py_Initialize(), and used in |
| 211 | PyInterpreterState_New(). A negative interpreter ID |
| 212 | indicates an error occurred. The main interpreter will |
| 213 | always have an ID of 0. Overflow results in a RuntimeError. |
| 214 | If that becomes a problem later then we can adjust, e.g. by |
| 215 | using a Python int. */ |
| 216 | int64_t next_id; |
| 217 | } interpreters; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 218 | // XXX Remove this field once we have a tp_* slot. |
| 219 | struct _xidregistry { |
| 220 | PyThread_type_lock mutex; |
| 221 | struct _xidregitem *head; |
| 222 | } xidregistry; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 223 | |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 224 | unsigned long main_thread; |
| 225 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 226 | #define NEXITFUNCS 32 |
| 227 | void (*exitfuncs[NEXITFUNCS])(void); |
| 228 | int nexitfuncs; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 229 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 230 | struct _gc_runtime_state gc; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 231 | struct _ceval_runtime_state ceval; |
| 232 | struct _gilstate_runtime_state gilstate; |
| 233 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 234 | PyPreConfig preconfig; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 235 | |
| 236 | Py_OpenCodeHookFunction open_code_hook; |
| 237 | void *open_code_userdata; |
| 238 | _Py_AuditHookEntry *audit_hook_head; |
| 239 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 240 | // XXX Consolidate globals found via the check-c-globals script. |
| 241 | } _PyRuntimeState; |
| 242 | |
Victor Stinner | f29084d | 2019-03-20 02:20:13 +0100 | [diff] [blame] | 243 | #define _PyRuntimeState_INIT \ |
| 244 | {.pre_initialized = 0, .core_initialized = 0, .initialized = 0} |
Victor Stinner | 33c377e | 2017-12-05 15:12:41 +0100 | [diff] [blame] | 245 | /* Note: _PyRuntimeState_INIT sets other fields to 0/NULL */ |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 246 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 247 | PyAPI_DATA(_PyRuntimeState) _PyRuntime; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 248 | PyAPI_FUNC(PyStatus) _PyRuntimeState_Init(_PyRuntimeState *runtime); |
Victor Stinner | b930a2d | 2019-04-24 17:14:33 +0200 | [diff] [blame] | 249 | PyAPI_FUNC(void) _PyRuntimeState_Fini(_PyRuntimeState *runtime); |
| 250 | PyAPI_FUNC(void) _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 251 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 252 | /* Initialize _PyRuntimeState. |
| 253 | Return NULL on success, or return an error message on failure. */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 254 | PyAPI_FUNC(PyStatus) _PyRuntime_Initialize(void); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 255 | |
Victor Stinner | f5f336a | 2019-03-19 14:53:58 +0100 | [diff] [blame] | 256 | PyAPI_FUNC(void) _PyRuntime_Finalize(void); |
| 257 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 258 | #define _Py_CURRENTLY_FINALIZING(runtime, tstate) \ |
| 259 | (runtime->finalizing == tstate) |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 260 | |
| 261 | |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 262 | /* Variable and macro for in-line access to current thread |
| 263 | and interpreter state */ |
| 264 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 265 | #define _PyRuntimeState_GetThreadState(runtime) \ |
| 266 | ((PyThreadState*)_Py_atomic_load_relaxed(&(runtime)->gilstate.tstate_current)) |
| 267 | |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 268 | /* Get the current Python thread state. |
| 269 | |
| 270 | Efficient macro reading directly the 'gilstate.tstate_current' atomic |
| 271 | variable. The macro is unsafe: it does not check for error and it can |
| 272 | return NULL. |
| 273 | |
| 274 | The caller must hold the GIL. |
| 275 | |
| 276 | See also PyThreadState_Get() and PyThreadState_GET(). */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 277 | #define _PyThreadState_GET() _PyRuntimeState_GetThreadState(&_PyRuntime) |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 278 | |
| 279 | /* Redefine PyThreadState_GET() as an alias to _PyThreadState_GET() */ |
| 280 | #undef PyThreadState_GET |
| 281 | #define PyThreadState_GET() _PyThreadState_GET() |
| 282 | |
| 283 | /* Get the current interpreter state. |
| 284 | |
| 285 | The macro is unsafe: it does not check for error and it can return NULL. |
| 286 | |
| 287 | The caller must hold the GIL. |
| 288 | |
| 289 | See also _PyInterpreterState_Get() |
| 290 | and _PyGILState_GetInterpreterStateUnsafe(). */ |
| 291 | #define _PyInterpreterState_GET_UNSAFE() (_PyThreadState_GET()->interp) |
| 292 | |
| 293 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 294 | /* Other */ |
| 295 | |
Victor Stinner | 0fd2c30 | 2019-06-04 03:15:09 +0200 | [diff] [blame] | 296 | PyAPI_FUNC(void) _PyThreadState_Init( |
| 297 | _PyRuntimeState *runtime, |
| 298 | PyThreadState *tstate); |
| 299 | PyAPI_FUNC(void) _PyThreadState_DeleteExcept( |
| 300 | _PyRuntimeState *runtime, |
| 301 | PyThreadState *tstate); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 302 | |
| 303 | PyAPI_FUNC(PyThreadState *) _PyThreadState_Swap( |
| 304 | struct _gilstate_runtime_state *gilstate, |
| 305 | PyThreadState *newts); |
Victor Stinner | 8bb3230 | 2019-04-24 16:47:40 +0200 | [diff] [blame] | 306 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 307 | PyAPI_FUNC(PyStatus) _PyInterpreterState_Enable(_PyRuntimeState *runtime); |
Victor Stinner | b930a2d | 2019-04-24 17:14:33 +0200 | [diff] [blame] | 308 | PyAPI_FUNC(void) _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime); |
| 309 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 310 | /* Used by _PyImport_Cleanup() */ |
Victor Stinner | 0a28f8d | 2019-06-19 02:54:39 +0200 | [diff] [blame] | 311 | extern void _PyInterpreterState_ClearModules(PyInterpreterState *interp); |
| 312 | |
Victor Stinner | b930a2d | 2019-04-24 17:14:33 +0200 | [diff] [blame] | 313 | PyAPI_FUNC(void) _PyGILState_Reinit(_PyRuntimeState *runtime); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 314 | |
| 315 | #ifdef __cplusplus |
| 316 | } |
| 317 | #endif |
| 318 | #endif /* !Py_INTERNAL_PYSTATE_H */ |