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