serial_executor.hpp 5.6 KB

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