10
10
#pragma once
11
11
12
12
#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
14
17
15
18
PYBIND11_NAMESPACE_BEGIN (PYBIND11_NAMESPACE)
16
19
@@ -21,7 +24,9 @@ PyThreadState *get_thread_state_unchecked();
21
24
22
25
PYBIND11_NAMESPACE_END (detail)
23
26
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)
25
30
26
31
/* The functions below essentially reproduce the PyGILState_* API using a RAII
27
32
* pattern, but there are a few important differences:
@@ -62,11 +67,11 @@ class gil_scoped_acquire {
62
67
63
68
if (!tstate) {
64
69
tstate = PyThreadState_New (internals.istate );
65
- # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
70
+ # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
66
71
if (!tstate) {
67
72
pybind11_fail (" scoped_acquire: could not create thread state!" );
68
73
}
69
- # endif
74
+ # endif
70
75
tstate->gilstate_counter = 0 ;
71
76
PYBIND11_TLS_REPLACE_VALUE (internals.tstate , tstate);
72
77
} else {
@@ -87,20 +92,20 @@ class gil_scoped_acquire {
87
92
88
93
PYBIND11_NOINLINE void dec_ref () {
89
94
--tstate->gilstate_counter ;
90
- # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
95
+ # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
91
96
if (detail::get_thread_state_unchecked () != tstate) {
92
97
pybind11_fail (" scoped_acquire::dec_ref(): thread state must be current!" );
93
98
}
94
99
if (tstate->gilstate_counter < 0 ) {
95
100
pybind11_fail (" scoped_acquire::dec_ref(): reference count underflow!" );
96
101
}
97
- # endif
102
+ # endif
98
103
if (tstate->gilstate_counter == 0 ) {
99
- # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
104
+ # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
100
105
if (!release) {
101
106
pybind11_fail (" scoped_acquire::dec_ref(): internal error!" );
102
107
}
103
- # endif
108
+ # endif
104
109
PyThreadState_Clear (tstate);
105
110
if (active) {
106
111
PyThreadState_DeleteCurrent ();
@@ -178,12 +183,14 @@ class gil_scoped_release {
178
183
bool disassoc;
179
184
bool active = true ;
180
185
};
181
- #elif defined(PYPY_VERSION) || defined(PYBIND11_SIMPLE_GIL)
186
+
187
+ # else // PYBIND11_SIMPLE_GIL_MANAGEMENT
188
+
182
189
class gil_scoped_acquire {
183
190
PyGILState_STATE state;
184
191
185
192
public:
186
- gil_scoped_acquire () { state = PyGILState_Ensure (); }
193
+ gil_scoped_acquire () : state{ PyGILState_Ensure ()} { }
187
194
gil_scoped_acquire (const gil_scoped_acquire &) = delete ;
188
195
gil_scoped_acquire &operator =(const gil_scoped_acquire &) = delete ;
189
196
~gil_scoped_acquire () { PyGILState_Release (state); }
@@ -194,19 +201,39 @@ class gil_scoped_release {
194
201
PyThreadState *state;
195
202
196
203
public:
197
- gil_scoped_release () { state = PyEval_SaveThread (); }
204
+ gil_scoped_release () : state{ PyEval_SaveThread ()} { }
198
205
gil_scoped_release (const gil_scoped_release &) = delete ;
199
206
gil_scoped_release &operator =(const gil_scoped_acquire &) = delete ;
200
207
~gil_scoped_release () { PyEval_RestoreThread (state); }
201
208
void disarm () {}
202
209
};
203
- #else
210
+
211
+ # endif // PYBIND11_SIMPLE_GIL_MANAGEMENT
212
+
213
+ #else // WITH_THREAD
214
+
204
215
class 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 ;
205
223
void disarm () {}
206
224
};
225
+
207
226
class 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 ;
208
234
void disarm () {}
209
235
};
210
- #endif
236
+
237
+ #endif // WITH_THREAD
211
238
212
239
PYBIND11_NAMESPACE_END (PYBIND11_NAMESPACE)
0 commit comments