1 // Copyright (C) 2013 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)
6 // 2013/11 Vicente J. Botet Escriba
7 // first implementation of a simple serial scheduler.
9 #ifndef BOOST_THREAD_SERIAL_EXECUTOR_HPP
10 #define BOOST_THREAD_SERIAL_EXECUTOR_HPP
12 #include <boost/thread/detail/config.hpp>
13 #include <boost/thread/detail/delete.hpp>
14 #include <boost/thread/detail/move.hpp>
15 #include <boost/thread/concurrent_queues/sync_queue.hpp>
16 #include <boost/thread/executors/work.hpp>
17 #include <boost/thread/executors/generic_executor_ref.hpp>
18 #include <boost/thread/future.hpp>
19 #include <boost/thread/scoped_thread.hpp>
21 #include <boost/config/abi_prefix.hpp>
30 /// type-erasure to store the works to do
31 typedef executors::work work;
33 typedef scoped_thread<> thread_t;
35 /// the thread safe work queue
36 concurrent::sync_queue<work > work_queue;
37 generic_executor_ref ex;
40 struct try_executing_one_task {
42 boost::promise<void> &p;
43 try_executing_one_task(work& task, boost::promise<void> &p)
51 p.set_exception(current_exception());
58 * The underlying executor wrapped on a generic executor reference.
60 generic_executor_ref& underlying_executor() BOOST_NOEXCEPT { return ex; }
63 * Effects: try to execute one task.
64 * Returns: whether a task has been executed.
65 * Throws: whatever the current task constructor throws or the task() throws.
67 bool try_executing_one()
72 if (work_queue.try_pull(task) == queue_op_status::success)
74 boost::promise<void> p;
75 try_executing_one_task tmp(task,p);
77 p.get_future().wait();
90 * Effects: schedule one task or yields
91 * Throws: whatever the current task constructor throws or the task() throws.
93 void schedule_one_or_yield()
95 if ( ! try_executing_one())
102 * The main loop of the worker thread
108 schedule_one_or_yield();
110 while (try_executing_one())
116 /// serial_executor is not copyable.
117 BOOST_THREAD_NO_COPYABLE(serial_executor)
120 * \b Effects: creates a thread pool that runs closures using one of its closure-executing methods.
122 * \b Throws: Whatever exception is thrown while initializing the needed resources.
124 template <class Executor>
125 serial_executor(Executor& ex)
126 : ex(ex), thr(&serial_executor::worker_thread, this)
130 * \b Effects: Destroys the thread pool.
132 * \b Synchronization: The completion of all the closures happen before the completion of the \c serial_executor destructor.
136 // signal to the worker thread that there will be no more submissions.
141 * \b Effects: close the \c serial_executor for submissions.
142 * The loop will work until there is no more closures to run.
150 * \b Returns: whether the pool is closed for submissions.
154 return work_queue.closed();
158 * \b Requires: \c Closure is a model of \c Callable(void()) and a model of \c CopyConstructible/MoveConstructible.
160 * \b Effects: The specified \c closure will be scheduled for execution at some point in the future.
161 * If invoked closure throws an exception the \c serial_executor will call \c std::terminate, as is the case with threads.
163 * \b Synchronization: completion of \c closure on a particular thread happens before destruction of thread's thread local variables.
165 * \b Throws: \c sync_queue_is_closed if the thread pool is closed.
166 * Whatever exception that can be throw while storing the closure.
168 void submit(BOOST_THREAD_RV_REF(work) closure)
170 work_queue.push(boost::move(closure));
173 #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
174 template <typename Closure>
175 void submit(Closure & closure)
177 submit(work(closure));
180 void submit(void (*closure)())
182 submit(work(closure));
185 template <typename Closure>
186 void submit(BOOST_THREAD_FWD_REF(Closure) closure)
188 work w((boost::forward<Closure>(closure)));
189 submit(boost::move(w));
193 * \b Requires: This must be called from an scheduled task.
195 * \b Effects: reschedule functions until pred()
197 template <typename Pred>
198 bool reschedule_until(Pred const& pred)
201 if ( ! try_executing_one())
211 using executors::serial_executor;
214 #include <boost/config/abi_suffix.hpp>