executor.hpp 4.1 KB

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