]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright (C) 2015 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_SERIAL_EXECUTOR_CONT_HPP | |
10 | #define BOOST_THREAD_SERIAL_EXECUTOR_CONT_HPP | |
11 | ||
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> | |
20 | ||
21 | #include <boost/config/abi_prefix.hpp> | |
22 | ||
23 | namespace boost | |
24 | { | |
25 | namespace executors | |
26 | { | |
27 | class serial_executor_cont | |
28 | { | |
29 | public: | |
30 | /// type-erasure to store the works to do | |
31 | typedef executors::work work; | |
32 | private: | |
33 | ||
34 | generic_executor_ref ex_; | |
35 | future<void> fut_; // protected by mtx_ | |
36 | bool closed_; // protected by mtx_ | |
37 | mutex mtx_; | |
38 | ||
39 | struct continuation { | |
40 | work task; | |
41 | template <class X> | |
42 | struct result { | |
43 | typedef void type; | |
44 | }; | |
45 | continuation(BOOST_THREAD_RV_REF(work) tsk) | |
46 | : task(boost::move(tsk)) {} | |
47 | void operator()(future<void> f) | |
48 | { | |
49 | try { | |
50 | task(); | |
51 | } catch (...) { | |
52 | std::terminate(); | |
53 | } | |
54 | } | |
55 | }; | |
56 | ||
57 | bool closed(lock_guard<mutex>&) const | |
58 | { | |
59 | return closed_; | |
60 | } | |
61 | public: | |
62 | /** | |
63 | * \par Returns | |
64 | * The underlying executor wrapped on a generic executor reference. | |
65 | */ | |
66 | generic_executor_ref& underlying_executor() BOOST_NOEXCEPT { return ex_; } | |
67 | ||
68 | /// serial_executor_cont is not copyable. | |
69 | BOOST_THREAD_NO_COPYABLE(serial_executor_cont) | |
70 | ||
71 | /** | |
72 | * \b Effects: creates a serial executor that runs closures in fifo order using one the associated executor. | |
73 | * | |
74 | * \b Throws: Whatever exception is thrown while initializing the needed resources. | |
75 | * | |
76 | * \b Notes: | |
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. | |
81 | */ | |
82 | template <class Executor> | |
83 | serial_executor_cont(Executor& ex) | |
84 | : ex_(ex), fut_(make_ready_future()), closed_(false) | |
85 | { | |
86 | } | |
87 | /** | |
88 | * \b Effects: Destroys the thread pool. | |
89 | * | |
90 | * \b Synchronization: The completion of all the closures happen before the completion of the \c serial_executor_cont destructor. | |
91 | */ | |
92 | ~serial_executor_cont() | |
93 | { | |
94 | // signal to the worker thread that there will be no more submissions. | |
95 | close(); | |
96 | } | |
97 | ||
98 | /** | |
99 | * \b Effects: close the \c serial_executor_cont for submissions. | |
100 | * The loop will work until there is no more closures to run. | |
101 | */ | |
102 | void close() | |
103 | { | |
104 | lock_guard<mutex> lk(mtx_); | |
105 | closed_ = true;; | |
106 | } | |
107 | ||
108 | /** | |
109 | * \b Returns: whether the pool is closed for submissions. | |
110 | */ | |
111 | bool closed() | |
112 | { | |
113 | lock_guard<mutex> lk(mtx_); | |
114 | return closed(lk); | |
115 | } | |
116 | ||
117 | /** | |
118 | * Effects: none. | |
119 | * Returns: always false. | |
120 | * Throws: No. | |
121 | * Remark: A serial executor can not execute one of its pending tasks as the tasks depends on the other tasks. | |
122 | */ | |
123 | bool try_executing_one() | |
124 | { | |
125 | return false; | |
126 | } | |
127 | ||
128 | /** | |
129 | * \b Requires: \c Closure is a model of \c Callable(void()) and a model of \c CopyConstructible/MoveConstructible. | |
130 | * | |
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. | |
133 | * | |
134 | * \b Throws: \c sync_queue_is_closed if the executor is closed. | |
135 | * Whatever exception that can be throw while storing the closure. | |
136 | * | |
137 | */ | |
138 | ||
139 | #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) | |
140 | template <typename Closure> | |
141 | void submit(Closure & closure) | |
142 | { | |
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))); | |
146 | } | |
147 | #endif | |
148 | void submit(void (*closure)()) | |
149 | { | |
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))); | |
153 | } | |
154 | ||
155 | template <typename Closure> | |
156 | void submit(BOOST_THREAD_FWD_REF(Closure) closure) | |
157 | { | |
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)))); | |
161 | } | |
162 | ||
163 | }; | |
164 | } | |
165 | using executors::serial_executor_cont; | |
166 | } | |
167 | ||
168 | #include <boost/config/abi_suffix.hpp> | |
169 | ||
170 | #endif |