executor.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (C) 2013,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. // 2013/09 Vicente J. Botet Escriba
  7. // Adapt to boost from CCIA C++11 implementation
  8. #ifndef BOOST_THREAD_EXECUTORS_EXECUTOR_HPP
  9. #define BOOST_THREAD_EXECUTORS_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/executors/work.hpp>
  14. #include <boost/config/abi_prefix.hpp>
  15. namespace boost
  16. {
  17. namespace executors
  18. {
  19. class executor
  20. {
  21. public:
  22. /// type-erasure to store the works to do
  23. typedef executors::work work;
  24. /// executor is not copyable.
  25. BOOST_THREAD_NO_COPYABLE(executor)
  26. executor() {}
  27. /**
  28. * \par Effects
  29. * Destroys the executor.
  30. *
  31. * \par Synchronization
  32. * The completion of all the closures happen before the completion of the executor destructor.
  33. */
  34. virtual ~executor() {}
  35. /**
  36. * \par Effects
  37. * Close the \c executor for submissions.
  38. * The worker threads will work until there is no more closures to run.
  39. */
  40. virtual void close() = 0;
  41. /**
  42. * \par Returns
  43. * Whether the pool is closed for submissions.
  44. */
  45. virtual bool closed() = 0;
  46. /**
  47. * \par Effects
  48. * The specified closure will be scheduled for execution at some point in the future.
  49. * If invoked closure throws an exception the executor will call std::terminate, as is the case with threads.
  50. *
  51. * \par Synchronization
  52. * Ccompletion of closure on a particular thread happens before destruction of thread's thread local variables.
  53. *
  54. * \par Throws
  55. * \c sync_queue_is_closed if the thread pool is closed.
  56. * Whatever exception that can be throw while storing the closure.
  57. */
  58. virtual void submit(BOOST_THREAD_RV_REF(work) closure) = 0;
  59. // virtual void submit(work& closure) = 0;
  60. /**
  61. * \par Requires
  62. * \c Closure is a model of Callable(void()) and a model of CopyConstructible/MoveConstructible.
  63. *
  64. * \par Effects
  65. * The specified closure will be scheduled for execution at some point in the future.
  66. * If invoked closure throws an exception the thread pool will call std::terminate, as is the case with threads.
  67. *
  68. * \par Synchronization
  69. * Completion of closure on a particular thread happens before destruction of thread's thread local variables.
  70. *
  71. * \par Throws
  72. * \c sync_queue_is_closed if the thread pool is closed.
  73. * Whatever exception that can be throw while storing the closure.
  74. */
  75. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  76. template <typename Closure>
  77. void submit(Closure & closure)
  78. {
  79. work w ((closure));
  80. submit(boost::move(w));
  81. }
  82. #endif
  83. void submit(void (*closure)())
  84. {
  85. work w ((closure));
  86. submit(boost::move(w));
  87. }
  88. template <typename Closure>
  89. void submit(BOOST_THREAD_FWD_REF(Closure) closure)
  90. {
  91. //submit(work(boost::forward<Closure>(closure)));
  92. work w((boost::forward<Closure>(closure)));
  93. submit(boost::move(w));
  94. }
  95. /**
  96. * \par Effects
  97. * Try to execute one task.
  98. *
  99. * \par Returns
  100. * Whether a task has been executed.
  101. *
  102. * \par Throws
  103. * Whatever the current task constructor throws or the task() throws.
  104. */
  105. virtual bool try_executing_one() = 0;
  106. /**
  107. * \par Requires
  108. * This must be called from an scheduled task.
  109. *
  110. * \par Effects
  111. * Reschedule functions until pred()
  112. */
  113. template <typename Pred>
  114. bool reschedule_until(Pred const& pred)
  115. {
  116. do {
  117. //schedule_one_or_yield();
  118. if ( ! try_executing_one())
  119. {
  120. return false;
  121. }
  122. } while (! pred());
  123. return true;
  124. }
  125. };
  126. }
  127. using executors::executor;
  128. }
  129. #include <boost/config/abi_suffix.hpp>
  130. #endif