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>
12 #include <boost/thread/detail/delete.hpp>
13 #include <boost/thread/detail/move.hpp>
14 #include <boost/thread/executors/executor.hpp>
16 #include <boost/shared_ptr.hpp>
18 #include <boost/config/abi_prefix.hpp>
25 template <class Executor>
26 class executor_ref : public executor
30 /// type-erasure to store the works to do
31 typedef executors::work work;
33 /// executor is not copyable.
34 BOOST_THREAD_NO_COPYABLE(executor_ref)
35 executor_ref(Executor& ex_) : ex(ex_) {}
39 * Destroys the executor.
41 * \par Synchronization
42 * The completion of all the closures happen before the completion of the executor destructor.
48 * Close the \c executor for submissions.
49 * The worker threads will work until there is no more closures to run.
51 void close() { ex.close(); }
55 * Whether the pool is closed for submissions.
57 bool closed() { return ex.closed(); }
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.
64 * \par Synchronization
65 * Ccompletion of closure on a particular thread happens before destruction of thread's thread local variables.
68 * \c sync_queue_is_closed if the thread pool is closed.
69 * Whatever exception that can be throw while storing the closure.
71 void submit(BOOST_THREAD_RV_REF(work) closure) {
72 ex.submit(boost::move(closure));
74 // void submit(work& closure) {
75 // ex.submit(closure);
81 * Try to execute one task.
84 * Whether a task has been executed.
87 * Whatever the current task constructor throws or the task() throws.
89 bool try_executing_one() { return ex.try_executing_one(); }
93 class generic_executor_ref
95 shared_ptr<executor> ex;
97 /// type-erasure to store the works to do
98 typedef executors::work work;
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_) )
107 //generic_executor_ref(generic_executor_ref const& other) noexcept {}
108 //generic_executor_ref& operator=(generic_executor_ref const& other) noexcept {}
113 * Close the \c executor for submissions.
114 * The worker threads will work until there is no more closures to run.
116 void close() { ex->close(); }
120 * Whether the pool is closed for submissions.
122 bool closed() { return ex->closed(); }
126 * \c Closure is a model of Callable(void()) and a model of CopyConstructible/MoveConstructible.
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.
132 * \par Synchronization
133 * Completion of closure on a particular thread happens before destruction of thread's thread local variables.
136 * \c sync_queue_is_closed if the thread pool is closed.
137 * Whatever exception that can be throw while storing the closure.
140 void submit(BOOST_THREAD_RV_REF(work) closure)
142 ex->submit(boost::move(closure));
145 #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
146 template <typename Closure>
147 void submit(Closure & closure)
149 //work w ((closure));
150 //submit(boost::move(w));
151 submit(work(closure));
154 void submit(void (*closure)())
157 submit(boost::move(w));
158 //submit(work(closure));
161 template <typename Closure>
162 void submit(BOOST_THREAD_FWD_REF(Closure) closure)
164 work w((boost::forward<Closure>(closure)));
165 submit(boost::move(w));
168 // size_t num_pending_closures() const
170 // return ex->num_pending_closures();
175 * Try to execute one task.
178 * Whether a task has been executed.
181 * Whatever the current task constructor throws or the task() throws.
183 bool try_executing_one() { return ex->try_executing_one(); }
187 * This must be called from an scheduled task.
190 * reschedule functions until pred()
192 template <typename Pred>
193 bool reschedule_until(Pred const& pred)
196 //schedule_one_or_yield();
197 if ( ! try_executing_one())
207 using executors::executor_ref;
208 using executors::generic_executor_ref;
211 #include <boost/config/abi_suffix.hpp>