fixes bug where priorities where lost when force-rechecking.
[libtorrent.git] / bindings / python / src / gil.hpp
blobd9534c92c0cfcf06e3e02c9e10b08ad510bb3371
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)
5 #ifndef GIL_070107_HPP
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);
27 PyThreadState* save;
30 struct lock_gil
32 lock_gil()
33 : state(PyGILState_Ensure())
36 ~lock_gil()
38 PyGILState_Release(state);
41 PyGILState_STATE state;
44 template <class F, class R>
45 struct allow_threading
47 allow_threading(F fn)
48 : fn(fn)
51 template <class A0>
52 R operator()(A0& a0)
54 allow_threading_guard guard;
55 return (a0.*fn)();
58 template <class A0, class A1>
59 R operator()(A0& a0, A1& a1)
61 allow_threading_guard guard;
62 return (a0.*fn)(a1);
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);
93 F fn;
96 template <class F>
97 struct visitor : boost::python::def_visitor<visitor<F> >
99 visitor(F fn)
100 : fn(fn)
103 template <class Class, class Options, class Signature>
104 void visit_aux(
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;
110 cl.def(
111 name
112 , boost::python::make_function(
113 allow_threading<F, return_type>(fn)
114 , options.policies()
115 , options.keywords()
116 , signature
121 template <class Class, class Options>
122 void visit(Class& cl, char const* name, Options const& options) const
124 this->visit_aux(
125 cl, name, options
126 , boost::python::detail::get_signature(fn, (typename Class::wrapped_type*)0)
130 F fn;
133 // Member function adaptor that releases and aqcuires the GIL
134 // around the function call.
135 template <class F>
136 visitor<F> allow_threads(F fn)
138 return visitor<F>(fn);
141 //}} // namespace libtorrent::python
143 #endif // GIL_070107_HPP