Chris@102
|
1 // Copyright (C) 2013 Vicente J. Botet Escriba
|
Chris@102
|
2 //
|
Chris@102
|
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@102
|
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@102
|
5 //
|
Chris@102
|
6 // 2013/11 Vicente J. Botet Escriba
|
Chris@102
|
7 // first implementation of a simple serial scheduler.
|
Chris@102
|
8
|
Chris@102
|
9 #ifndef BOOST_THREAD_SERIAL_EXECUTOR_HPP
|
Chris@102
|
10 #define BOOST_THREAD_SERIAL_EXECUTOR_HPP
|
Chris@102
|
11
|
Chris@102
|
12 #include <boost/thread/detail/config.hpp>
|
Chris@102
|
13 #include <boost/thread/detail/delete.hpp>
|
Chris@102
|
14 #include <boost/thread/detail/move.hpp>
|
Chris@102
|
15 #include <boost/thread/concurrent_queues/sync_queue.hpp>
|
Chris@102
|
16 #include <boost/thread/executors/work.hpp>
|
Chris@102
|
17 #include <boost/thread/executors/generic_executor_ref.hpp>
|
Chris@102
|
18 #include <boost/thread/future.hpp>
|
Chris@102
|
19 #include <boost/thread/scoped_thread.hpp>
|
Chris@102
|
20
|
Chris@102
|
21 #include <boost/config/abi_prefix.hpp>
|
Chris@102
|
22
|
Chris@102
|
23 namespace boost
|
Chris@102
|
24 {
|
Chris@102
|
25 namespace executors
|
Chris@102
|
26 {
|
Chris@102
|
27 class serial_executor
|
Chris@102
|
28 {
|
Chris@102
|
29 public:
|
Chris@102
|
30 /// type-erasure to store the works to do
|
Chris@102
|
31 typedef executors::work work;
|
Chris@102
|
32 private:
|
Chris@102
|
33 typedef scoped_thread<> thread_t;
|
Chris@102
|
34
|
Chris@102
|
35 /// the thread safe work queue
|
Chris@102
|
36 concurrent::sync_queue<work > work_queue;
|
Chris@102
|
37 generic_executor_ref ex;
|
Chris@102
|
38 thread_t thr;
|
Chris@102
|
39
|
Chris@102
|
40 struct try_executing_one_task {
|
Chris@102
|
41 work& task;
|
Chris@102
|
42 boost::promise<void> &p;
|
Chris@102
|
43 try_executing_one_task(work& task, boost::promise<void> &p)
|
Chris@102
|
44 : task(task), p(p) {}
|
Chris@102
|
45 void operator()() {
|
Chris@102
|
46 try {
|
Chris@102
|
47 task();
|
Chris@102
|
48 p.set_value();
|
Chris@102
|
49 } catch (...)
|
Chris@102
|
50 {
|
Chris@102
|
51 p.set_exception(current_exception());
|
Chris@102
|
52 }
|
Chris@102
|
53 }
|
Chris@102
|
54 };
|
Chris@102
|
55 public:
|
Chris@102
|
56 /**
|
Chris@102
|
57 * \par Returns
|
Chris@102
|
58 * The underlying executor wrapped on a generic executor reference.
|
Chris@102
|
59 */
|
Chris@102
|
60 generic_executor_ref& underlying_executor() BOOST_NOEXCEPT { return ex; }
|
Chris@102
|
61
|
Chris@102
|
62 /**
|
Chris@102
|
63 * Effects: try to execute one task.
|
Chris@102
|
64 * Returns: whether a task has been executed.
|
Chris@102
|
65 * Throws: whatever the current task constructor throws or the task() throws.
|
Chris@102
|
66 */
|
Chris@102
|
67 bool try_executing_one()
|
Chris@102
|
68 {
|
Chris@102
|
69 work task;
|
Chris@102
|
70 try
|
Chris@102
|
71 {
|
Chris@102
|
72 if (work_queue.try_pull(task) == queue_op_status::success)
|
Chris@102
|
73 {
|
Chris@102
|
74 boost::promise<void> p;
|
Chris@102
|
75 try_executing_one_task tmp(task,p);
|
Chris@102
|
76 ex.submit(tmp);
|
Chris@102
|
77 p.get_future().wait();
|
Chris@102
|
78 return true;
|
Chris@102
|
79 }
|
Chris@102
|
80 return false;
|
Chris@102
|
81 }
|
Chris@102
|
82 catch (...)
|
Chris@102
|
83 {
|
Chris@102
|
84 std::terminate();
|
Chris@102
|
85 return false;
|
Chris@102
|
86 }
|
Chris@102
|
87 }
|
Chris@102
|
88 private:
|
Chris@102
|
89 /**
|
Chris@102
|
90 * Effects: schedule one task or yields
|
Chris@102
|
91 * Throws: whatever the current task constructor throws or the task() throws.
|
Chris@102
|
92 */
|
Chris@102
|
93 void schedule_one_or_yield()
|
Chris@102
|
94 {
|
Chris@102
|
95 if ( ! try_executing_one())
|
Chris@102
|
96 {
|
Chris@102
|
97 this_thread::yield();
|
Chris@102
|
98 }
|
Chris@102
|
99 }
|
Chris@102
|
100
|
Chris@102
|
101 /**
|
Chris@102
|
102 * The main loop of the worker thread
|
Chris@102
|
103 */
|
Chris@102
|
104 void worker_thread()
|
Chris@102
|
105 {
|
Chris@102
|
106 while (!closed())
|
Chris@102
|
107 {
|
Chris@102
|
108 schedule_one_or_yield();
|
Chris@102
|
109 }
|
Chris@102
|
110 while (try_executing_one())
|
Chris@102
|
111 {
|
Chris@102
|
112 }
|
Chris@102
|
113 }
|
Chris@102
|
114
|
Chris@102
|
115 public:
|
Chris@102
|
116 /// serial_executor is not copyable.
|
Chris@102
|
117 BOOST_THREAD_NO_COPYABLE(serial_executor)
|
Chris@102
|
118
|
Chris@102
|
119 /**
|
Chris@102
|
120 * \b Effects: creates a thread pool that runs closures using one of its closure-executing methods.
|
Chris@102
|
121 *
|
Chris@102
|
122 * \b Throws: Whatever exception is thrown while initializing the needed resources.
|
Chris@102
|
123 */
|
Chris@102
|
124 template <class Executor>
|
Chris@102
|
125 serial_executor(Executor& ex)
|
Chris@102
|
126 : ex(ex), thr(&serial_executor::worker_thread, this)
|
Chris@102
|
127 {
|
Chris@102
|
128 }
|
Chris@102
|
129 /**
|
Chris@102
|
130 * \b Effects: Destroys the thread pool.
|
Chris@102
|
131 *
|
Chris@102
|
132 * \b Synchronization: The completion of all the closures happen before the completion of the \c serial_executor destructor.
|
Chris@102
|
133 */
|
Chris@102
|
134 ~serial_executor()
|
Chris@102
|
135 {
|
Chris@102
|
136 // signal to the worker thread that there will be no more submissions.
|
Chris@102
|
137 close();
|
Chris@102
|
138 }
|
Chris@102
|
139
|
Chris@102
|
140 /**
|
Chris@102
|
141 * \b Effects: close the \c serial_executor for submissions.
|
Chris@102
|
142 * The loop will work until there is no more closures to run.
|
Chris@102
|
143 */
|
Chris@102
|
144 void close()
|
Chris@102
|
145 {
|
Chris@102
|
146 work_queue.close();
|
Chris@102
|
147 }
|
Chris@102
|
148
|
Chris@102
|
149 /**
|
Chris@102
|
150 * \b Returns: whether the pool is closed for submissions.
|
Chris@102
|
151 */
|
Chris@102
|
152 bool closed()
|
Chris@102
|
153 {
|
Chris@102
|
154 return work_queue.closed();
|
Chris@102
|
155 }
|
Chris@102
|
156
|
Chris@102
|
157 /**
|
Chris@102
|
158 * \b Requires: \c Closure is a model of \c Callable(void()) and a model of \c CopyConstructible/MoveConstructible.
|
Chris@102
|
159 *
|
Chris@102
|
160 * \b Effects: The specified \c closure will be scheduled for execution at some point in the future.
|
Chris@102
|
161 * If invoked closure throws an exception the \c serial_executor will call \c std::terminate, as is the case with threads.
|
Chris@102
|
162 *
|
Chris@102
|
163 * \b Synchronization: completion of \c closure on a particular thread happens before destruction of thread's thread local variables.
|
Chris@102
|
164 *
|
Chris@102
|
165 * \b Throws: \c sync_queue_is_closed if the thread pool is closed.
|
Chris@102
|
166 * Whatever exception that can be throw while storing the closure.
|
Chris@102
|
167 */
|
Chris@102
|
168
|
Chris@102
|
169 #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
Chris@102
|
170 template <typename Closure>
|
Chris@102
|
171 void submit(Closure & closure)
|
Chris@102
|
172 {
|
Chris@102
|
173 work_queue.push(work(closure));
|
Chris@102
|
174 }
|
Chris@102
|
175 #endif
|
Chris@102
|
176 void submit(void (*closure)())
|
Chris@102
|
177 {
|
Chris@102
|
178 work_queue.push(work(closure));
|
Chris@102
|
179 }
|
Chris@102
|
180
|
Chris@102
|
181 template <typename Closure>
|
Chris@102
|
182 void submit(BOOST_THREAD_RV_REF(Closure) closure)
|
Chris@102
|
183 {
|
Chris@102
|
184 work_queue.push(work(boost::forward<Closure>(closure)));
|
Chris@102
|
185 }
|
Chris@102
|
186
|
Chris@102
|
187 /**
|
Chris@102
|
188 * \b Requires: This must be called from an scheduled task.
|
Chris@102
|
189 *
|
Chris@102
|
190 * \b Effects: reschedule functions until pred()
|
Chris@102
|
191 */
|
Chris@102
|
192 template <typename Pred>
|
Chris@102
|
193 bool reschedule_until(Pred const& pred)
|
Chris@102
|
194 {
|
Chris@102
|
195 do {
|
Chris@102
|
196 if ( ! try_executing_one())
|
Chris@102
|
197 {
|
Chris@102
|
198 return false;
|
Chris@102
|
199 }
|
Chris@102
|
200 } while (! pred());
|
Chris@102
|
201 return true;
|
Chris@102
|
202 }
|
Chris@102
|
203
|
Chris@102
|
204 };
|
Chris@102
|
205 }
|
Chris@102
|
206 using executors::serial_executor;
|
Chris@102
|
207 }
|
Chris@102
|
208
|
Chris@102
|
209 #include <boost/config/abi_suffix.hpp>
|
Chris@102
|
210
|
Chris@102
|
211 #endif
|