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