Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
fbafdea6
Commit
fbafdea6
authored
Apr 25, 2016
by
Wenzel Jakob
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
a few more GIL-related compatibility fixes
parent
17b10d7c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
10 deletions
+38
-10
include/pybind11/cast.h
+12
-0
include/pybind11/common.h
+7
-0
include/pybind11/pybind11.h
+19
-10
No files found.
include/pybind11/cast.h
View file @
fbafdea6
...
...
@@ -117,6 +117,18 @@ PYBIND11_NOINLINE inline handle get_object_handle(const void *ptr) {
return
handle
((
PyObject
*
)
it
->
second
);
}
inline
PyThreadState
*
get_thread_state_unchecked
()
{
#if PY_VERSION_HEX < 0x03000000
return
_PyThreadState_Current
;
#elif PY_VERSION_HEX < 0x03050000
return
(
PyThreadState
*
)
_Py_atomic_load_relaxed
(
&
_PyThreadState_Current
);
#elif PY_VERSION_HEX < 0x03050200
return
(
PyThreadState
*
)
_PyThreadState_Current
.
value
;
#else
return
_PyThreadState_UncheckedGet
();
#endif
}
class
type_caster_generic
{
public
:
PYBIND11_NOINLINE
type_caster_generic
(
const
std
::
type_info
&
type_info
)
...
...
include/pybind11/common.h
View file @
fbafdea6
...
...
@@ -110,6 +110,13 @@
extern "C" PYBIND11_EXPORT PyObject *init##name()
#endif
#if PY_VERSION_HEX >= 0x03050000 && PY_VERSION_HEX < 0x03050200
extern
"C"
{
struct
_Py_atomic_address
{
void
*
value
;
};
PyAPI_DATA
(
_Py_atomic_address
)
_PyThreadState_Current
;
};
#endif
#define PYBIND11_TRY_NEXT_OVERLOAD ((PyObject *) 1) // special failure return code
#define PYBIND11_STRINGIFY(x) #x
#define PYBIND11_TOSTRING(x) PYBIND11_STRINGIFY(x)
...
...
include/pybind11/pybind11.h
View file @
fbafdea6
...
...
@@ -1053,11 +1053,13 @@ template <typename InputType, typename OutputType> void implicitly_convertible()
*
* 3. The reference count of an acquired thread state can be controlled. This
* can be handy to prevent cases where callbacks issued from an external
* thread constantly construct and destroy thread state data structures. */
* thread would otherwise constantly construct and destroy thread state data
* structures.
*/
class
gil_scoped_acquire
{
public
:
gil_scoped_acquire
()
{
PYBIND11_NOINLINE
gil_scoped_acquire
()
{
auto
const
&
internals
=
detail
::
get_internals
();
tstate
=
(
PyThreadState
*
)
PyThread_get_key_value
(
internals
.
tstate
);
...
...
@@ -1068,17 +1070,24 @@ public:
pybind11_fail
(
"scoped_acquire: could not create thread state!"
);
#endif
tstate
->
gilstate_counter
=
0
;
#if PY_MAJOR_VERSION < 3
PyThread_delete_key_value
(
internals
.
tstate
);
#endif
PyThread_set_key_value
(
internals
.
tstate
,
tstate
);
}
else
{
release
=
PyThreadState_GET
()
!=
tstate
;
release
=
detail
::
get_thread_state_unchecked
()
!=
tstate
;
}
if
(
release
)
{
PyInterpreterState
*
interp
=
tstate
->
interp
;
/* Work around an annoying assertion in PyThreadState_Swap */
tstate
->
interp
=
nullptr
;
#if defined(Py_DEBUG)
PyInterpreterState
*
interp
=
tstate
->
interp
;
tstate
->
interp
=
nullptr
;
#endif
PyEval_AcquireThread
(
tstate
);
tstate
->
interp
=
interp
;
#if defined(Py_DEBUG)
tstate
->
interp
=
interp
;
#endif
}
inc_ref
();
...
...
@@ -1088,10 +1097,10 @@ public:
++
tstate
->
gilstate_counter
;
}
void
dec_ref
()
{
PYBIND11_NOINLINE
void
dec_ref
()
{
--
tstate
->
gilstate_counter
;
#if !defined(NDEBUG)
if
(
PyThreadState_GET
()
!=
tstate
)
if
(
detail
::
get_thread_state_unchecked
()
!=
tstate
)
pybind11_fail
(
"scoped_acquire::dec_ref(): thread state must be current!"
);
if
(
tstate
->
gilstate_counter
<
0
)
pybind11_fail
(
"scoped_acquire::dec_ref(): reference count underflow!"
);
...
...
@@ -1103,12 +1112,12 @@ public:
#endif
PyThreadState_Clear
(
tstate
);
PyThreadState_DeleteCurrent
();
PyThread_
set_key_value
(
detail
::
get_internals
().
tstate
,
nullptr
);
PyThread_
delete_key_value
(
detail
::
get_internals
().
tstate
);
release
=
false
;
}
}
~
gil_scoped_acquire
()
{
PYBIND11_NOINLINE
~
gil_scoped_acquire
()
{
dec_ref
();
if
(
release
)
PyEval_SaveThread
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment