12#include "detail/common.h"
13#include "detail/internals.h"
24#if defined(WITH_THREAD) && !defined(PYPY_VERSION)
51 auto &
internals = detail::get_internals();
60 tstate = PyGILState_GetThisThreadState();
64 tstate = PyThreadState_New(
internals.istate);
67 pybind11_fail(
"scoped_acquire: could not create thread state!");
70 tstate->gilstate_counter = 0;
73 release = detail::get_thread_state_unchecked() != tstate;
77 PyEval_AcquireThread(tstate);
83 void inc_ref() { ++tstate->gilstate_counter; }
86 --tstate->gilstate_counter;
88 if (detail::get_thread_state_unchecked() != tstate) {
89 pybind11_fail(
"scoped_acquire::dec_ref(): thread state must be current!");
91 if (tstate->gilstate_counter < 0) {
92 pybind11_fail(
"scoped_acquire::dec_ref(): reference count underflow!");
95 if (tstate->gilstate_counter == 0) {
101 PyThreadState_Clear(tstate);
103 PyThreadState_DeleteCurrent();
125 PyThreadState *tstate =
nullptr;
136 auto &
internals = detail::get_internals();
138 tstate = PyEval_SaveThread();
160 PyEval_RestoreThread(tstate);
165 auto key = detail::get_internals().tstate;
171 PyThreadState *tstate;
175#elif defined(PYPY_VERSION)
177 PyGILState_STATE state;
186 PyThreadState *state;
PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Thrown when pybind11::cast or.
#define PYBIND11_NOINLINE
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)
#define PYBIND11_TLS_GET_VALUE(key)
#define PYBIND11_TLS_DELETE_VALUE(key)
#define PYBIND11_TLS_REPLACE_VALUE(key, value)
PyThreadState * get_thread_state_unchecked()
Internal data structure used to track registered instances and types.