-
Notifications
You must be signed in to change notification settings - Fork 0
/
InterpreterLockGuard.hpp
49 lines (43 loc) · 1.22 KB
/
InterpreterLockGuard.hpp
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
#pragma once
#include <Python.h>
#include <boost/noncopyable.hpp>
namespace pccl {
namespace python {
// When a C++ function is called by Python and it will be long-running,
// it should release the Global Interpreter Lock (GIL) by constructing one of these.
// This does not mean the C++ function needs to do this when invoked directly from C++:
// it should instead have a wrapper that sits in front of its Python binding, like this:
//
// void MyClass::longRunningFunction(); // native C++ code
//
// void myWrapper(MyClass& my) // local function in Python bindings module
// {
// InterpreterLockGuard guard;
// my.longRunningFunction();
// }
//
// // within BOOST_PYTHON_MODULE():
// class_<MyClass>("MyClass")
// .def("longRunningFunction", myWrapper);
//
// See http://wiki.python.org/moin/boost.python/HowTo#Multithreading_Support_for_my_function
class InterpreterLockGuard
: boost::noncopyable
{
public:
InterpreterLockGuard();
~InterpreterLockGuard();
private:
bool m_doRelease;
};
class InterpreterLockAcquirer
: boost::noncopyable
{
public:
InterpreterLockAcquirer();
~InterpreterLockAcquirer();
private:
PyThreadState* m_threadState;
};
} // namespace python
} // namespace pccl