Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
fbafdea6
Commit
fbafdea6
authored
Apr 25, 2016
by
Wenzel Jakob
Browse files
a few more GIL-related compatibility fixes
parent
17b10d7c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
10 deletions
+38
-10
include/pybind11/cast.h
include/pybind11/cast.h
+12
-0
include/pybind11/common.h
include/pybind11/common.h
+7
-0
include/pybind11/pybind11.h
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
=
PyT
hread
S
tate_
GET
()
!=
tstate
;
release
=
detail
::
get_t
hread
_s
tate_
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
(
PyT
hread
S
tate_
GET
()
!=
tstate
)
if
(
detail
::
get_t
hread
_s
tate_
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_
s
et_key_value
(
detail
::
get_internals
().
tstate
,
nullptr
);
PyThread_
del
et
e
_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