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" |
| 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 | |
| 18 | struct _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 Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 29 | Py_tss_t autoTSSkey; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 30 | }; |
| 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 Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 40 | typedef 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 Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame^] | 51 | /* Python program name */ |
| 52 | wchar_t *program_name; |
| 53 | /* Set by Py_SetPythonHome() or PYTHONHOME environment variable */ |
| 54 | wchar_t *home; |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 55 | } _PyPathConfig; |
| 56 | |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame^] | 57 | #ifdef MS_WINDOWS |
| 58 | #define _PyPathConfig_INIT \ |
| 59 | {.program_full_path = NULL, \ |
| 60 | .prefix = NULL, \ |
| 61 | .dll_path = NULL, \ |
| 62 | .module_search_path = NULL, \ |
| 63 | .program_name = NULL, \ |
| 64 | .home = NULL} |
| 65 | #else |
| 66 | #define _PyPathConfig_INIT \ |
| 67 | {.program_full_path = NULL, \ |
| 68 | .prefix = NULL, \ |
| 69 | .exec_prefix = NULL, \ |
| 70 | .module_search_path = NULL, \ |
| 71 | .program_name = NULL, \ |
| 72 | .home = NULL} |
| 73 | #endif |
| 74 | |
| 75 | PyAPI_DATA(_PyPathConfig) _Py_path_config; |
| 76 | |
| 77 | PyAPI_FUNC(_PyInitError) _PyPathConfig_Calculate( |
| 78 | _PyPathConfig *config, |
| 79 | const _PyMainInterpreterConfig *main_config); |
| 80 | PyAPI_FUNC(void) _PyPathConfig_Clear(_PyPathConfig *config); |
Victor Stinner | 0ea395a | 2017-12-01 20:50:58 +0100 | [diff] [blame] | 81 | |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 82 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 83 | /* Full Python runtime state */ |
| 84 | |
| 85 | typedef struct pyruntimestate { |
| 86 | int initialized; |
| 87 | int core_initialized; |
| 88 | PyThreadState *finalizing; |
| 89 | |
| 90 | struct pyinterpreters { |
| 91 | PyThread_type_lock mutex; |
| 92 | PyInterpreterState *head; |
| 93 | PyInterpreterState *main; |
| 94 | /* _next_interp_id is an auto-numbered sequence of small |
| 95 | integers. It gets initialized in _PyInterpreterState_Init(), |
| 96 | which is called in Py_Initialize(), and used in |
| 97 | PyInterpreterState_New(). A negative interpreter ID |
| 98 | indicates an error occurred. The main interpreter will |
| 99 | always have an ID of 0. Overflow results in a RuntimeError. |
| 100 | If that becomes a problem later then we can adjust, e.g. by |
| 101 | using a Python int. */ |
| 102 | int64_t next_id; |
| 103 | } interpreters; |
| 104 | |
| 105 | #define NEXITFUNCS 32 |
| 106 | void (*exitfuncs[NEXITFUNCS])(void); |
| 107 | int nexitfuncs; |
| 108 | void (*pyexitfunc)(void); |
| 109 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 110 | struct _gc_runtime_state gc; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 111 | struct _warnings_runtime_state warnings; |
| 112 | struct _ceval_runtime_state ceval; |
| 113 | struct _gilstate_runtime_state gilstate; |
| 114 | |
| 115 | // XXX Consolidate globals found via the check-c-globals script. |
| 116 | } _PyRuntimeState; |
| 117 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 118 | #define _PyRuntimeState_INIT {.initialized = 0, .core_initialized = 0} |
| 119 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 120 | PyAPI_DATA(_PyRuntimeState) _PyRuntime; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 121 | PyAPI_FUNC(_PyInitError) _PyRuntimeState_Init(_PyRuntimeState *); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 122 | PyAPI_FUNC(void) _PyRuntimeState_Fini(_PyRuntimeState *); |
| 123 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 124 | /* Initialize _PyRuntimeState. |
| 125 | Return NULL on success, or return an error message on failure. */ |
| 126 | PyAPI_FUNC(_PyInitError) _PyRuntime_Initialize(void); |
| 127 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 128 | #define _Py_CURRENTLY_FINALIZING(tstate) \ |
| 129 | (_PyRuntime.finalizing == tstate) |
| 130 | |
| 131 | |
| 132 | /* Other */ |
| 133 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 134 | PyAPI_FUNC(_PyInitError) _PyInterpreterState_Enable(_PyRuntimeState *); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 135 | |
| 136 | #ifdef __cplusplus |
| 137 | } |
| 138 | #endif |
| 139 | #endif /* !Py_INTERNAL_PYSTATE_H */ |