1 // Copyright (C) 2013,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)
6 // 2013/11 Vicente J. Botet Escriba
7 // first implementation of a simple user scheduler.
8 // 2013/11 Vicente J. Botet Escriba
9 // rename loop_executor.
11 #ifndef BOOST_THREAD_EXECUTORS_LOOP_EXECUTOR_HPP
12 #define BOOST_THREAD_EXECUTORS_LOOP_EXECUTOR_HPP
14 #include <boost/thread/detail/config.hpp>
15 #include <boost/thread/detail/delete.hpp>
16 #include <boost/thread/detail/move.hpp>
17 #include <boost/thread/concurrent_queues/sync_queue.hpp>
18 #include <boost/thread/executors/work.hpp>
19 #include <boost/assert.hpp>
21 #include <boost/config/abi_prefix.hpp>
31 /// type-erasure to store the works to do
32 typedef executors::work work;
34 /// the thread safe work queue
35 concurrent::sync_queue<work > work_queue;
39 * Effects: try to execute one task.
40 * Returns: whether a task has been executed.
41 * Throws: whatever the current task constructor throws or the task() throws.
43 bool try_executing_one()
45 return execute_one(/*wait:*/false);
50 * Effects: Execute one task.
51 * Remark: If wait is true, waits until a task is available or the executor
52 * is closed. If wait is false, returns false immediately if no
54 * Returns: whether a task has been executed (if wait is true, only returns false if closed).
55 * Throws: whatever the current task constructor throws or the task() throws.
57 bool execute_one(bool wait)
62 queue_op_status status = wait ?
63 work_queue.wait_pull(task) :
64 work_queue.try_pull(task);
65 if (status == queue_op_status::success)
70 BOOST_ASSERT(!wait || status == queue_op_status::closed);
81 /// loop_executor is not copyable.
82 BOOST_THREAD_NO_COPYABLE(loop_executor)
85 * \b Effects: creates a thread pool that runs closures using one of its closure-executing methods.
87 * \b Throws: Whatever exception is thrown while initializing the needed resources.
93 * \b Effects: Destroys the thread pool.
95 * \b Synchronization: The completion of all the closures happen before the completion of the \c loop_executor destructor.
99 // signal to all the worker thread that there will be no more submissions.
104 * The main loop of the worker thread
108 while (execute_one(/*wait:*/true))
111 BOOST_ASSERT(closed());
112 while (try_executing_one())
118 * \b Effects: close the \c loop_executor for submissions.
119 * The loop will work until there is no more closures to run.
127 * \b Returns: whether the pool is closed for submissions.
131 return work_queue.closed();
135 * \b Requires: \c Closure is a model of \c Callable(void()) and a model of \c CopyConstructible/MoveConstructible.
137 * \b Effects: The specified \c closure will be scheduled for execution at some point in the future.
138 * If invoked closure throws an exception the \c loop_executor will call \c std::terminate, as is the case with threads.
140 * \b Synchronization: completion of \c closure on a particular thread happens before destruction of thread's thread local variables.
142 * \b Throws: \c sync_queue_is_closed if the thread pool is closed.
143 * Whatever exception that can be throw while storing the closure.
145 void submit(BOOST_THREAD_RV_REF(work) closure) {
146 work_queue.push(boost::move(closure));
149 #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
150 template <typename Closure>
151 void submit(Closure & closure)
153 submit(work(closure));
157 void submit(void (*closure)())
159 submit(work(closure));
162 template <typename Closure>
163 void submit(BOOST_THREAD_FWD_REF(Closure) closure)
165 //work_queue.push(work(boost::forward<Closure>(closure)));
166 work w((boost::forward<Closure>(closure)));
167 submit(boost::move(w));
171 * \b Requires: This must be called from an scheduled task.
173 * \b Effects: reschedule functions until pred()
175 template <typename Pred>
176 bool reschedule_until(Pred const& pred)
179 if ( ! try_executing_one())
188 * run queued closures
190 void run_queued_closures()
192 sync_queue<work>::underlying_queue_type q = work_queue.underlying_queue();
195 work& task = q.front();
203 using executors::loop_executor;
207 #include <boost/config/abi_suffix.hpp>