future.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*! \file
  2. * \brief TODO.
  3. *
  4. * TODO.
  5. *
  6. * Copyright (c) 2005-2007 Philipp Henkel
  7. *
  8. * Use, modification, and distribution are subject to the
  9. * Boost Software License, Version 1.0. (See accompanying file
  10. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  11. *
  12. * http://threadpool.sourceforge.net
  13. *
  14. */
  15. #ifndef THREADPOOL_DETAIL_FUTURE_IMPL_HPP_INCLUDED
  16. #define THREADPOOL_DETAIL_FUTURE_IMPL_HPP_INCLUDED
  17. #include "locking_ptr.hpp"
  18. #include <boost/smart_ptr.hpp>
  19. #include <boost/optional.hpp>
  20. #include <boost/thread/mutex.hpp>
  21. #include <boost/thread/condition.hpp>
  22. #include <boost/thread/xtime.hpp>
  23. #include <boost/utility/result_of.hpp>
  24. #include <boost/static_assert.hpp>
  25. #include <boost/type_traits.hpp>
  26. namespace boost { namespace threadpool { namespace detail
  27. {
  28. template<class Result>
  29. class future_impl
  30. {
  31. public:
  32. typedef Result const & result_type; //!< Indicates the functor's result type.
  33. typedef Result future_result_type; //!< Indicates the future's result type.
  34. typedef future_impl<future_result_type> future_type;
  35. private:
  36. volatile bool m_ready;
  37. volatile future_result_type m_result;
  38. mutable mutex m_monitor;
  39. mutable condition m_condition_ready;
  40. volatile bool m_is_cancelled;
  41. volatile bool m_executing;
  42. public:
  43. public:
  44. future_impl()
  45. : m_ready(false)
  46. , m_is_cancelled(false)
  47. {
  48. }
  49. bool ready() const volatile
  50. {
  51. return m_ready;
  52. }
  53. void wait() const volatile
  54. {
  55. const future_type* self = const_cast<const future_type*>(this);
  56. mutex::scoped_lock lock(self->m_monitor);
  57. while(!m_ready)
  58. {
  59. self->m_condition_ready.wait(lock);
  60. }
  61. }
  62. bool timed_wait(boost::xtime const & timestamp) const
  63. {
  64. const future_type* self = const_cast<const future_type*>(this);
  65. mutex::scoped_lock lock(self->m_monitor);
  66. while(!m_ready)
  67. {
  68. if(!self->m_condition_ready.timed_wait(lock, timestamp)) return false;
  69. }
  70. return true;
  71. }
  72. result_type operator()() const volatile
  73. {
  74. wait();
  75. /*
  76. if( throw_exception_ != 0 )
  77. {
  78. throw_exception_( this );
  79. }
  80. */
  81. return *(const_cast<const future_result_type*>(&m_result));
  82. }
  83. void set_value(future_result_type const & r) volatile
  84. {
  85. locking_ptr<future_type, mutex> lockedThis(*this, m_monitor);
  86. if(!m_ready && !m_is_cancelled)
  87. {
  88. lockedThis->m_result = r;
  89. lockedThis->m_ready = true;
  90. lockedThis->m_condition_ready.notify_all();
  91. }
  92. }
  93. /*
  94. template<class E> void set_exception() // throw()
  95. {
  96. m_impl->template set_exception<E>();
  97. }
  98. template<class E> void set_exception( char const * what ) // throw()
  99. {
  100. m_impl->template set_exception<E>( what );
  101. }
  102. */
  103. bool cancel() volatile
  104. {
  105. if(!m_ready || m_executing)
  106. {
  107. m_is_cancelled = true;
  108. return true;
  109. }
  110. else
  111. {
  112. return false;
  113. }
  114. }
  115. bool is_cancelled() const volatile
  116. {
  117. return m_is_cancelled;
  118. }
  119. void set_execution_status(bool executing) volatile
  120. {
  121. m_executing = executing;
  122. }
  123. };
  124. template<
  125. template <typename> class Future,
  126. typename Function
  127. >
  128. class future_impl_task_func
  129. {
  130. public:
  131. typedef void result_type; //!< Indicates the functor's result type.
  132. typedef Function function_type; //!< Indicates the function's type.
  133. typedef typename result_of<function_type()>::type future_result_type; //!< Indicates the future's result type.
  134. typedef Future<future_result_type> future_type; //!< Indicates the future's type.
  135. // The task is required to be a nullary function.
  136. BOOST_STATIC_ASSERT(function_traits<function_type()>::arity == 0);
  137. // The task function's result type is required not to be void.
  138. BOOST_STATIC_ASSERT(!is_void<future_result_type>::value);
  139. private:
  140. function_type m_function;
  141. shared_ptr<future_type> m_future;
  142. public:
  143. future_impl_task_func(function_type const & function, shared_ptr<future_type> const & future)
  144. : m_function(function)
  145. , m_future(future)
  146. {
  147. }
  148. void operator()()
  149. {
  150. if(m_function)
  151. {
  152. m_future->set_execution_status(true);
  153. if(!m_future->is_cancelled())
  154. {
  155. // TODO future exeception handling
  156. m_future->set_value(m_function());
  157. }
  158. m_future->set_execution_status(false); // TODO consider exceptions
  159. }
  160. }
  161. };
  162. } } } // namespace boost::threadpool::detail
  163. #endif // THREADPOOL_DETAIL_FUTURE_IMPL_HPP_INCLUDED