detail/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>
37 {
38 public:
39  typedef Result const & result_type;
40 
41  typedef Result future_result_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 
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 >
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;
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 
Result future_result_type
Indicates the future&#39;s result type.
Future< future_result_type > future_type
Indicates the future&#39;s type.
void set_value(future_result_type const &r) volatile
The namespace threadpool contains a thread pool and related utility classes.
Smart pointer with a scoped locking mechanism.
Definition: locking_ptr.hpp:37
volatile future_result_type m_result
future_impl_task_func(function_type const &function, shared_ptr< future_type > const &future)
result_of< function_type()>::type future_result_type
Indicates the future&#39;s result type.
Experimental. Do not use in production code. TODO.
Definition: future.hpp:43
void set_execution_status(bool executing) volatile
result_type operator()() const volatile
Result const & result_type
Indicates the functor&#39;s result type.
void result_type
Indicates the functor&#39;s result type.
future_impl< future_result_type > future_type
Function function_type
Indicates the function&#39;s type.
The locking_ptr is smart pointer with a scoped locking mechanism.
bool timed_wait(boost::xtime const &timestamp) const


asr_descriptor_surface_based_recognition
Author(s): Allgeyer Tobias, Hutmacher Robin, Meißner Pascal
autogenerated on Mon Dec 16 2019 03:31:15