1 | /*
|
---|
2 | * Copyright (c) 2019 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #ifndef LIBCPP_BITS_THREAD_PACKAGED_TASK
|
---|
30 | #define LIBCPP_BITS_THREAD_PACKAGED_TASK
|
---|
31 |
|
---|
32 | #include <__bits/exception.hpp>
|
---|
33 | #include <__bits/functional/function.hpp>
|
---|
34 | #include <__bits/memory/allocator_traits.hpp>
|
---|
35 | #include <__bits/thread/future.hpp>
|
---|
36 | #include <__bits/thread/future_common.hpp>
|
---|
37 | #include <__bits/thread/shared_state.hpp>
|
---|
38 | #include <type_traits>
|
---|
39 | #include <utility>
|
---|
40 |
|
---|
41 | namespace std
|
---|
42 | {
|
---|
43 | /**
|
---|
44 | * 30.6.9, class template packaged_task:
|
---|
45 | */
|
---|
46 |
|
---|
47 | template<class>
|
---|
48 | class packaged_task; // undefined
|
---|
49 |
|
---|
50 | template<class R, class... Args>
|
---|
51 | class packaged_task<R(Args...)>
|
---|
52 | {
|
---|
53 | public:
|
---|
54 | packaged_task() noexcept
|
---|
55 | : func_{}, state_{}
|
---|
56 | { /* DUMMY BODY */ }
|
---|
57 |
|
---|
58 | template<
|
---|
59 | class F, enable_if_t<
|
---|
60 | !is_same_v<
|
---|
61 | decay_t<F>, packaged_task<R(Args...)>
|
---|
62 | >, int
|
---|
63 | > = 0
|
---|
64 | >
|
---|
65 | explicit packaged_task(F&& f)
|
---|
66 | : func_{forward<F>(f)}, state_{new aux::shared_state<R>{}}
|
---|
67 | { /* DUMMY BODY */ }
|
---|
68 |
|
---|
69 | template<
|
---|
70 | class F, class Allocator, enable_if_t<
|
---|
71 | is_same_v<
|
---|
72 | decay_t<F>, packaged_task<R(Args...)>
|
---|
73 | >, int
|
---|
74 | > = 0
|
---|
75 | >
|
---|
76 | explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f)
|
---|
77 | : func_{forward<F>(f)}, state_{}
|
---|
78 | {
|
---|
79 | typename allocator_traits<
|
---|
80 | Allocator
|
---|
81 | >::template rebind_alloc<aux::shared_state<R>> rebound{a};
|
---|
82 |
|
---|
83 | state_ = rebound.allocate(1);
|
---|
84 | rebound.construct(state_);
|
---|
85 | }
|
---|
86 |
|
---|
87 | ~packaged_task()
|
---|
88 | {
|
---|
89 | if (state_)
|
---|
90 | {
|
---|
91 | if (!state_->is_set())
|
---|
92 | {
|
---|
93 | state_->set_exception(make_exception_ptr(
|
---|
94 | future_error{make_error_code(future_errc::broken_promise)}
|
---|
95 | ));
|
---|
96 | state_->mark_set(true);
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (state_->decrement())
|
---|
100 | {
|
---|
101 | state_->destroy();
|
---|
102 | delete state_;
|
---|
103 | state_ = nullptr;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | packaged_task(const packaged_task&) = delete;
|
---|
109 | packaged_task& operator=(const packaged_task&) = delete;
|
---|
110 |
|
---|
111 | packaged_task(packaged_task&& rhs)
|
---|
112 | : func_{move(rhs.func_)}, state_{move(rhs.state_)}
|
---|
113 | { /* DUMMY BODY */ }
|
---|
114 |
|
---|
115 | packaged_task& operator=(packaged_task&& rhs)
|
---|
116 | {
|
---|
117 | if (state_)
|
---|
118 | {
|
---|
119 | if (state_->decrement())
|
---|
120 | {
|
---|
121 | state_->destroy();
|
---|
122 | delete state_;
|
---|
123 | state_ = nullptr;
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | func_ = move(rhs.func_);
|
---|
128 | state_ = move(rhs.state_);
|
---|
129 | rhs.state_ = nullptr;
|
---|
130 |
|
---|
131 | return *this;
|
---|
132 | }
|
---|
133 |
|
---|
134 | void swap(packaged_task& other) noexcept
|
---|
135 | {
|
---|
136 | std::swap(func_, other.func_);
|
---|
137 | std::swap(state_, other.state_);
|
---|
138 | }
|
---|
139 |
|
---|
140 | bool valid() const noexcept
|
---|
141 | {
|
---|
142 | return state_ != nullptr;
|
---|
143 | }
|
---|
144 |
|
---|
145 | future<R> get_future()
|
---|
146 | {
|
---|
147 | if (!state_)
|
---|
148 | throw future_error{make_error_code(future_errc::no_state)};
|
---|
149 |
|
---|
150 | state_->increment();
|
---|
151 |
|
---|
152 | return future<R>{state_};
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Note: This is how the signature is in the standard,
|
---|
157 | * should be investigated and verified.
|
---|
158 | */
|
---|
159 | void operator()(Args... args)
|
---|
160 | {
|
---|
161 | if (!state_)
|
---|
162 | throw future_error{make_error_code(future_errc::no_state)};
|
---|
163 | if (state_->is_set())
|
---|
164 | {
|
---|
165 | throw future_error{
|
---|
166 | make_error_code(future_errc::promise_already_satisfied)
|
---|
167 | };
|
---|
168 | }
|
---|
169 |
|
---|
170 | try
|
---|
171 | {
|
---|
172 | state_->set_value(invoke(func_, args...));
|
---|
173 | }
|
---|
174 | catch(const exception& __exception)
|
---|
175 | {
|
---|
176 | state_->set_exception(make_exception_ptr(__exception));
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | void make_ready_at_thread_exit(Args... args)
|
---|
181 | {
|
---|
182 | if (!state_)
|
---|
183 | throw future_error{make_error_code(future_errc::no_state)};
|
---|
184 | if (state_->is_set())
|
---|
185 | {
|
---|
186 | throw future_error{
|
---|
187 | make_error_code(future_errc::promise_already_satisfied)
|
---|
188 | };
|
---|
189 | }
|
---|
190 |
|
---|
191 | try
|
---|
192 | {
|
---|
193 | state_->set_value(invoke(func_, args...), false);
|
---|
194 | aux::set_state_value_at_thread_exit(this->state_);
|
---|
195 | }
|
---|
196 | catch(const exception& __exception)
|
---|
197 | {
|
---|
198 | state_->set_exception(make_exception_ptr(__exception), false);
|
---|
199 | aux::set_state_exception_at_thread_exit(this->state_);
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | void reset()
|
---|
204 | {
|
---|
205 | if (!state_)
|
---|
206 | throw future_error{make_error_code(future_errc::no_state)};
|
---|
207 |
|
---|
208 | *this = packaged_task{move(func_)};
|
---|
209 | }
|
---|
210 |
|
---|
211 | private:
|
---|
212 | function<R(Args...)> func_;
|
---|
213 |
|
---|
214 | aux::shared_state<R>* state_;
|
---|
215 | };
|
---|
216 |
|
---|
217 | template<class R, class... Args>
|
---|
218 | void swap(packaged_task<R(Args...)>& lhs, packaged_task<R(Args...)>& rhs) noexcept
|
---|
219 | {
|
---|
220 | lhs.swap(rhs);
|
---|
221 | };
|
---|
222 |
|
---|
223 | template<class R, class Alloc>
|
---|
224 | struct uses_allocator<packaged_task<R>, Alloc>: true_type
|
---|
225 | { /* DUMMY BODY */ };
|
---|
226 | }
|
---|
227 |
|
---|
228 | #endif
|
---|