serial_executor.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright (C) 2013 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. // 2013/11 Vicente J. Botet Escriba
  7. // first implementation of a simple serial scheduler.
  8. #ifndef BOOST_THREAD_SERIAL_EXECUTOR_HPP
  9. #define BOOST_THREAD_SERIAL_EXECUTOR_HPP
  10. #include <boost/thread/detail/config.hpp>
  11. #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION && defined BOOST_THREAD_PROVIDES_EXECUTORS && defined BOOST_THREAD_USES_MOVE
  12. #include <exception>
  13. #include <boost/thread/detail/delete.hpp>
  14. #include <boost/thread/detail/move.hpp>
  15. #include <boost/thread/concurrent_queues/sync_queue.hpp>
  16. #include <boost/thread/executors/work.hpp>
  17. #include <boost/thread/executors/generic_executor_ref.hpp>
  18. #include <boost/thread/future.hpp>
  19. #include <boost/thread/scoped_thread.hpp>
  20. #include <boost/config/abi_prefix.hpp>
  21. #if defined(BOOST_MSVC)
  22. # pragma warning(push)
  23. # pragma warning(disable: 4355) // 'this' : used in base member initializer list
  24. #endif
  25. namespace boost
  26. {
  27. namespace executors
  28. {
  29. class serial_executor
  30. {
  31. public:
  32. /// type-erasure to store the works to do
  33. typedef executors::work work;
  34. private:
  35. typedef scoped_thread<> thread_t;
  36. /// the thread safe work queue
  37. concurrent::sync_queue<work > work_queue;
  38. generic_executor_ref ex;
  39. thread_t thr;
  40. struct try_executing_one_task {
  41. work& task;
  42. boost::promise<void> &p;
  43. try_executing_one_task(work& task, boost::promise<void> &p)
  44. : task(task), p(p) {}
  45. void operator()() {
  46. try {
  47. task();
  48. p.set_value();
  49. } catch (...)
  50. {
  51. p.set_exception(current_exception());
  52. }
  53. }
  54. };
  55. public:
  56. /**
  57. * \par Returns
  58. * The underlying executor wrapped on a generic executor reference.
  59. */
  60. generic_executor_ref& underlying_executor() BOOST_NOEXCEPT { return ex; }
  61. /**
  62. * Effects: try to execute one task.
  63. * Returns: whether a task has been executed.
  64. * Throws: whatever the current task constructor throws or the task() throws.
  65. */
  66. bool try_executing_one()
  67. {
  68. work task;
  69. try
  70. {
  71. if (work_queue.try_pull(task) == queue_op_status::success)
  72. {
  73. boost::promise<void> p;
  74. try_executing_one_task tmp(task,p);
  75. ex.submit(tmp);
  76. p.get_future().wait();
  77. return true;
  78. }
  79. return false;
  80. }
  81. catch (...)
  82. {
  83. std::terminate();
  84. //return false;
  85. }
  86. }
  87. private:
  88. /**
  89. * Effects: schedule one task or yields
  90. * Throws: whatever the current task constructor throws or the task() throws.
  91. */
  92. void schedule_one_or_yield()
  93. {
  94. if ( ! try_executing_one())
  95. {
  96. this_thread::yield();
  97. }
  98. }
  99. /**
  100. * The main loop of the worker thread
  101. */
  102. void worker_thread()
  103. {
  104. while (!closed())
  105. {
  106. schedule_one_or_yield();
  107. }
  108. while (try_executing_one())
  109. {
  110. }
  111. }
  112. public:
  113. /// serial_executor is not copyable.
  114. BOOST_THREAD_NO_COPYABLE(serial_executor)
  115. /**
  116. * \b Effects: creates a thread pool that runs closures using one of its closure-executing methods.
  117. *
  118. * \b Throws: Whatever exception is thrown while initializing the needed resources.
  119. */
  120. template <class Executor>
  121. serial_executor(Executor& ex)
  122. : ex(ex), thr(&serial_executor::worker_thread, this)
  123. {
  124. }
  125. /**
  126. * \b Effects: Destroys the thread pool.
  127. *
  128. * \b Synchronization: The completion of all the closures happen before the completion of the \c serial_executor destructor.
  129. */
  130. ~serial_executor()
  131. {
  132. // signal to the worker thread that there will be no more submissions.
  133. close();
  134. }
  135. /**
  136. * \b Effects: close the \c serial_executor for submissions.
  137. * The loop will work until there is no more closures to run.
  138. */
  139. void close()
  140. {
  141. work_queue.close();
  142. }
  143. /**
  144. * \b Returns: whether the pool is closed for submissions.
  145. */
  146. bool closed()
  147. {
  148. return work_queue.closed();
  149. }
  150. /**
  151. * \b Requires: \c Closure is a model of \c Callable(void()) and a model of \c CopyConstructible/MoveConstructible.
  152. *
  153. * \b Effects: The specified \c closure will be scheduled for execution at some point in the future.
  154. * If invoked closure throws an exception the \c serial_executor will call \c std::terminate, as is the case with threads.
  155. *
  156. * \b Synchronization: completion of \c closure on a particular thread happens before destruction of thread's thread local variables.
  157. *
  158. * \b Throws: \c sync_queue_is_closed if the thread pool is closed.
  159. * Whatever exception that can be throw while storing the closure.
  160. */
  161. void submit(BOOST_THREAD_RV_REF(work) closure)
  162. {
  163. work_queue.push(boost::move(closure));
  164. }
  165. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  166. template <typename Closure>
  167. void submit(Closure & closure)
  168. {
  169. submit(work(closure));
  170. }
  171. #endif
  172. void submit(void (*closure)())
  173. {
  174. submit(work(closure));
  175. }
  176. template <typename Closure>
  177. void submit(BOOST_THREAD_FWD_REF(Closure) closure)
  178. {
  179. work w((boost::forward<Closure>(closure)));
  180. submit(boost::move(w));
  181. }
  182. /**
  183. * \b Requires: This must be called from an scheduled task.
  184. *
  185. * \b Effects: reschedule functions until pred()
  186. */
  187. template <typename Pred>
  188. bool reschedule_until(Pred const& pred)
  189. {
  190. do {
  191. if ( ! try_executing_one())
  192. {
  193. return false;
  194. }
  195. } while (! pred());
  196. return true;
  197. }
  198. };
  199. }
  200. using executors::serial_executor;
  201. }
  202. #if defined(BOOST_MSVC)
  203. # pragma warning(pop)
  204. #endif
  205. #include <boost/config/abi_suffix.hpp>
  206. #endif
  207. #endif