blob: 721d34fa4c19623af9d931672e71c0cd2a6c7b33 [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 Snow2ebc5ce2017-09-07 23:51:28 -060076/* Full Python runtime state */
77
78typedef struct pyruntimestate {
79 int initialized;
80 int core_initialized;
81 PyThreadState *finalizing;
82
83 struct pyinterpreters {
84 PyThread_type_lock mutex;
85 PyInterpreterState *head;
86 PyInterpreterState *main;
87 /* _next_interp_id is an auto-numbered sequence of small
88 integers. It gets initialized in _PyInterpreterState_Init(),
89 which is called in Py_Initialize(), and used in
90 PyInterpreterState_New(). A negative interpreter ID
91 indicates an error occurred. The main interpreter will
92 always have an ID of 0. Overflow results in a RuntimeError.
93 If that becomes a problem later then we can adjust, e.g. by
94 using a Python int. */
95 int64_t next_id;
96 } interpreters;
97
98#define NEXITFUNCS 32
99 void (*exitfuncs[NEXITFUNCS])(void);
100 int nexitfuncs;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600101
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600102 struct _gc_runtime_state gc;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600103 struct _warnings_runtime_state warnings;
104 struct _ceval_runtime_state ceval;
105 struct _gilstate_runtime_state gilstate;
106
107 // XXX Consolidate globals found via the check-c-globals script.
108} _PyRuntimeState;
109
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800110#define _PyRuntimeState_INIT {.initialized = 0, .core_initialized = 0}
Victor Stinner33c377e2017-12-05 15:12:41 +0100111/* Note: _PyRuntimeState_INIT sets other fields to 0/NULL */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800112
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600113PyAPI_DATA(_PyRuntimeState) _PyRuntime;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800114PyAPI_FUNC(_PyInitError) _PyRuntimeState_Init(_PyRuntimeState *);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600115PyAPI_FUNC(void) _PyRuntimeState_Fini(_PyRuntimeState *);
116
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800117/* Initialize _PyRuntimeState.
118 Return NULL on success, or return an error message on failure. */
119PyAPI_FUNC(_PyInitError) _PyRuntime_Initialize(void);
120
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600121#define _Py_CURRENTLY_FINALIZING(tstate) \
122 (_PyRuntime.finalizing == tstate)
123
124
125/* Other */
126
Victor Stinnera7368ac2017-11-15 18:11:45 -0800127PyAPI_FUNC(_PyInitError) _PyInterpreterState_Enable(_PyRuntimeState *);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600128
129#ifdef __cplusplus
130}
131#endif
132#endif /* !Py_INTERNAL_PYSTATE_H */