generic_executor_ref.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright (C) 2014 Vicente J. Botet Escriba
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. #ifndef BOOST_THREAD_EXECUTORS_GENERIC_EXECUTOR_REF_HPP
  7. #define BOOST_THREAD_EXECUTORS_GENERIC_EXECUTOR_REF_HPP
  8. #include <boost/thread/detail/config.hpp>
  9. #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION && defined BOOST_THREAD_PROVIDES_EXECUTORS && defined BOOST_THREAD_USES_MOVE
  10. #include <boost/thread/detail/delete.hpp>
  11. #include <boost/thread/detail/move.hpp>
  12. #include <boost/thread/executors/executor.hpp>
  13. #include <boost/shared_ptr.hpp>
  14. #include <boost/config/abi_prefix.hpp>
  15. namespace boost
  16. {
  17. namespace executors
  18. {
  19. template <class Executor>
  20. class executor_ref : public executor
  21. {
  22. Executor& ex;
  23. public:
  24. /// type-erasure to store the works to do
  25. typedef executors::work work;
  26. /// executor is not copyable.
  27. BOOST_THREAD_NO_COPYABLE(executor_ref)
  28. executor_ref(Executor& ex_) : ex(ex_) {}
  29. /**
  30. * \par Effects
  31. * Destroys the executor.
  32. *
  33. * \par Synchronization
  34. * The completion of all the closures happen before the completion of the executor destructor.
  35. */
  36. ~executor_ref() {}
  37. /**
  38. * \par Effects
  39. * Close the \c executor for submissions.
  40. * The worker threads will work until there is no more closures to run.
  41. */
  42. void close() { ex.close(); }
  43. /**
  44. * \par Returns
  45. * Whether the pool is closed for submissions.
  46. */
  47. bool closed() { return ex.closed(); }
  48. /**
  49. * \par Effects
  50. * The specified closure will be scheduled for execution at some point in the future.
  51. * If invoked closure throws an exception the executor will call std::terminate, as is the case with threads.
  52. *
  53. * \par Synchronization
  54. * Ccompletion of closure on a particular thread happens before destruction of thread's thread local variables.
  55. *
  56. * \par Throws
  57. * \c sync_queue_is_closed if the thread pool is closed.
  58. * Whatever exception that can be throw while storing the closure.
  59. */
  60. void submit(BOOST_THREAD_RV_REF(work) closure) {
  61. ex.submit(boost::move(closure));
  62. }
  63. // void submit(work& closure) {
  64. // ex.submit(closure);
  65. // }
  66. /**
  67. * \par Effects
  68. * Try to execute one task.
  69. *
  70. * \par Returns
  71. * Whether a task has been executed.
  72. *
  73. * \par Throws
  74. * Whatever the current task constructor throws or the task() throws.
  75. */
  76. bool try_executing_one() { return ex.try_executing_one(); }
  77. };
  78. class generic_executor_ref
  79. {
  80. shared_ptr<executor> ex;
  81. public:
  82. /// type-erasure to store the works to do
  83. typedef executors::work work;
  84. template<typename Executor>
  85. generic_executor_ref(Executor& ex_)
  86. //: ex(make_shared<executor_ref<Executor> >(ex_)) // todo check why this doesn't works with C++03
  87. : ex( new executor_ref<Executor>(ex_) )
  88. {
  89. }
  90. //generic_executor_ref(generic_executor_ref const& other) noexcept {}
  91. //generic_executor_ref& operator=(generic_executor_ref const& other) noexcept {}
  92. /**
  93. * \par Effects
  94. * Close the \c executor for submissions.
  95. * The worker threads will work until there is no more closures to run.
  96. */
  97. void close() { ex->close(); }
  98. /**
  99. * \par Returns
  100. * Whether the pool is closed for submissions.
  101. */
  102. bool closed() { return ex->closed(); }
  103. /**
  104. * \par Requires
  105. * \c Closure is a model of Callable(void()) and a model of CopyConstructible/MoveConstructible.
  106. *
  107. * \par Effects
  108. * The specified closure will be scheduled for execution at some point in the future.
  109. * If invoked closure throws an exception the thread pool will call std::terminate, as is the case with threads.
  110. *
  111. * \par Synchronization
  112. * Completion of closure on a particular thread happens before destruction of thread's thread local variables.
  113. *
  114. * \par Throws
  115. * \c sync_queue_is_closed if the thread pool is closed.
  116. * Whatever exception that can be throw while storing the closure.
  117. */
  118. void submit(BOOST_THREAD_RV_REF(work) closure)
  119. {
  120. ex->submit(boost::move(closure));
  121. }
  122. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  123. template <typename Closure>
  124. void submit(Closure & closure)
  125. {
  126. //work w ((closure));
  127. //submit(boost::move(w));
  128. submit(work(closure));
  129. }
  130. #endif
  131. void submit(void (*closure)())
  132. {
  133. work w ((closure));
  134. submit(boost::move(w));
  135. //submit(work(closure));
  136. }
  137. template <typename Closure>
  138. void submit(BOOST_THREAD_FWD_REF(Closure) closure)
  139. {
  140. work w((boost::forward<Closure>(closure)));
  141. submit(boost::move(w));
  142. }
  143. // size_t num_pending_closures() const
  144. // {
  145. // return ex->num_pending_closures();
  146. // }
  147. /**
  148. * \par Effects
  149. * Try to execute one task.
  150. *
  151. * \par Returns
  152. * Whether a task has been executed.
  153. *
  154. * \par Throws
  155. * Whatever the current task constructor throws or the task() throws.
  156. */
  157. bool try_executing_one() { return ex->try_executing_one(); }
  158. /**
  159. * \par Requires
  160. * This must be called from an scheduled task.
  161. *
  162. * \par Effects
  163. * reschedule functions until pred()
  164. */
  165. template <typename Pred>
  166. bool reschedule_until(Pred const& pred)
  167. {
  168. do {
  169. //schedule_one_or_yield();
  170. if ( ! try_executing_one())
  171. {
  172. return false;
  173. }
  174. } while (! pred());
  175. return true;
  176. }
  177. };
  178. }
  179. using executors::executor_ref;
  180. using executors::generic_executor_ref;
  181. }
  182. #include <boost/config/abi_suffix.hpp>
  183. #endif
  184. #endif