blob: 20c5946b14f445906947e06401c6d71ae5f1a866 [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;
29 int autoTLSkey;
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
40/* Full Python runtime state */
41
42typedef struct pyruntimestate {
43 int initialized;
44 int core_initialized;
45 PyThreadState *finalizing;
46
47 struct pyinterpreters {
48 PyThread_type_lock mutex;
49 PyInterpreterState *head;
50 PyInterpreterState *main;
51 /* _next_interp_id is an auto-numbered sequence of small
52 integers. It gets initialized in _PyInterpreterState_Init(),
53 which is called in Py_Initialize(), and used in
54 PyInterpreterState_New(). A negative interpreter ID
55 indicates an error occurred. The main interpreter will
56 always have an ID of 0. Overflow results in a RuntimeError.
57 If that becomes a problem later then we can adjust, e.g. by
58 using a Python int. */
59 int64_t next_id;
60 } interpreters;
61
62#define NEXITFUNCS 32
63 void (*exitfuncs[NEXITFUNCS])(void);
64 int nexitfuncs;
65 void (*pyexitfunc)(void);
66
67 struct _pyobj_runtime_state obj;
68 struct _gc_runtime_state gc;
69 struct _pymem_runtime_state mem;
70 struct _warnings_runtime_state warnings;
71 struct _ceval_runtime_state ceval;
72 struct _gilstate_runtime_state gilstate;
73
74 // XXX Consolidate globals found via the check-c-globals script.
75} _PyRuntimeState;
76
77PyAPI_DATA(_PyRuntimeState) _PyRuntime;
78PyAPI_FUNC(void) _PyRuntimeState_Init(_PyRuntimeState *);
79PyAPI_FUNC(void) _PyRuntimeState_Fini(_PyRuntimeState *);
80
81#define _Py_CURRENTLY_FINALIZING(tstate) \
82 (_PyRuntime.finalizing == tstate)
83
84
85/* Other */
86
87PyAPI_FUNC(void) _PyInterpreterState_Enable(_PyRuntimeState *);
88
89#ifdef __cplusplus
90}
91#endif
92#endif /* !Py_INTERNAL_PYSTATE_H */