File tree 2 files changed +22
-3
lines changed
2 files changed +22
-3
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ endif()
27
27
28
28
option (PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT} )
29
29
option (PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT} )
30
+ option (PYBIND11_USE_PYGILSTATE_API "Use PyGILState API for gil management?" OFF )
30
31
31
32
list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR} /tools" )
32
33
@@ -101,7 +102,9 @@ if(NOT (CMAKE_VERSION VERSION_LESS 3.0)) # CMake >= 3.0
101
102
$<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS} >
102
103
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR} >)
103
104
target_compile_options (pybind11 INTERFACE $<BUILD_INTERFACE:${PYBIND11_CPP_STANDARD} >)
104
-
105
+ if ( PYBIND11_USE_PYGILSTATE_API )
106
+ target_compile_definitions (pybind11 INTERFACE PYBIND11_USE_PYGILSTATE_API)
107
+ endif ()
105
108
add_library (module INTERFACE )
106
109
add_library (pybind11::module ALIAS module)
107
110
if (NOT MSVC )
Original file line number Diff line number Diff line change @@ -1866,8 +1866,22 @@ void print(Args &&...args) {
1866
1866
detail::print (c.args (), c.kwargs ());
1867
1867
}
1868
1868
1869
- #if defined(WITH_THREAD) && !defined(PYPY_VERSION)
1869
+ #if defined(PYPY_VERSION)
1870
+ class gil_scoped_acquire {
1871
+ PyGILState_STATE state;
1872
+ public:
1873
+ gil_scoped_acquire () { state = PyGILState_Ensure (); }
1874
+ ~gil_scoped_acquire () { PyGILState_Release (state); }
1875
+ };
1870
1876
1877
+ class gil_scoped_release {
1878
+ PyThreadState *state;
1879
+ public:
1880
+ gil_scoped_release () { state = PyEval_SaveThread (); }
1881
+ ~gil_scoped_release () { PyEval_RestoreThread (state); }
1882
+ };
1883
+ #else
1884
+ #if defined(WITH_THREAD) && !defined(PYBIND11_USE_PYGILSTATE_API)
1871
1885
/* The functions below essentially reproduce the PyGILState_* API using a RAII
1872
1886
* pattern, but there are a few important differences:
1873
1887
*
@@ -1992,7 +2006,7 @@ class gil_scoped_release {
1992
2006
PyThreadState *tstate;
1993
2007
bool disassoc;
1994
2008
};
1995
- #elif defined(PYPY_VERSION )
2009
+ #elif defined(PYBIND11_USE_PYGILSTATE_API )
1996
2010
class gil_scoped_acquire {
1997
2011
PyGILState_STATE state;
1998
2012
public:
@@ -2011,6 +2025,8 @@ class gil_scoped_acquire { };
2011
2025
class gil_scoped_release { };
2012
2026
#endif
2013
2027
2028
+ #endif
2029
+
2014
2030
error_already_set::~error_already_set () {
2015
2031
if (m_type) {
2016
2032
gil_scoped_acquire gil;
You can’t perform that action at this time.
0 commit comments