]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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_ADAPTOR_HPP | |
10 | #define BOOST_THREAD_EXECUTORS_EXECUTOR_ADAPTOR_HPP | |
11 | ||
12 | #include <boost/thread/detail/config.hpp> | |
92f5a8d4 | 13 | #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION && defined BOOST_THREAD_PROVIDES_EXECUTORS && defined BOOST_THREAD_USES_MOVE |
7c673cae FG |
14 | |
15 | #include <boost/thread/executors/executor.hpp> | |
16 | ||
17 | #include <boost/config/abi_prefix.hpp> | |
18 | ||
19 | namespace boost | |
20 | { | |
21 | namespace executors | |
22 | { | |
23 | /** | |
24 | * Polymorphic adaptor of a model of Executor to an executor. | |
25 | */ | |
26 | template <typename Executor> | |
27 | class executor_adaptor : public executor | |
28 | { | |
29 | Executor ex; | |
30 | public: | |
31 | /// type-erasure to store the works to do | |
32 | typedef executor::work work; | |
33 | ||
34 | /// executor is not copyable. | |
35 | BOOST_THREAD_NO_COPYABLE(executor_adaptor) | |
36 | ||
37 | /** | |
38 | * executor_adaptor constructor | |
39 | */ | |
40 | #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) | |
41 | template <typename ...Args> | |
42 | executor_adaptor(BOOST_THREAD_RV_REF(Args) ... args) : ex(boost::forward<Args>(args)...) {} | |
43 | #else | |
44 | /** | |
45 | * executor_adaptor constructor | |
46 | */ | |
47 | executor_adaptor() : ex() {} | |
48 | ||
49 | template <typename A1> | |
50 | executor_adaptor( | |
51 | BOOST_THREAD_FWD_REF(A1) a1 | |
52 | ) : | |
53 | ex( | |
54 | boost::forward<A1>(a1) | |
55 | ) {} | |
56 | template <typename A1, typename A2> | |
57 | executor_adaptor( | |
58 | BOOST_THREAD_FWD_REF(A1) a1, | |
59 | BOOST_THREAD_FWD_REF(A2) a2 | |
60 | ) : | |
61 | ex( | |
62 | boost::forward<A1>(a1), | |
63 | boost::forward<A2>(a2) | |
64 | ) {} | |
65 | template <typename A1, typename A2, typename A3> | |
66 | executor_adaptor( | |
67 | BOOST_THREAD_FWD_REF(A1) a1, | |
68 | BOOST_THREAD_FWD_REF(A2) a2, | |
69 | BOOST_THREAD_FWD_REF(A3) a3 | |
70 | ) : | |
71 | ex( | |
72 | boost::forward<A1>(a1), | |
73 | boost::forward<A2>(a2), | |
74 | boost::forward<A3>(a3) | |
75 | ) {} | |
76 | #endif | |
77 | Executor& underlying_executor() { return ex; } | |
78 | ||
79 | /** | |
80 | * \b Effects: close the \c executor for submissions. | |
81 | * The worker threads will work until there is no more closures to run. | |
82 | */ | |
83 | void close() { ex.close(); } | |
84 | ||
85 | /** | |
86 | * \b Returns: whether the pool is closed for submissions. | |
87 | */ | |
88 | bool closed() { return ex.closed(); } | |
89 | ||
90 | /** | |
91 | * \b Effects: The specified closure will be scheduled for execution at some point in the future. | |
92 | * If invoked closure throws an exception the executor will call std::terminate, as is the case with threads. | |
93 | * | |
94 | * \b Synchronization: completion of closure on a particular thread happens before destruction of thread's thread local variables. | |
95 | * | |
96 | * \b Throws: \c sync_queue_is_closed if the thread pool is closed. | |
97 | * Whatever exception that can be throw while storing the closure. | |
98 | */ | |
99 | void submit(BOOST_THREAD_RV_REF(work) closure) { | |
100 | return ex.submit(boost::move(closure)); | |
101 | } | |
102 | ||
103 | #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) | |
104 | template <typename Closure> | |
105 | void submit(Closure & closure) | |
106 | { | |
107 | submit(work(closure)); | |
108 | } | |
109 | #endif | |
110 | void submit(void (*closure)()) | |
111 | { | |
112 | submit(work(closure)); | |
113 | } | |
114 | ||
115 | template <typename Closure> | |
116 | void submit(BOOST_THREAD_FWD_REF(Closure) closure) | |
117 | { | |
118 | //submit(work(boost::forward<Closure>(closure))); | |
119 | work w((boost::forward<Closure>(closure))); | |
120 | submit(boost::move(w)); | |
121 | } | |
122 | ||
123 | /** | |
124 | * Effects: try to execute one task. | |
125 | * Returns: whether a task has been executed. | |
126 | * Throws: whatever the current task constructor throws or the task() throws. | |
127 | */ | |
128 | bool try_executing_one() { return ex.try_executing_one(); } | |
129 | ||
130 | }; | |
131 | } | |
132 | using executors::executor_adaptor; | |
133 | } | |
134 | ||
135 | #include <boost/config/abi_suffix.hpp> | |
136 | ||
137 | #endif | |
92f5a8d4 | 138 | #endif |