1#[cfg(all(Py_3_10, not(PyPy), not(Py_LIMITED_API)))]
2use crate::frameobject::PyFrameObject;
3use crate::moduleobject::PyModuleDef;
4use crate::object::PyObject;
5use std::os::raw::c_int;
6
7#[cfg(not(PyPy))]
8use std::os::raw::c_long;
9
10pub const MAX_CO_EXTRA_USERS: c_int = 255;
11
12opaque_struct!(pub PyThreadState);
13opaque_struct!(pub PyInterpreterState);
14
15extern "C" {
16 #[cfg(not(PyPy))]
17 pub fn PyInterpreterState_New() -> *mut PyInterpreterState;
18 #[cfg(not(PyPy))]
19 pub fn PyInterpreterState_Clear(arg1: *mut PyInterpreterState);
20 #[cfg(not(PyPy))]
21 pub fn PyInterpreterState_Delete(arg1: *mut PyInterpreterState);
22
23 #[cfg(all(Py_3_9, not(PyPy)))]
24 pub fn PyInterpreterState_Get() -> *mut PyInterpreterState;
25
26 #[cfg(all(Py_3_8, not(PyPy)))]
27 pub fn PyInterpreterState_GetDict(arg1: *mut PyInterpreterState) -> *mut PyObject;
28
29 #[cfg(not(PyPy))]
30 pub fn PyInterpreterState_GetID(arg1: *mut PyInterpreterState) -> i64;
31
32 #[cfg_attr(PyPy, link_name = "PyPyState_AddModule")]
33 pub fn PyState_AddModule(arg1: *mut PyObject, arg2: *mut PyModuleDef) -> c_int;
34
35 #[cfg_attr(PyPy, link_name = "PyPyState_RemoveModule")]
36 pub fn PyState_RemoveModule(arg1: *mut PyModuleDef) -> c_int;
37
38 #[cfg_attr(all(PyPy, Py_3_10), link_name = "PyPyState_FindModule")]
40 pub fn PyState_FindModule(arg1: *mut PyModuleDef) -> *mut PyObject;
41
42 #[cfg_attr(PyPy, link_name = "PyPyThreadState_New")]
43 pub fn PyThreadState_New(arg1: *mut PyInterpreterState) -> *mut PyThreadState;
44 #[cfg_attr(PyPy, link_name = "PyPyThreadState_Clear")]
45 pub fn PyThreadState_Clear(arg1: *mut PyThreadState);
46 #[cfg_attr(PyPy, link_name = "PyPyThreadState_Delete")]
47 pub fn PyThreadState_Delete(arg1: *mut PyThreadState);
48
49 #[cfg_attr(PyPy, link_name = "PyPyThreadState_Get")]
50 pub fn PyThreadState_Get() -> *mut PyThreadState;
51}
52
53#[inline]
54pub unsafe fn PyThreadState_GET() -> *mut PyThreadState {
55 PyThreadState_Get()
56}
57
58extern "C" {
59 #[cfg_attr(PyPy, link_name = "PyPyThreadState_Swap")]
60 pub fn PyThreadState_Swap(arg1: *mut PyThreadState) -> *mut PyThreadState;
61 #[cfg_attr(PyPy, link_name = "PyPyThreadState_GetDict")]
62 pub fn PyThreadState_GetDict() -> *mut PyObject;
63 #[cfg(not(PyPy))]
64 pub fn PyThreadState_SetAsyncExc(arg1: c_long, arg2: *mut PyObject) -> c_int;
65}
66
67extern "C" {
71 #[cfg(all(Py_3_10, not(PyPy), not(Py_LIMITED_API)))]
73 pub fn PyThreadState_GetFrame(arg1: *mut PyThreadState) -> *mut PyFrameObject;
74}
75
76#[repr(C)]
77#[derive(Copy, Clone, Debug, PartialEq, Eq)]
78pub enum PyGILState_STATE {
79 PyGILState_LOCKED,
80 PyGILState_UNLOCKED,
81}
82
83#[cfg(not(Py_3_14))]
84struct HangThread;
85
86#[cfg(not(Py_3_14))]
87impl Drop for HangThread {
88 fn drop(&mut self) {
89 loop {
90 #[cfg(target_family = "unix")]
91 unsafe {
92 libc::pause();
93 }
94 #[cfg(not(target_family = "unix"))]
95 std::thread::sleep(std::time::Duration::from_secs(9_999_999));
96 }
97 }
98}
99
100mod raw {
109 #[cfg(all(not(Py_3_14), rustc_has_extern_c_unwind))]
110 extern "C-unwind" {
111 #[cfg_attr(PyPy, link_name = "PyPyGILState_Ensure")]
112 pub fn PyGILState_Ensure() -> super::PyGILState_STATE;
113 }
114
115 #[cfg(not(all(not(Py_3_14), rustc_has_extern_c_unwind)))]
116 extern "C" {
117 #[cfg_attr(PyPy, link_name = "PyPyGILState_Ensure")]
118 pub fn PyGILState_Ensure() -> super::PyGILState_STATE;
119 }
120}
121
122#[cfg(not(Py_3_14))]
123pub unsafe extern "C" fn PyGILState_Ensure() -> PyGILState_STATE {
124 let guard = HangThread;
125 let ret: PyGILState_STATE = raw::PyGILState_Ensure();
144 std::mem::forget(guard);
145 ret
146}
147
148#[cfg(Py_3_14)]
149pub use self::raw::PyGILState_Ensure;
150
151extern "C" {
152 #[cfg_attr(PyPy, link_name = "PyPyGILState_Release")]
153 pub fn PyGILState_Release(arg1: PyGILState_STATE);
154 #[cfg(not(PyPy))]
155 pub fn PyGILState_GetThisThreadState() -> *mut PyThreadState;
156}