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