Make the generated reverse wrappers (virtual methods) python thread safe, i.e. they now acquire / release the Python GIL when called from C/C++ code, but only if Python threading was initialized (PyEval_ThreadsInitialized).
This commit is contained in:
1 parent
3f8b90adba
commit
1a0c9ff1b1
2 files changed
+12
No files matched your search
@@ -585,6 +585,16 @@ class ReverseWrapperBase(object):
|
||||
assert isinstance(decl_modifiers, (list, tuple))
|
||||
assert all([isinstance(mod, str) for mod in decl_modifiers])
|
||||
|
||||
|
||||
## reverse wrappers are called from C/C++ code, when the Python GIL may not be held...
|
||||
gil_state_var = self.declarations.declare_variable('PyGILState_STATE', '__py_gil_state')
|
||||
self.before_call.write_code('%s = (PyEval_ThreadsInitialized() ? PyGILState_Ensure() : (PyGILState_STATE) 0);'
|
||||
% gil_state_var)
|
||||
self.before_call.add_cleanup_code('if (PyEval_ThreadsInitialized())\n'
|
||||
' PyGILState_Release(%s);' % gil_state_var)
|
||||
|
||||
|
||||
|
||||
self.declarations.declare_variable('PyObject*', 'py_retval')
|
||||
if self.return_value.ctype != 'void' \
|
||||
and not self.return_value.REQUIRES_ASSIGNMENT_CONSTRUCTOR:
|
||||
|
||||
@@ -40,6 +40,8 @@ def write_preamble(code_sink, min_python_version=None):
|
||||
code_sink.writeln(r'''
|
||||
#if PY_VERSION_HEX < 0x02040000
|
||||
|
||||
#define PyEval_ThreadsInitialized() 1
|
||||
|
||||
#define Py_CLEAR(op) \
|
||||
do { \
|
||||
if (op) { \
|
||||
|
||||
Reference in new issue
Block a user