]>
Commit | Line | Data |
---|---|---|
1 | // Copyright (C) 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 serial scheduler. | |
8 | ||
9 | #ifndef BOOST_THREAD_INLINE_EXECUTOR_HPP | |
10 | #define BOOST_THREAD_INLINE_EXECUTOR_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/executors/work.hpp> | |
16 | ||
17 | #include <boost/config/abi_prefix.hpp> | |
18 | ||
19 | namespace boost | |
20 | { | |
21 | namespace executors | |
22 | { | |
23 | class inline_executor | |
24 | { | |
25 | public: | |
26 | /// type-erasure to store the works to do | |
27 | typedef executors::work work; | |
28 | bool closed_; | |
29 | mutable mutex mtx_; | |
30 | /** | |
31 | * Effects: try to execute one task. | |
32 | * Returns: whether a task has been executed. | |
33 | * Throws: whatever the current task constructor throws or the task() throws. | |
34 | */ | |
35 | bool try_executing_one() | |
36 | { | |
37 | return false; | |
38 | } | |
39 | ||
40 | public: | |
41 | /// inline_executor is not copyable. | |
42 | BOOST_THREAD_NO_COPYABLE(inline_executor) | |
43 | ||
44 | /** | |
45 | * \b Effects: creates a inline executor that runs closures immediately. | |
46 | * | |
47 | * \b Throws: Nothing. | |
48 | */ | |
49 | inline_executor() | |
50 | : closed_(false) | |
51 | { | |
52 | } | |
53 | /** | |
54 | * \b Effects: Destroys the inline executor. | |
55 | * | |
56 | * \b Synchronization: The completion of all the closures happen before the completion of the \c inline_executor destructor. | |
57 | */ | |
58 | ~inline_executor() | |
59 | { | |
60 | // signal to all the worker thread that there will be no more submissions. | |
61 | close(); | |
62 | } | |
63 | ||
64 | /** | |
65 | * \b Effects: close the \c inline_executor for submissions. | |
66 | * The loop will work until there is no more closures to run. | |
67 | */ | |
68 | void close() | |
69 | { | |
70 | lock_guard<mutex> lk(mtx_); | |
71 | closed_ = true; | |
72 | } | |
73 | ||
74 | /** | |
75 | * \b Returns: whether the pool is closed for submissions. | |
76 | */ | |
77 | bool closed(lock_guard<mutex>& ) | |
78 | { | |
79 | return closed_; | |
80 | } | |
81 | bool closed() | |
82 | { | |
83 | lock_guard<mutex> lk(mtx_); | |
84 | return closed(lk); | |
85 | } | |
86 | ||
87 | /** | |
88 | * \b Requires: \c Closure is a model of \c Callable(void()) and a model of \c CopyConstructible/MoveConstructible. | |
89 | * | |
90 | * \b Effects: The specified \c closure will be scheduled for execution at some point in the future. | |
91 | * If invoked closure throws an exception the \c inline_executor will call \c std::terminate, as is the case with threads. | |
92 | * | |
93 | * \b Synchronization: completion of \c closure on a particular thread happens before destruction of thread's thread local variables. | |
94 | * | |
95 | * \b Throws: \c sync_queue_is_closed if the thread pool is closed. | |
96 | * Whatever exception that can be throw while storing the closure. | |
97 | */ | |
98 | ||
99 | #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) | |
100 | template <typename Closure> | |
101 | void submit(Closure & closure) | |
102 | { | |
103 | { | |
104 | lock_guard<mutex> lk(mtx_); | |
105 | if (closed(lk)) BOOST_THROW_EXCEPTION( sync_queue_is_closed() ); | |
106 | } | |
107 | try | |
108 | { | |
109 | closure(); | |
110 | } | |
111 | catch (...) | |
112 | { | |
113 | std::terminate(); | |
114 | return; | |
115 | } | |
116 | } | |
117 | #endif | |
118 | void submit(void (*closure)()) | |
119 | { | |
120 | { | |
121 | lock_guard<mutex> lk(mtx_); | |
122 | if (closed(lk)) BOOST_THROW_EXCEPTION( sync_queue_is_closed() ); | |
123 | } | |
124 | try | |
125 | { | |
126 | closure(); | |
127 | } | |
128 | catch (...) | |
129 | { | |
130 | std::terminate(); | |
131 | return; | |
132 | } | |
133 | } | |
134 | ||
135 | template <typename Closure> | |
136 | void submit(BOOST_THREAD_FWD_REF(Closure) closure) | |
137 | { | |
138 | { | |
139 | lock_guard<mutex> lk(mtx_); | |
140 | if (closed(lk)) BOOST_THROW_EXCEPTION( sync_queue_is_closed() ); | |
141 | } | |
142 | try | |
143 | { | |
144 | closure(); | |
145 | } | |
146 | catch (...) | |
147 | { | |
148 | std::terminate(); | |
149 | return; | |
150 | } | |
151 | } | |
152 | ||
153 | /** | |
154 | * \b Requires: This must be called from an scheduled task. | |
155 | * | |
156 | * \b Effects: reschedule functions until pred() | |
157 | */ | |
158 | template <typename Pred> | |
159 | bool reschedule_until(Pred const& ) | |
160 | { | |
161 | return false; | |
162 | } | |
163 | ||
164 | }; | |
165 | } | |
166 | using executors::inline_executor; | |
167 | } | |
168 | ||
169 | #include <boost/config/abi_suffix.hpp> | |
170 | ||
171 | #endif |