1 | // Copyright John Zwinck 2012.
|
---|
2 | // Distributed under the Boost Software License, Version 1.0. (See
|
---|
3 | // accompanying file LICENSE_1_0.txt or copy at
|
---|
4 | // http://www.boost.org/LICENSE_1_0.txt)
|
---|
5 | #ifndef BOOST_PYTHON_GIL_JZ201212_HPP
|
---|
6 | # define BOOST_PYTHON_GIL_JZ201212_HPP
|
---|
7 |
|
---|
8 | # include <boost/python/detail/prefix.hpp>
|
---|
9 | # include <boost/noncopyable.hpp>
|
---|
10 |
|
---|
11 | namespace boost { namespace python {
|
---|
12 |
|
---|
13 | // When a C++ function is called by Python and it will be long-running,
|
---|
14 | // it should release the Global Interpreter Lock (GIL) by constructing one of these.
|
---|
15 | // This does not mean the C++ function needs to do this when invoked directly from C++:
|
---|
16 | // it should instead have a wrapper that sits in front of its Python binding, like this:
|
---|
17 | //
|
---|
18 | // void MyClass::longRunningFunction(); // native C++ code
|
---|
19 | //
|
---|
20 | // void myWrapper(MyClass& my) // local function in Python bindings module
|
---|
21 | // {
|
---|
22 | // gil_guard_release guard;
|
---|
23 | // my.longRunningFunction();
|
---|
24 | // }
|
---|
25 | //
|
---|
26 | // // within BOOST_PYTHON_MODULE():
|
---|
27 | // class_<MyClass>("MyClass")
|
---|
28 | // .def("longRunningFunction", myWrapper);
|
---|
29 | //
|
---|
30 | // See http://wiki.python.org/moin/boost.python/HowTo#Multithreading_Support_for_my_function
|
---|
31 | class gil_guard_release
|
---|
32 | : boost::noncopyable
|
---|
33 | {
|
---|
34 | public:
|
---|
35 | gil_guard_release();
|
---|
36 | ~gil_guard_release();
|
---|
37 |
|
---|
38 | private:
|
---|
39 | bool m_do_acquire;
|
---|
40 | };
|
---|
41 |
|
---|
42 | class gil_guard_acquire
|
---|
43 | : boost::noncopyable
|
---|
44 | {
|
---|
45 | public:
|
---|
46 | gil_guard_acquire();
|
---|
47 | ~gil_guard_acquire();
|
---|
48 |
|
---|
49 | private:
|
---|
50 | PyThreadState* m_thread_state;
|
---|
51 | };
|
---|
52 |
|
---|
53 | }} // namespace boost::python
|
---|
54 |
|
---|
55 | #endif // BOOST_PYTHON_GIL_JZ201212_HPP
|
---|