-
Notifications
You must be signed in to change notification settings - Fork 0
/
InterpreterLockGuard.cpp
65 lines (55 loc) · 1.22 KB
/
InterpreterLockGuard.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
58
59
60
61
62
63
64
65
#include "InterpreterLockGuard.hpp"
#include <boost/assert.hpp>
#include <boost/thread/tss.hpp>
namespace pccl {
namespace python {
namespace {
void cleanup(PyThreadState*)
{
}
boost::thread_specific_ptr<PyThreadState> g_threadState(cleanup);
}
InterpreterLockGuard::InterpreterLockGuard()
: m_doRelease(false)
{
if (!g_threadState.get() && PyEval_ThreadsInitialized())
{
g_threadState.reset(PyEval_SaveThread());
m_doRelease = true;
}
}
InterpreterLockGuard::~InterpreterLockGuard()
{
if (m_doRelease)
{
if (g_threadState.get())
{
PyEval_RestoreThread(g_threadState.get());
g_threadState.reset();
}
else
{
// This should never happen.
BOOST_ASSERT(false);
}
}
}
InterpreterLockAcquirer::InterpreterLockAcquirer()
: m_threadState(g_threadState.get())
{
if (m_threadState)
{
PyEval_RestoreThread(m_threadState);
g_threadState.reset();
}
}
InterpreterLockAcquirer::~InterpreterLockAcquirer()
{
if (m_threadState)
{
BOOST_ASSERT(!g_threadState.get());
g_threadState.reset(PyEval_SaveThread());
}
}
} // namespace python
} // namespace pccl