| The Gaudi Framework v28r2-patches (bc4ad17a)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

future.hpp
Go to the documentation of this file.
1
17 #ifndef THREADPOOL_DETAIL_FUTURE_IMPL_HPP_INCLUDED
18 #define THREADPOOL_DETAIL_FUTURE_IMPL_HPP_INCLUDED
19
20
21 #include "locking_ptr.hpp"
22
23 #include <boost/smart_ptr.hpp>
24 #include <boost/optional.hpp>
25 #include <boost/thread/mutex.hpp>
26 #include <boost/thread/condition.hpp>
27 #include <boost/thread/xtime.hpp>
28 #include <boost/utility/result_of.hpp>
29 #include <boost/static_assert.hpp>
30 #include <boost/type_traits.hpp>
31
32 namespace boost { namespace threadpool { namespace detail
33 {
34
35 template<class Result>
36 class future_impl
37 {
38 public:
39 typedef Result const & result_type;
40
41 typedef Result future_result_type;
42 typedef future_impl<future_result_type> future_type;
43
44 private:
45 volatile bool m_ready;
46 volatile future_result_type m_result;
47
48 mutable mutex m_monitor;
49 mutable condition m_condition_ready;
50
51 volatile bool m_is_cancelled;
52 volatile bool m_executing;
53
54 public:
55
56
57 public:
58
59 future_impl()
60 : m_ready(false)
61 , m_is_cancelled(false)
62 {
63 }
64
65 bool ready() const volatile
66 {
67 return m_ready;
68 }
69
70 void wait() const volatile
71 {
72 const future_type* self = const_cast<const future_type*>(this);
73 mutex::scoped_lock lock(self->m_monitor);
74
75 while(!m_ready)
76 {
77 self->m_condition_ready.wait(lock);
78 }
79 }
80
81
82 bool timed_wait(boost::xtime const & timestamp) const
83 {
84 const future_type* self = const_cast<const future_type*>(this);
85 mutex::scoped_lock lock(self->m_monitor);
86
87 while(!m_ready)
88 {
89 if(!self->m_condition_ready.timed_wait(lock, timestamp)) return false;
90 }
91
92 return true;
93 }
94
95
96 result_type operator()() const volatile
97 {
98 wait();
99 /*
100 if( throw_exception_ != 0 )
101 {
102 throw_exception_( this );
103 }
104 */
105
106 return *(const_cast<const future_result_type*>(&m_result));
107 }
108
109
110 void set_value(future_result_type const & r) volatile
111 {
112 locking_ptr<future_type, mutex> lockedThis(*this, m_monitor);
113 if(!m_ready && !m_is_cancelled)
114 {
115 lockedThis->m_result = r;
116 lockedThis->m_ready = true;
117 lockedThis->m_condition_ready.notify_all();
118 }
119 }
120 /*
121 template<class E> void set_exception() // throw()
122 {
123 m_impl->template set_exception<E>();
124 }
125
126 template<class E> void set_exception( char const * what ) // throw()
127 {
128 m_impl->template set_exception<E>( what );
129 }
130 */
131
132
133 bool cancel() volatile
134 {
135 if(!m_ready || m_executing)
136 {
137 m_is_cancelled = true;
138 return true;
139 }
140 else
141 {
142 return false;
143 }
144 }
145
146
147 bool is_cancelled() const volatile
148 {
149 return m_is_cancelled;
150 }
151
152
153 void set_execution_status(bool executing) volatile
154 {
155 m_executing = executing;
156 }
157 };
158
159
160 template<
161 template <typename> class Future,
162 typename Function
163 >
164 class future_impl_task_func
165 {
166
167 public:
168 typedef void result_type;
169
170 typedef Function function_type;
171 typedef typename result_of<function_type()>::type future_result_type;
172 typedef Future<future_result_type> future_type;
173
174 // The task is required to be a nullary function.
175 BOOST_STATIC_ASSERT(function_traits<function_type()>::arity == 0);
176
177 // The task function's result type is required not to be void.
178 BOOST_STATIC_ASSERT(!is_void<future_result_type>::value);
179
180 private:
181 function_type m_function;
182 shared_ptr<future_type> m_future;
183
184 public:
185 future_impl_task_func(function_type const & function, shared_ptr<future_type> const & future)
186 : m_function(function)
187 , m_future(future)
188 {
189 }
190
191 void operator()()
192 {
193 if(m_function)
194 {
195 m_future->set_execution_status(true);
196 if(!m_future->is_cancelled())
197 {
198 // TODO future exeception handling
199 m_future->set_value(m_function());
200 }
201 m_future->set_execution_status(false); // TODO consider exceptions
202 }
203 }
204
205 };
206
207
208
209
210
211 } } } // namespace boost::threadpool::detail
212
213 #endif // THREADPOOL_DETAIL_FUTURE_IMPL_HPP_INCLUDED
214
215
boost::threadpool::detail::future_impl::future_result_type
Result future_result_type
Indicates the future&#39;s result type.
Definition: future.hpp:41
boost::threadpool::detail::future_impl_task_func::future_type
Future< future_result_type > future_type
Indicates the future&#39;s type.
Definition: future.hpp:172
boost::threadpool::detail::future_impl::set_value
void set_value(future_result_type const &r) volatile
Definition: future.hpp:110
boost
The namespace threadpool contains a thread pool and related utility classes.
Definition: iter_pos.hpp:13
boost::threadpool::detail::locking_ptr
Smart pointer with a scoped locking mechanism.
Definition: locking_ptr.hpp:37
boost::threadpool::detail::future_impl::m_result
volatile future_result_type m_result
Definition: future.hpp:46
boost::threadpool::detail::future_impl_task_func::future_impl_task_func
future_impl_task_func(function_type const &function, shared_ptr< future_type > const &future)
Definition: future.hpp:185
boost::threadpool::detail::future_impl::m_monitor
mutex m_monitor
Definition: future.hpp:48
boost::threadpool::detail::future_impl_task_func::future_result_type
result_of< function_type()>::type future_result_type
Indicates the future&#39;s result type.
Definition: future.hpp:171
boost::threadpool::detail::future_impl_task_func::m_future
shared_ptr< future_type > m_future
Definition: future.hpp:182
boost::threadpool::detail::future_impl::wait
void wait() const volatile
Definition: future.hpp:70
GaudiMath::Function
Genfun::AbsFunction Function
Definition: GaudiMath.h:24
boost::threadpool::future
Experimental.
Definition: future.hpp:43
boost::threadpool::detail::future_impl::set_execution_status
void set_execution_status(bool executing) volatile
Definition: future.hpp:153
boost::threadpool::detail::future_impl::operator()
result_type operator()() const volatile
Definition: future.hpp:96
boost::threadpool::detail::future_impl::future_impl
future_impl()
Definition: future.hpp:59
boost::threadpool::detail::future_impl::m_ready
volatile bool m_ready
Definition: future.hpp:45
detail
TupleObj.h GaudiAlg/TupleObj.h namespace with few technical implementations.
boost::threadpool::detail::future_impl::m_executing
volatile bool m_executing
Definition: future.hpp:52
gaudirun.type
type
Definition: gaudirun.py:125
boost::threadpool::detail::future_impl::result_type
Result const & result_type
Indicates the functor&#39;s result type.
Definition: future.hpp:39
boost::threadpool::detail::future_impl::m_condition_ready
condition m_condition_ready
Definition: future.hpp:49
boost::threadpool::detail::future_impl_task_func::operator()
void operator()()
Definition: future.hpp:191
boost::threadpool::detail::future_impl::cancel
bool cancel() volatile
Definition: future.hpp:133
boost::threadpool::detail::future_impl_task_func::result_type
void result_type
Indicates the functor&#39;s result type.
Definition: future.hpp:168
boost::threadpool::detail::future_impl::ready
bool ready() const volatile
Definition: future.hpp:65
boost::threadpool::detail::future_impl::future_type
future_impl< future_result_type > future_type
Definition: future.hpp:42
boost::threadpool::detail::future_impl
Definition: future.hpp:36
boost::threadpool::detail::future_impl_task_func::m_function
function_type m_function
Definition: future.hpp:181
boost::threadpool::detail::future_impl_task_func
Definition: future.hpp:164
boost::threadpool::detail::future_impl_task_func::function_type
Function function_type
Indicates the function&#39;s type.
Definition: future.hpp:170
boost::threadpool::detail::future_impl::m_is_cancelled
volatile bool m_is_cancelled
Definition: future.hpp:51
locking_ptr.hpp
The locking_ptr is smart pointer with a scoped locking mechanism.
boost::threadpool::detail::future_impl::timed_wait
bool timed_wait(boost::xtime const &timestamp) const
Definition: future.hpp:82
boost::threadpool::detail::future_impl::is_cancelled
bool is_cancelled() const volatile
Definition: future.hpp:147