1010#pragma once
1111
1212#include " detail/common.h"
13- #include " detail/internals.h"
13+
14+ #if defined(WITH_THREAD) && !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
15+ # include " detail/internals.h"
16+ #endif
1417
1518PYBIND11_NAMESPACE_BEGIN (PYBIND11_NAMESPACE)
1619
@@ -21,7 +24,9 @@ PyThreadState *get_thread_state_unchecked();
2124
2225PYBIND11_NAMESPACE_END (detail)
2326
24- #if defined(WITH_THREAD) && !defined(PYPY_VERSION) && !defined(PYBIND11_SIMPLE_GIL)
27+ #if defined(WITH_THREAD)
28+
29+ # if !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
2530
2631/* The functions below essentially reproduce the PyGILState_* API using a RAII
2732 * pattern, but there are a few important differences:
@@ -62,11 +67,11 @@ class gil_scoped_acquire {
6267
6368 if (!tstate) {
6469 tstate = PyThreadState_New (internals.istate );
65- # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
70+ # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
6671 if (!tstate) {
6772 pybind11_fail (" scoped_acquire: could not create thread state!" );
6873 }
69- # endif
74+ # endif
7075 tstate->gilstate_counter = 0 ;
7176 PYBIND11_TLS_REPLACE_VALUE (internals.tstate , tstate);
7277 } else {
@@ -87,20 +92,20 @@ class gil_scoped_acquire {
8792
8893 PYBIND11_NOINLINE void dec_ref () {
8994 --tstate->gilstate_counter ;
90- # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
95+ # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
9196 if (detail::get_thread_state_unchecked () != tstate) {
9297 pybind11_fail (" scoped_acquire::dec_ref(): thread state must be current!" );
9398 }
9499 if (tstate->gilstate_counter < 0 ) {
95100 pybind11_fail (" scoped_acquire::dec_ref(): reference count underflow!" );
96101 }
97- # endif
102+ # endif
98103 if (tstate->gilstate_counter == 0 ) {
99- # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
104+ # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
100105 if (!release) {
101106 pybind11_fail (" scoped_acquire::dec_ref(): internal error!" );
102107 }
103- # endif
108+ # endif
104109 PyThreadState_Clear (tstate);
105110 if (active) {
106111 PyThreadState_DeleteCurrent ();
@@ -178,12 +183,14 @@ class gil_scoped_release {
178183 bool disassoc;
179184 bool active = true ;
180185};
181- #elif defined(PYPY_VERSION) || defined(PYBIND11_SIMPLE_GIL)
186+
187+ # else // PYBIND11_SIMPLE_GIL_MANAGEMENT
188+
182189class gil_scoped_acquire {
183190 PyGILState_STATE state;
184191
185192public:
186- gil_scoped_acquire () { state = PyGILState_Ensure (); }
193+ gil_scoped_acquire () : state{ PyGILState_Ensure ()} { }
187194 gil_scoped_acquire (const gil_scoped_acquire &) = delete ;
188195 gil_scoped_acquire &operator =(const gil_scoped_acquire &) = delete ;
189196 ~gil_scoped_acquire () { PyGILState_Release (state); }
@@ -194,19 +201,39 @@ class gil_scoped_release {
194201 PyThreadState *state;
195202
196203public:
197- gil_scoped_release () { state = PyEval_SaveThread (); }
204+ gil_scoped_release () : state{ PyEval_SaveThread ()} { }
198205 gil_scoped_release (const gil_scoped_release &) = delete ;
199206 gil_scoped_release &operator =(const gil_scoped_acquire &) = delete ;
200207 ~gil_scoped_release () { PyEval_RestoreThread (state); }
201208 void disarm () {}
202209};
203- #else
210+
211+ # endif // PYBIND11_SIMPLE_GIL_MANAGEMENT
212+
213+ #else // WITH_THREAD
214+
204215class gil_scoped_acquire {
216+ public:
217+ gil_scoped_acquire () {
218+ // Trick to suppress `unused variable` error messages (at call sites).
219+ (void ) (this != (this + 1 ));
220+ }
221+ gil_scoped_acquire (const gil_scoped_acquire &) = delete ;
222+ gil_scoped_acquire &operator =(const gil_scoped_acquire &) = delete ;
205223 void disarm () {}
206224};
225+
207226class gil_scoped_release {
227+ public:
228+ gil_scoped_release () {
229+ // Trick to suppress `unused variable` error messages (at call sites).
230+ (void ) (this != (this + 1 ));
231+ }
232+ gil_scoped_release (const gil_scoped_release &) = delete ;
233+ gil_scoped_release &operator =(const gil_scoped_acquire &) = delete ;
208234 void disarm () {}
209235};
210- #endif
236+
237+ #endif // WITH_THREAD
211238
212239PYBIND11_NAMESPACE_END (PYBIND11_NAMESPACE)
0 commit comments