From ccde3a583b74bca8f8a5b5c67e9ee8c7232990da Mon Sep 17 00:00:00 2001 From: Matthew Andrew <matthew.andrew@stfc.ac.uk> Date: Wed, 15 Aug 2018 10:33:51 +0100 Subject: [PATCH] Commited new class Re #23266 --- .../ReleaseGlobalInterpreterLock.h | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Framework/PythonInterface/inc/MantidPythonInterface/kernel/Environment/ReleaseGlobalInterpreterLock.h diff --git a/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Environment/ReleaseGlobalInterpreterLock.h b/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Environment/ReleaseGlobalInterpreterLock.h new file mode 100644 index 00000000000..8003167db39 --- /dev/null +++ b/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Environment/ReleaseGlobalInterpreterLock.h @@ -0,0 +1,52 @@ +#ifndef MANTID_PYTHONINTERFACE_RELEASEGLOBALINTERPRETERLOCK_H_ +#define MANTID_PYTHONINTERFACE_RELEASEGLOBALINTERPRETERLOCK_H_ + +#include "MantidPythonInterface/kernel/DllConfig.h" +#include <boost/python/detail/wrap_python.hpp> + +namespace Mantid { +namespace PythonInterface { +namespace Environment { + +/** + * Defines a structure for releaseing the Python GIL + * using the RAII pattern. This releases the Python GIL + * for the duration of the current scope. + */ +class PYTHON_KERNEL_DLL ReleaseGlobalInterpreterLock { +public: + /// Default constructor + ReleaseGlobalInterpreterLock(); + /// Destructor + ~ReleaseGlobalInterpreterLock(); + +private: + // Stores the current python trace used to track where in + // a python script you are. + Py_tracefunc m_tracefunc; + PyObject *m_tracearg; + /// Saved thread state + 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 + +#endif /* MANTID_PYTHONINTERFACE_RELEASEGLOBALINTERPRETERLock_H_ */ -- GitLab