| 1 | // Copyright (C) 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 | |
| 7 | #ifndef BOOST_THREAD_EXECUTORS_GENERIC_EXECUTOR_REF_HPP |
| 8 | #define BOOST_THREAD_EXECUTORS_GENERIC_EXECUTOR_REF_HPP |
| 9 | |
| 10 | #include <boost/thread/detail/config.hpp> |
| 11 | |
| 12 | #include <boost/thread/detail/delete.hpp> |
| 13 | #include <boost/thread/detail/move.hpp> |
| 14 | #include <boost/thread/executors/executor.hpp> |
| 15 | |
| 16 | #include <boost/shared_ptr.hpp> |
| 17 | |
| 18 | #include <boost/config/abi_prefix.hpp> |
| 19 | |
| 20 | namespace boost |
| 21 | { |
| 22 | namespace executors |
| 23 | { |
| 24 | |
| 25 | template <class Executor> |
| 26 | class executor_ref : public executor |
| 27 | { |
| 28 | Executor& ex; |
| 29 | public: |
| 30 | /// type-erasure to store the works to do |
| 31 | typedef executors::work work; |
| 32 | |
| 33 | /// executor is not copyable. |
| 34 | BOOST_THREAD_NO_COPYABLE(executor_ref) |
| 35 | executor_ref(Executor& ex_) : ex(ex_) {} |
| 36 | |
| 37 | /** |
| 38 | * \par Effects |
| 39 | * Destroys the executor. |
| 40 | * |
| 41 | * \par Synchronization |
| 42 | * The completion of all the closures happen before the completion of the executor destructor. |
| 43 | */ |
| 44 | ~executor_ref() {} |
| 45 | |
| 46 | /** |
| 47 | * \par Effects |
| 48 | * Close the \c executor for submissions. |
| 49 | * The worker threads will work until there is no more closures to run. |
| 50 | */ |
| 51 | void close() { ex.close(); } |
| 52 | |
| 53 | /** |
| 54 | * \par Returns |
| 55 | * Whether the pool is closed for submissions. |
| 56 | */ |
| 57 | bool closed() { return ex.closed(); } |
| 58 | |
| 59 | /** |
| 60 | * \par Effects |
| 61 | * The specified closure will be scheduled for execution at some point in the future. |
| 62 | * If invoked closure throws an exception the executor will call std::terminate, as is the case with threads. |
| 63 | * |
| 64 | * \par Synchronization |
| 65 | * Ccompletion of closure on a particular thread happens before destruction of thread's thread local variables. |
| 66 | * |
| 67 | * \par Throws |
| 68 | * \c sync_queue_is_closed if the thread pool is closed. |
| 69 | * Whatever exception that can be throw while storing the closure. |
| 70 | */ |
| 71 | void submit(BOOST_THREAD_RV_REF(work) closure) { |
| 72 | ex.submit(boost::move(closure)); |
| 73 | } |
| 74 | // void submit(work& closure) { |
| 75 | // ex.submit(closure); |
| 76 | // } |
| 77 | |
| 78 | |
| 79 | /** |
| 80 | * \par Effects |
| 81 | * Try to execute one task. |
| 82 | * |
| 83 | * \par Returns |
| 84 | * Whether a task has been executed. |
| 85 | * |
| 86 | * \par Throws |
| 87 | * Whatever the current task constructor throws or the task() throws. |
| 88 | */ |
| 89 | bool try_executing_one() { return ex.try_executing_one(); } |
| 90 | |
| 91 | }; |
| 92 | |
| 93 | class generic_executor_ref |
| 94 | { |
| 95 | shared_ptr<executor> ex; |
| 96 | public: |
| 97 | /// type-erasure to store the works to do |
| 98 | typedef executors::work work; |
| 99 | |
| 100 | template<typename Executor> |
| 101 | generic_executor_ref(Executor& ex_) |
| 102 | //: ex(make_shared<executor_ref<Executor> >(ex_)) // todo check why this doesn't works with C++03 |
| 103 | : ex( new executor_ref<Executor>(ex_) ) |
| 104 | { |
| 105 | } |
| 106 | |
| 107 | //generic_executor_ref(generic_executor_ref const& other) noexcept {} |
| 108 | //generic_executor_ref& operator=(generic_executor_ref const& other) noexcept {} |
| 109 | |
| 110 | |
| 111 | /** |
| 112 | * \par Effects |
| 113 | * Close the \c executor for submissions. |
| 114 | * The worker threads will work until there is no more closures to run. |
| 115 | */ |
| 116 | void close() { ex->close(); } |
| 117 | |
| 118 | /** |
| 119 | * \par Returns |
| 120 | * Whether the pool is closed for submissions. |
| 121 | */ |
| 122 | bool closed() { return ex->closed(); } |
| 123 | |
| 124 | /** |
| 125 | * \par Requires |
| 126 | * \c Closure is a model of Callable(void()) and a model of CopyConstructible/MoveConstructible. |
| 127 | * |
| 128 | * \par Effects |
| 129 | * The specified closure will be scheduled for execution at some point in the future. |
| 130 | * If invoked closure throws an exception the thread pool will call std::terminate, as is the case with threads. |
| 131 | * |
| 132 | * \par Synchronization |
| 133 | * Completion of closure on a particular thread happens before destruction of thread's thread local variables. |
| 134 | * |
| 135 | * \par Throws |
| 136 | * \c sync_queue_is_closed if the thread pool is closed. |
| 137 | * Whatever exception that can be throw while storing the closure. |
| 138 | */ |
| 139 | |
| 140 | void submit(BOOST_THREAD_RV_REF(work) closure) |
| 141 | { |
| 142 | ex->submit(boost::move(closure)); |
| 143 | } |
| 144 | |
| 145 | #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) |
| 146 | template <typename Closure> |
| 147 | void submit(Closure & closure) |
| 148 | { |
| 149 | //work w ((closure)); |
| 150 | //submit(boost::move(w)); |
| 151 | submit(work(closure)); |
| 152 | } |
| 153 | #endif |
| 154 | void submit(void (*closure)()) |
| 155 | { |
| 156 | work w ((closure)); |
| 157 | submit(boost::move(w)); |
| 158 | //submit(work(closure)); |
| 159 | } |
| 160 | |
| 161 | template <typename Closure> |
| 162 | void submit(BOOST_THREAD_FWD_REF(Closure) closure) |
| 163 | { |
| 164 | work w((boost::forward<Closure>(closure))); |
| 165 | submit(boost::move(w)); |
| 166 | } |
| 167 | |
| 168 | // size_t num_pending_closures() const |
| 169 | // { |
| 170 | // return ex->num_pending_closures(); |
| 171 | // } |
| 172 | |
| 173 | /** |
| 174 | * \par Effects |
| 175 | * Try to execute one task. |
| 176 | * |
| 177 | * \par Returns |
| 178 | * Whether a task has been executed. |
| 179 | * |
| 180 | * \par Throws |
| 181 | * Whatever the current task constructor throws or the task() throws. |
| 182 | */ |
| 183 | bool try_executing_one() { return ex->try_executing_one(); } |
| 184 | |
| 185 | /** |
| 186 | * \par Requires |
| 187 | * This must be called from an scheduled task. |
| 188 | * |
| 189 | * \par Effects |
| 190 | * reschedule functions until pred() |
| 191 | */ |
| 192 | template <typename Pred> |
| 193 | bool reschedule_until(Pred const& pred) |
| 194 | { |
| 195 | do { |
| 196 | //schedule_one_or_yield(); |
| 197 | if ( ! try_executing_one()) |
| 198 | { |
| 199 | return false; |
| 200 | } |
| 201 | } while (! pred()); |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | }; |
| 206 | } |
| 207 | using executors::executor_ref; |
| 208 | using executors::generic_executor_ref; |
| 209 | } |
| 210 | |
| 211 | #include <boost/config/abi_suffix.hpp> |
| 212 | |
| 213 | #endif |
| 214 | |