scheduler.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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_SCHEDULER_HPP
  7. #define BOOST_THREAD_EXECUTORS_SCHEDULER_HPP
  8. #include <boost/thread/detail/config.hpp>
  9. #include <boost/thread/executors/detail/scheduled_executor_base.hpp>
  10. #include <boost/chrono/time_point.hpp>
  11. #include <boost/chrono/duration.hpp>
  12. #include <boost/chrono/system_clocks.hpp>
  13. #include <boost/config/abi_prefix.hpp>
  14. namespace boost
  15. {
  16. namespace executors
  17. {
  18. /// Wraps the reference to an executor and a function to make a work that submit the function using the executor.
  19. template <class Executor, class Function>
  20. class resubmitter
  21. {
  22. public:
  23. resubmitter(Executor& ex, Function funct) :
  24. ex(ex),
  25. funct(boost::move(funct))
  26. {}
  27. void operator()()
  28. {
  29. ex.submit(funct);
  30. }
  31. private:
  32. Executor& ex;
  33. Function funct;
  34. };
  35. /// resubmitter factory
  36. template <class Executor, class Function>
  37. resubmitter<Executor, typename decay<Function>::type>
  38. resubmit(Executor& ex, BOOST_THREAD_FWD_REF(Function) funct) {
  39. return resubmitter<Executor, typename decay<Function>::type >(ex, boost::move(funct));
  40. }
  41. /// Wraps references to a @c Scheduler and an @c Executor providing an @c Executor that
  42. /// resubmit the function using the referenced Executor at a given @c time_point known at construction.
  43. template <class Scheduler, class Executor>
  44. class resubmit_at_executor
  45. {
  46. public:
  47. typedef typename Scheduler::clock clock;
  48. typedef typename Scheduler::work work;
  49. template <class Duration>
  50. resubmit_at_executor(Scheduler& sch, Executor& ex, chrono::time_point<clock, Duration> const& tp) :
  51. sch(sch),
  52. ex(ex),
  53. tp(tp),
  54. is_closed(false)
  55. {
  56. }
  57. ~resubmit_at_executor()
  58. {
  59. close();
  60. }
  61. template <class Work>
  62. void submit(BOOST_THREAD_FWD_REF(Work) w)
  63. {
  64. if (closed())
  65. {
  66. BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
  67. }
  68. sch.submit_at(resubmit(ex,boost::forward<Work>(w)), tp);
  69. }
  70. Executor& underlying_executor()
  71. {
  72. return ex;
  73. }
  74. Scheduler& underlying_scheduler()
  75. {
  76. return sch;
  77. }
  78. void close()
  79. {
  80. is_closed = true;
  81. }
  82. bool closed()
  83. {
  84. return is_closed || sch.closed() || ex.closed();
  85. }
  86. private:
  87. Scheduler& sch;
  88. Executor& ex;
  89. typename clock::time_point tp;
  90. bool is_closed;
  91. };
  92. /// Expression template helper storing a pair of references to an @c Scheduler and an @c Executor
  93. /// It provides factory helper functions such as at/after that convert these a pair of @c Scheduler @c Executor
  94. /// into an new @c Executor that submit the work using the referenced @c Executor at/after a specific time/duration
  95. /// respectively, using the referenced @Scheduler.
  96. template <class Scheduler, class Executor>
  97. class scheduler_executor_wrapper
  98. {
  99. public:
  100. typedef typename Scheduler::clock clock;
  101. typedef typename Scheduler::work work;
  102. typedef resubmit_at_executor<Scheduler, Executor> the_executor;
  103. scheduler_executor_wrapper(Scheduler& sch, Executor& ex) :
  104. sch(sch),
  105. ex(ex)
  106. {}
  107. ~scheduler_executor_wrapper()
  108. {
  109. }
  110. Executor& underlying_executor()
  111. {
  112. return ex;
  113. }
  114. Scheduler& underlying_scheduler()
  115. {
  116. return sch;
  117. }
  118. template <class Rep, class Period>
  119. the_executor after(chrono::duration<Rep,Period> const& rel_time)
  120. {
  121. return at(clock::now() + rel_time );
  122. }
  123. template <class Duration>
  124. the_executor at(chrono::time_point<clock,Duration> const& abs_time)
  125. {
  126. return the_executor(sch, ex, abs_time);
  127. }
  128. private:
  129. Scheduler& sch;
  130. Executor& ex;
  131. }; //end class
  132. /// Wraps a reference to a @c Scheduler providing an @c Executor that
  133. /// run the function at a given @c time_point known at construction.
  134. template <class Scheduler>
  135. class at_executor
  136. {
  137. public:
  138. typedef typename Scheduler::clock clock;
  139. typedef typename Scheduler::work work;
  140. typedef typename clock::time_point time_point;
  141. template <class Duration>
  142. at_executor(Scheduler& sch, chrono::time_point<clock,Duration> const& tp) :
  143. sch(sch),
  144. tp(tp),
  145. is_closed(false)
  146. {}
  147. ~at_executor()
  148. {
  149. close();
  150. }
  151. Scheduler& underlying_scheduler()
  152. {
  153. return sch;
  154. }
  155. void close()
  156. {
  157. is_closed = true;
  158. }
  159. bool closed()
  160. {
  161. return is_closed || sch.closed();
  162. }
  163. template <class Work>
  164. void submit(BOOST_THREAD_FWD_REF(Work) w)
  165. {
  166. if (closed())
  167. {
  168. BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
  169. }
  170. sch.submit_at(boost::forward<Work>(w), tp);
  171. }
  172. template <class Executor>
  173. resubmit_at_executor<Scheduler, Executor> on(Executor& ex)
  174. {
  175. return resubmit_at_executor<Scheduler, Executor>(sch, ex, tp);
  176. }
  177. private:
  178. Scheduler& sch;
  179. time_point tp;
  180. bool is_closed;
  181. }; //end class
  182. /// A @c Scheduler using a specific thread. Note that a Scheduler is not an Executor.
  183. /// It provides factory helper functions such as at/after that convert a @c Scheduler into an @c Executor
  184. /// that submit the work at/after a specific time/duration respectively.
  185. template <class Clock = chrono::steady_clock>
  186. class scheduler : public detail::scheduled_executor_base<Clock>
  187. {
  188. public:
  189. typedef typename detail::scheduled_executor_base<Clock>::work work;
  190. typedef Clock clock;
  191. scheduler()
  192. : super(),
  193. thr(&super::loop, this) {}
  194. ~scheduler()
  195. {
  196. this->close();
  197. thr.join();
  198. }
  199. template <class Ex>
  200. scheduler_executor_wrapper<scheduler, Ex> on(Ex& ex)
  201. {
  202. return scheduler_executor_wrapper<scheduler, Ex>(*this, ex);
  203. }
  204. template <class Rep, class Period>
  205. at_executor<scheduler> after(chrono::duration<Rep,Period> const& rel_time)
  206. {
  207. return at(rel_time + clock::now());
  208. }
  209. template <class Duration>
  210. at_executor<scheduler> at(chrono::time_point<clock,Duration> const& tp)
  211. {
  212. return at_executor<scheduler>(*this, tp);
  213. }
  214. private:
  215. typedef detail::scheduled_executor_base<Clock> super;
  216. thread thr;
  217. };
  218. }
  219. using executors::resubmitter;
  220. using executors::resubmit;
  221. using executors::resubmit_at_executor;
  222. using executors::scheduler_executor_wrapper;
  223. using executors::at_executor;
  224. using executors::scheduler;
  225. }
  226. #include <boost/config/abi_suffix.hpp>
  227. #endif