diff --git a/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Environment/ReleaseGlobalInterpreterLock.h b/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Environment/ReleaseGlobalInterpreterLock.h index 8003167db39e4d2a45a90f03420cfb7ecb483d87..c1f45931c66f02c8eb85548a111f042f50274070 100644 --- a/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Environment/ReleaseGlobalInterpreterLock.h +++ b/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Environment/ReleaseGlobalInterpreterLock.h @@ -29,22 +29,6 @@ private: PyThreadState *m_saved; }; -ReleaseGlobalInterpreterLock::ReleaseGlobalInterpreterLock() - : m_tracefunc(nullptr), m_tracearg(nullptr), m_saved(nullptr) { - PyThreadState *curThreadState = PyThreadState_GET(); - m_tracefunc = curThreadState->c_tracefunc; - m_tracearg = curThreadState->c_traceobj; - Py_XINCREF(m_tracearg); - PyEval_SetTrace(nullptr, nullptr); - m_saved = PyEval_SaveThread(); -} - -ReleaseGlobalInterpreterLock::~ReleaseGlobalInterpreterLock() { - PyEval_RestoreThread(m_saved); - PyEval_SetTrace(m_tracefunc, m_tracearg); - Py_XDECREF(m_tracearg); -} - } // namespace Environment } // namespace PythonInterface } // namespace Mantid diff --git a/Framework/PythonInterface/mantid/kernel/CMakeLists.txt b/Framework/PythonInterface/mantid/kernel/CMakeLists.txt index ca639abb8e96d43ade974d3463057629f8b535ec..f78be07e854e006d2e3c425723db81b549d7bbba 100644 --- a/Framework/PythonInterface/mantid/kernel/CMakeLists.txt +++ b/Framework/PythonInterface/mantid/kernel/CMakeLists.txt @@ -89,6 +89,7 @@ set ( SRC_FILES src/Registry/TypeRegistry.cpp src/Environment/ErrorHandling.cpp src/Environment/GlobalInterpreterLock.cpp + src/Environment/ReleaseGlobalInterpreterLock.cpp src/Environment/WrapperHelpers.cpp ) diff --git a/Framework/PythonInterface/mantid/kernel/src/Environment/ReleaseGlobalInterpreterLock.cpp b/Framework/PythonInterface/mantid/kernel/src/Environment/ReleaseGlobalInterpreterLock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89d8b4c56a0d437bb77a961f8d0faf1bc045eea5 --- /dev/null +++ b/Framework/PythonInterface/mantid/kernel/src/Environment/ReleaseGlobalInterpreterLock.cpp @@ -0,0 +1,32 @@ +#include "MantidPythonInterface/kernel/Environment/ReleaseGlobalInterpreterLock.h" + +namespace Mantid { +namespace PythonInterface { +namespace Environment { + +/** + * Ensures this thread releases the Python GIL also save trace information + * to be restored upon destruction. + */ +ReleaseGlobalInterpreterLock::ReleaseGlobalInterpreterLock() + : m_tracefunc(nullptr), m_tracearg(nullptr), m_saved(nullptr) { + PyThreadState *curThreadState = PyThreadState_GET(); + m_tracefunc = curThreadState->c_tracefunc; + m_tracearg = curThreadState->c_traceobj; + Py_XINCREF(m_tracearg); + PyEval_SetTrace(nullptr, nullptr); + m_saved = PyEval_SaveThread(); +} + +/** + * Restores the Python GIL to the thread when the object falls out of scope. + */ +ReleaseGlobalInterpreterLock::~ReleaseGlobalInterpreterLock() { + PyEval_RestoreThread(m_saved); + PyEval_SetTrace(m_tracefunc, m_tracearg); + Py_XDECREF(m_tracearg); +} + +} // namespace Environment +} // namespace PythonInterface +} // namespace Mantid \ No newline at end of file