forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GlobalInterpreterLock.cpp
57 lines (48 loc) · 1.95 KB
/
GlobalInterpreterLock.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 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/GlobalInterpreterLock.h"
#include "MantidPythonInterface/core/VersionCompat.h"
namespace Mantid::PythonInterface {
//------------------------------------------------------------------------------
// GlobalInterpreterLock Static helpers
//------------------------------------------------------------------------------
/**
* Check if the current thread has the lock
* @return True if the current thread holds the GIL, false otherwise
*/
bool GlobalInterpreterLock::locked() {
#if defined(IS_PY3K)
return (PyGILState_Check() == 1);
#else
PyThreadState *ts = _PyThreadState_Current;
return (ts && ts == PyGILState_GetThisThreadState());
#endif
}
/**
* @return A handle to the Python threadstate before the acquire() call.
*/
PyGILState_STATE GlobalInterpreterLock::acquire() { return PyGILState_Ensure(); }
/**
* There must be have been a call to acquire() to create the tstate value given
* here.
* @param tstate The Python threadstate returned by the matching call to
* acquire()
*/
void GlobalInterpreterLock::release(PyGILState_STATE tstate) { PyGILState_Release(tstate); }
//------------------------------------------------------------------------------
// GlobalInterpreterLock Public members
//------------------------------------------------------------------------------
/**
* Ensures this thread is ready to call Python code
*/
GlobalInterpreterLock::GlobalInterpreterLock() : m_state(this->acquire()) {}
/**
* Resets the Python threadstate to the state before
* this object was created.
*/
GlobalInterpreterLock::~GlobalInterpreterLock() { this->release(m_state); }
} // namespace Mantid::PythonInterface