1 // Copyright Daniel Wallin 2007. Use, modification and distribution is
2 // subject to the Boost Software License, Version 1.0. (See accompanying
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 # define GIL_070107_HPP
8 # include <boost/python/make_function.hpp>
9 # include <boost/python/def_visitor.hpp>
10 # include <boost/python/signature.hpp>
11 # include <boost/mpl/at.hpp>
13 //namespace libtorrent { namespace python {
15 // RAII helper to release GIL.
16 struct allow_threading_guard
18 allow_threading_guard()
19 : save(PyEval_SaveThread())
22 ~allow_threading_guard()
24 PyEval_RestoreThread(save
);
33 : state(PyGILState_Ensure())
38 PyGILState_Release(state
);
41 PyGILState_STATE state
;
44 template <class F
, class R
>
45 struct allow_threading
54 allow_threading_guard guard
;
58 template <class A0
, class A1
>
59 R
operator()(A0
& a0
, A1
& a1
)
61 allow_threading_guard guard
;
65 template <class A0
, class A1
, class A2
>
66 R
operator()(A0
& a0
, A1
& a1
, A2
& a2
)
68 allow_threading_guard guard
;
69 return (a0
.*fn
)(a1
,a2
);
72 template <class A0
, class A1
, class A2
, class A3
>
73 R
operator()(A0
& a0
, A1
& a1
, A2
& a2
, A3
& a3
)
75 allow_threading_guard guard
;
76 return (a0
.*fn
)(a1
,a2
,a3
);
79 template <class A0
, class A1
, class A2
, class A3
, class A4
>
80 R
operator()(A0
& a0
, A1
& a1
, A2
& a2
, A3
& a3
, A4
& a4
)
82 allow_threading_guard guard
;
83 return (a0
.*fn
)(a1
,a2
,a3
,a4
);
86 template <class A0
, class A1
, class A2
, class A3
, class A4
, class A5
>
87 R
operator()(A0
& a0
, A1
& a1
, A2
& a2
, A3
& a3
, A4
& a4
, A5
& a5
)
89 allow_threading_guard guard
;
90 return (a0
.*fn
)(a1
,a2
,a3
,a4
,a5
);
97 struct visitor
: boost::python::def_visitor
<visitor
<F
> >
103 template <class Class
, class Options
, class Signature
>
105 Class
& cl
, char const* name
106 , Options
const& options
, Signature
const& signature
) const
108 typedef typename
boost::mpl::at_c
<Signature
,0>::type return_type
;
112 , boost::python::make_function(
113 allow_threading
<F
, return_type
>(fn
)
121 template <class Class
, class Options
>
122 void visit(Class
& cl
, char const* name
, Options
const& options
) const
126 , boost::python::detail::get_signature(fn
, (typename
Class::wrapped_type
*)0)
133 // Member function adaptor that releases and aqcuires the GIL
134 // around the function call.
136 visitor
<F
> allow_threads(F fn
)
138 return visitor
<F
>(fn
);
141 //}} // namespace libtorrent::python
143 #endif // GIL_070107_HPP