1 // Copyright (C) 2014 Vicente J. Botet Escriba
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)
7 #ifndef BOOST_THREAD_EXECUTORS_GENERIC_EXECUTOR_REF_HPP
8 #define BOOST_THREAD_EXECUTORS_GENERIC_EXECUTOR_REF_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
13 #include <boost/thread/detail/delete.hpp>
14 #include <boost/thread/detail/move.hpp>
15 #include <boost/thread/executors/executor.hpp>
17 #include <boost/shared_ptr.hpp>
19 #include <boost/config/abi_prefix.hpp>
26 template <class Executor>
27 class executor_ref : public executor
31 /// type-erasure to store the works to do
32 typedef executors::work work;
34 /// executor is not copyable.
35 BOOST_THREAD_NO_COPYABLE(executor_ref)
36 executor_ref(Executor& ex_) : ex(ex_) {}
40 * Destroys the executor.
42 * \par Synchronization
43 * The completion of all the closures happen before the completion of the executor destructor.
49 * Close the \c executor for submissions.
50 * The worker threads will work until there is no more closures to run.
52 void close() { ex.close(); }
56 * Whether the pool is closed for submissions.
58 bool closed() { return ex.closed(); }
62 * The specified closure will be scheduled for execution at some point in the future.
63 * If invoked closure throws an exception the executor will call std::terminate, as is the case with threads.
65 * \par Synchronization
66 * Ccompletion of closure on a particular thread happens before destruction of thread's thread local variables.
69 * \c sync_queue_is_closed if the thread pool is closed.
70 * Whatever exception that can be throw while storing the closure.
72 void submit(BOOST_THREAD_RV_REF(work) closure) {
73 ex.submit(boost::move(closure));
75 // void submit(work& closure) {
76 // ex.submit(closure);
82 * Try to execute one task.
85 * Whether a task has been executed.
88 * Whatever the current task constructor throws or the task() throws.
90 bool try_executing_one() { return ex.try_executing_one(); }
94 class generic_executor_ref
96 shared_ptr<executor> ex;
98 /// type-erasure to store the works to do
99 typedef executors::work work;
101 template<typename Executor>
102 generic_executor_ref(Executor& ex_)
103 //: ex(make_shared<executor_ref<Executor> >(ex_)) // todo check why this doesn't works with C++03
104 : ex( new executor_ref<Executor>(ex_) )
108 //generic_executor_ref(generic_executor_ref const& other) noexcept {}
109 //generic_executor_ref& operator=(generic_executor_ref const& other) noexcept {}
114 * Close the \c executor for submissions.
115 * The worker threads will work until there is no more closures to run.
117 void close() { ex->close(); }
121 * Whether the pool is closed for submissions.
123 bool closed() { return ex->closed(); }
127 * \c Closure is a model of Callable(void()) and a model of CopyConstructible/MoveConstructible.
130 * The specified closure will be scheduled for execution at some point in the future.
131 * If invoked closure throws an exception the thread pool will call std::terminate, as is the case with threads.
133 * \par Synchronization
134 * Completion of closure on a particular thread happens before destruction of thread's thread local variables.
137 * \c sync_queue_is_closed if the thread pool is closed.
138 * Whatever exception that can be throw while storing the closure.
141 void submit(BOOST_THREAD_RV_REF(work) closure)
143 ex->submit(boost::move(closure));
146 #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
147 template <typename Closure>
148 void submit(Closure & closure)
150 //work w ((closure));
151 //submit(boost::move(w));
152 submit(work(closure));
155 void submit(void (*closure)())
158 submit(boost::move(w));
159 //submit(work(closure));
162 template <typename Closure>
163 void submit(BOOST_THREAD_FWD_REF(Closure) closure)
165 work w((boost::forward<Closure>(closure)));
166 submit(boost::move(w));
169 // size_t num_pending_closures() const
171 // return ex->num_pending_closures();
176 * Try to execute one task.
179 * Whether a task has been executed.
182 * Whatever the current task constructor throws or the task() throws.
184 bool try_executing_one() { return ex->try_executing_one(); }
188 * This must be called from an scheduled task.
191 * reschedule functions until pred()
193 template <typename Pred>
194 bool reschedule_until(Pred const& pred)
197 //schedule_one_or_yield();
198 if ( ! try_executing_one())
208 using executors::executor_ref;
209 using executors::generic_executor_ref;
212 #include <boost/config/abi_suffix.hpp>