1 // Copyright (C) 2015 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_CONT_HPP
10 #define BOOST_THREAD_SERIAL_EXECUTOR_CONT_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>
27 class serial_executor_cont
30 /// type-erasure to store the works to do
31 typedef executors::work work;
34 generic_executor_ref ex_;
35 future<void> fut_; // protected by mtx_
36 bool closed_; // protected by mtx_
45 continuation(BOOST_THREAD_RV_REF(work) tsk)
46 : task(boost::move(tsk)) {}
47 void operator()(future<void> f)
57 bool closed(lock_guard<mutex>&) const
64 * The underlying executor wrapped on a generic executor reference.
66 generic_executor_ref& underlying_executor() BOOST_NOEXCEPT { return ex_; }
68 /// serial_executor_cont is not copyable.
69 BOOST_THREAD_NO_COPYABLE(serial_executor_cont)
72 * \b Effects: creates a serial executor that runs closures in fifo order using one the associated executor.
74 * \b Throws: Whatever exception is thrown while initializing the needed resources.
77 * * The lifetime of the associated executor must outlive the serial executor.
78 * * The current implementation doesn't support submission from synchronous continuation, that is,
79 * - the executor must execute the continuation asynchronously or
80 * - the continuation can not submit to this serial executor.
82 template <class Executor>
83 serial_executor_cont(Executor& ex)
84 : ex_(ex), fut_(make_ready_future()), closed_(false)
88 * \b Effects: Destroys the thread pool.
90 * \b Synchronization: The completion of all the closures happen before the completion of the \c serial_executor_cont destructor.
92 ~serial_executor_cont()
94 // signal to the worker thread that there will be no more submissions.
99 * \b Effects: close the \c serial_executor_cont for submissions.
100 * The loop will work until there is no more closures to run.
104 lock_guard<mutex> lk(mtx_);
109 * \b Returns: whether the pool is closed for submissions.
113 lock_guard<mutex> lk(mtx_);
119 * Returns: always false.
121 * Remark: A serial executor can not execute one of its pending tasks as the tasks depends on the other tasks.
123 bool try_executing_one()
129 * \b Requires: \c Closure is a model of \c Callable(void()) and a model of \c CopyConstructible/MoveConstructible.
131 * \b Effects: The specified \c closure will be scheduled for execution after the last submitted closure finish.
132 * If the invoked closure throws an exception the \c serial_executor_cont will call \c std::terminate, as is the case with threads.
134 * \b Throws: \c sync_queue_is_closed if the executor is closed.
135 * Whatever exception that can be throw while storing the closure.
139 #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
140 template <typename Closure>
141 void submit(Closure & closure)
143 lock_guard<mutex> lk(mtx_);
144 if (closed(lk)) BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
145 fut_ = fut_.then(ex_, continuation(work(closure)));
148 void submit(void (*closure)())
150 lock_guard<mutex> lk(mtx_);
151 if (closed(lk)) BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
152 fut_ = fut_.then(ex_, continuation(work(closure)));
155 template <typename Closure>
156 void submit(BOOST_THREAD_FWD_REF(Closure) closure)
158 lock_guard<mutex> lk(mtx_);
159 if (closed(lk)) BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
160 fut_ = fut_.then(ex_, continuation(work(boost::forward<Closure>(closure))));
165 using executors::serial_executor_cont;
168 #include <boost/config/abi_suffix.hpp>