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>
23 #if defined(BOOST_MSVC)
24 # pragma warning(push)
25 # pragma warning(disable: 4355) // 'this' : used in base member initializer list
35 /// type-erasure to store the works to do
36 typedef executors::work work;
38 typedef scoped_thread<> thread_t;
40 /// the thread safe work queue
41 concurrent::sync_queue<work > work_queue;
42 generic_executor_ref ex;
45 struct try_executing_one_task {
47 boost::promise<void> &p;
48 try_executing_one_task(work& task, boost::promise<void> &p)
56 p.set_exception(current_exception());
63 * The underlying executor wrapped on a generic executor reference.
65 generic_executor_ref& underlying_executor() BOOST_NOEXCEPT { return ex; }
68 * Effects: try to execute one task.
69 * Returns: whether a task has been executed.
70 * Throws: whatever the current task constructor throws or the task() throws.
72 bool try_executing_one()
77 if (work_queue.try_pull(task) == queue_op_status::success)
79 boost::promise<void> p;
80 try_executing_one_task tmp(task,p);
82 p.get_future().wait();
95 * Effects: schedule one task or yields
96 * Throws: whatever the current task constructor throws or the task() throws.
98 void schedule_one_or_yield()
100 if ( ! try_executing_one())
102 this_thread::yield();
107 * The main loop of the worker thread
113 schedule_one_or_yield();
115 while (try_executing_one())
121 /// serial_executor is not copyable.
122 BOOST_THREAD_NO_COPYABLE(serial_executor)
125 * \b Effects: creates a thread pool that runs closures using one of its closure-executing methods.
127 * \b Throws: Whatever exception is thrown while initializing the needed resources.
129 template <class Executor>
130 serial_executor(Executor& ex)
131 : ex(ex), thr(&serial_executor::worker_thread, this)
135 * \b Effects: Destroys the thread pool.
137 * \b Synchronization: The completion of all the closures happen before the completion of the \c serial_executor destructor.
141 // signal to the worker thread that there will be no more submissions.
146 * \b Effects: close the \c serial_executor for submissions.
147 * The loop will work until there is no more closures to run.
155 * \b Returns: whether the pool is closed for submissions.
159 return work_queue.closed();
163 * \b Requires: \c Closure is a model of \c Callable(void()) and a model of \c CopyConstructible/MoveConstructible.
165 * \b Effects: The specified \c closure will be scheduled for execution at some point in the future.
166 * If invoked closure throws an exception the \c serial_executor will call \c std::terminate, as is the case with threads.
168 * \b Synchronization: completion of \c closure on a particular thread happens before destruction of thread's thread local variables.
170 * \b Throws: \c sync_queue_is_closed if the thread pool is closed.
171 * Whatever exception that can be throw while storing the closure.
173 void submit(BOOST_THREAD_RV_REF(work) closure)
175 work_queue.push(boost::move(closure));
178 #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
179 template <typename Closure>
180 void submit(Closure & closure)
182 submit(work(closure));
185 void submit(void (*closure)())
187 submit(work(closure));
190 template <typename Closure>
191 void submit(BOOST_THREAD_FWD_REF(Closure) closure)
193 work w((boost::forward<Closure>(closure)));
194 submit(boost::move(w));
198 * \b Requires: This must be called from an scheduled task.
200 * \b Effects: reschedule functions until pred()
202 template <typename Pred>
203 bool reschedule_until(Pred const& pred)
206 if ( ! try_executing_one())
216 using executors::serial_executor;
219 #if defined(BOOST_MSVC)
220 # pragma warning(pop)
223 #include <boost/config/abi_suffix.hpp>