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