12#include "detail/common.h"
14#if defined(WITH_THREAD) && !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
15# include "detail/internals.h"
27#if defined(WITH_THREAD)
29# if !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
56 auto &
internals = detail::get_internals();
65 tstate = PyGILState_GetThisThreadState();
69 tstate = PyThreadState_New(
internals.istate);
70# if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
72 pybind11_fail(
"scoped_acquire: could not create thread state!");
75 tstate->gilstate_counter = 0;
78 release = detail::get_thread_state_unchecked() != tstate;
82 PyEval_AcquireThread(tstate);
91 void inc_ref() { ++tstate->gilstate_counter; }
94 --tstate->gilstate_counter;
95# if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
96 if (detail::get_thread_state_unchecked() != tstate) {
97 pybind11_fail(
"scoped_acquire::dec_ref(): thread state must be current!");
99 if (tstate->gilstate_counter < 0) {
100 pybind11_fail(
"scoped_acquire::dec_ref(): reference count underflow!");
103 if (tstate->gilstate_counter == 0) {
104# if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
109 PyThreadState_Clear(tstate);
111 PyThreadState_DeleteCurrent();
133 PyThreadState *tstate =
nullptr;
144 auto &
internals = detail::get_internals();
146 tstate = PyEval_SaveThread();
171 PyEval_RestoreThread(tstate);
176 auto key = detail::get_internals().tstate;
182 PyThreadState *tstate;
190 PyGILState_STATE state;
201 PyThreadState *state;
219 (void) (
this != (
this + 1));
230 (void) (
this != (
this + 1));
gil_scoped_acquire(const gil_scoped_acquire &)=delete
gil_scoped_acquire & operator=(const gil_scoped_acquire &)=delete
gil_scoped_release & operator=(const gil_scoped_release &)=delete
gil_scoped_release(const gil_scoped_release &)=delete
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.