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/09 Vicente J. Botet Escriba |
7 | // Adapt to boost from CCIA C++11 implementation |
8 | |
9 | #ifndef BOOST_THREAD_EXECUTORS_EXECUTOR_HPP |
10 | #define BOOST_THREAD_EXECUTORS_EXECUTOR_HPP |
11 | |
12 | #include <boost/thread/detail/config.hpp> |
13 | |
14 | #include <boost/thread/detail/delete.hpp> |
15 | #include <boost/thread/detail/move.hpp> |
16 | #include <boost/thread/executors/work.hpp> |
17 | |
18 | #include <boost/config/abi_prefix.hpp> |
19 | |
20 | namespace boost |
21 | { |
22 | namespace executors |
23 | { |
24 | class executor |
25 | { |
26 | public: |
27 | /// type-erasure to store the works to do |
28 | typedef executors::work work; |
29 | |
30 | /// executor is not copyable. |
31 | BOOST_THREAD_NO_COPYABLE(executor) |
32 | executor() {} |
33 | |
34 | /** |
35 | * \par Effects |
36 | * Destroys the executor. |
37 | * |
38 | * \par Synchronization |
39 | * The completion of all the closures happen before the completion of the executor destructor. |
40 | */ |
41 | virtual ~executor() {} |
42 | |
43 | /** |
44 | * \par Effects |
45 | * Close the \c executor for submissions. |
46 | * The worker threads will work until there is no more closures to run. |
47 | */ |
48 | virtual void close() = 0; |
49 | |
50 | /** |
51 | * \par Returns |
52 | * Whether the pool is closed for submissions. |
53 | */ |
54 | virtual bool closed() = 0; |
55 | |
56 | /** |
57 | * \par Effects |
58 | * The specified closure will be scheduled for execution at some point in the future. |
59 | * If invoked closure throws an exception the executor will call std::terminate, as is the case with threads. |
60 | * |
61 | * \par Synchronization |
62 | * Ccompletion of closure on a particular thread happens before destruction of thread's thread local variables. |
63 | * |
64 | * \par Throws |
65 | * \c sync_queue_is_closed if the thread pool is closed. |
66 | * Whatever exception that can be throw while storing the closure. |
67 | */ |
68 | virtual void submit(BOOST_THREAD_RV_REF(work) closure) = 0; |
69 | // virtual void submit(work& closure) = 0; |
70 | |
71 | /** |
72 | * \par Requires |
73 | * \c Closure is a model of Callable(void()) and a model of CopyConstructible/MoveConstructible. |
74 | * |
75 | * \par Effects |
76 | * The specified closure will be scheduled for execution at some point in the future. |
77 | * If invoked closure throws an exception the thread pool will call std::terminate, as is the case with threads. |
78 | * |
79 | * \par Synchronization |
80 | * Completion of closure on a particular thread happens before destruction of thread's thread local variables. |
81 | * |
82 | * \par Throws |
83 | * \c sync_queue_is_closed if the thread pool is closed. |
84 | * Whatever exception that can be throw while storing the closure. |
85 | */ |
86 | |
87 | #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) |
88 | template <typename Closure> |
89 | void submit(Closure & closure) |
90 | { |
91 | work w ((closure)); |
92 | submit(boost::move(w)); |
93 | } |
94 | #endif |
95 | void submit(void (*closure)()) |
96 | { |
97 | work w ((closure)); |
98 | submit(boost::move(w)); |
99 | } |
100 | |
101 | template <typename Closure> |
102 | void submit(BOOST_THREAD_FWD_REF(Closure) closure) |
103 | { |
104 | //submit(work(boost::forward<Closure>(closure))); |
105 | work w((boost::forward<Closure>(closure))); |
106 | submit(boost::move(w)); |
107 | } |
108 | |
109 | /** |
110 | * \par Effects |
111 | * Try to execute one task. |
112 | * |
113 | * \par Returns |
114 | * Whether a task has been executed. |
115 | * |
116 | * \par Throws |
117 | * Whatever the current task constructor throws or the task() throws. |
118 | */ |
119 | virtual bool try_executing_one() = 0; |
120 | |
121 | /** |
122 | * \par Requires |
123 | * This must be called from an scheduled task. |
124 | * |
125 | * \par Effects |
126 | * Reschedule functions until pred() |
127 | */ |
128 | template <typename Pred> |
129 | bool reschedule_until(Pred const& pred) |
130 | { |
131 | do { |
132 | //schedule_one_or_yield(); |
133 | if ( ! try_executing_one()) |
134 | { |
135 | return false; |
136 | } |
137 | } while (! pred()); |
138 | return true; |
139 | } |
140 | }; |
141 | |
142 | } |
143 | using executors::executor; |
144 | } |
145 | |
146 | #include <boost/config/abi_suffix.hpp> |
147 | |
148 | #endif |
149 | |