executor_adaptor.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_ADAPTOR_HPP
  9. #define BOOST_THREAD_EXECUTORS_EXECUTOR_ADAPTOR_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/executors/executor.hpp>
  13. #include <boost/config/abi_prefix.hpp>
  14. namespace boost
  15. {
  16. namespace executors
  17. {
  18. /**
  19. * Polymorphic adaptor of a model of Executor to an executor.
  20. */
  21. template <typename Executor>
  22. class executor_adaptor : public executor
  23. {
  24. Executor ex;
  25. public:
  26. /// type-erasure to store the works to do
  27. typedef executor::work work;
  28. /// executor is not copyable.
  29. BOOST_THREAD_NO_COPYABLE(executor_adaptor)
  30. /**
  31. * executor_adaptor constructor
  32. */
  33. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  34. template <typename ...Args>
  35. executor_adaptor(BOOST_THREAD_RV_REF(Args) ... args) : ex(boost::forward<Args>(args)...) {}
  36. #else
  37. /**
  38. * executor_adaptor constructor
  39. */
  40. executor_adaptor() : ex() {}
  41. template <typename A1>
  42. executor_adaptor(
  43. BOOST_THREAD_FWD_REF(A1) a1
  44. ) :
  45. ex(
  46. boost::forward<A1>(a1)
  47. ) {}
  48. template <typename A1, typename A2>
  49. executor_adaptor(
  50. BOOST_THREAD_FWD_REF(A1) a1,
  51. BOOST_THREAD_FWD_REF(A2) a2
  52. ) :
  53. ex(
  54. boost::forward<A1>(a1),
  55. boost::forward<A2>(a2)
  56. ) {}
  57. template <typename A1, typename A2, typename A3>
  58. executor_adaptor(
  59. BOOST_THREAD_FWD_REF(A1) a1,
  60. BOOST_THREAD_FWD_REF(A2) a2,
  61. BOOST_THREAD_FWD_REF(A3) a3
  62. ) :
  63. ex(
  64. boost::forward<A1>(a1),
  65. boost::forward<A2>(a2),
  66. boost::forward<A3>(a3)
  67. ) {}
  68. #endif
  69. Executor& underlying_executor() { return ex; }
  70. /**
  71. * \b Effects: close the \c executor for submissions.
  72. * The worker threads will work until there is no more closures to run.
  73. */
  74. void close() { ex.close(); }
  75. /**
  76. * \b Returns: whether the pool is closed for submissions.
  77. */
  78. bool closed() { return ex.closed(); }
  79. /**
  80. * \b Effects: The specified closure will be scheduled for execution at some point in the future.
  81. * If invoked closure throws an exception the executor will call std::terminate, as is the case with threads.
  82. *
  83. * \b Synchronization: completion of closure on a particular thread happens before destruction of thread's thread local variables.
  84. *
  85. * \b Throws: \c sync_queue_is_closed if the thread pool is closed.
  86. * Whatever exception that can be throw while storing the closure.
  87. */
  88. void submit(BOOST_THREAD_RV_REF(work) closure) {
  89. return ex.submit(boost::move(closure));
  90. }
  91. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  92. template <typename Closure>
  93. void submit(Closure & closure)
  94. {
  95. submit(work(closure));
  96. }
  97. #endif
  98. void submit(void (*closure)())
  99. {
  100. submit(work(closure));
  101. }
  102. template <typename Closure>
  103. void submit(BOOST_THREAD_FWD_REF(Closure) closure)
  104. {
  105. //submit(work(boost::forward<Closure>(closure)));
  106. work w((boost::forward<Closure>(closure)));
  107. submit(boost::move(w));
  108. }
  109. /**
  110. * Effects: try to execute one task.
  111. * Returns: whether a task has been executed.
  112. * Throws: whatever the current task constructor throws or the task() throws.
  113. */
  114. bool try_executing_one() { return ex.try_executing_one(); }
  115. };
  116. }
  117. using executors::executor_adaptor;
  118. }
  119. #include <boost/config/abi_suffix.hpp>
  120. #endif
  121. #endif