scheduler.hpp 7.1 KB

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