forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReleaseGlobalInterpreterLock.cpp
34 lines (30 loc) · 1.18 KB
/
ReleaseGlobalInterpreterLock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidPythonInterface/core/ReleaseGlobalInterpreterLock.h"
namespace Mantid::PythonInterface {
/**
* 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 Mantid::PythonInterface