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