/Users/buildslave/jenkins/workspace/coverage/llvm-project/libcxx/src/future.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===------------------------- future.cpp ---------------------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | |
9 | | #include "__config" |
10 | | |
11 | | #ifndef _LIBCPP_HAS_NO_THREADS |
12 | | |
13 | | #include "future" |
14 | | #include "string" |
15 | | |
16 | | _LIBCPP_BEGIN_NAMESPACE_STD |
17 | | |
18 | | class _LIBCPP_HIDDEN __future_error_category |
19 | | : public __do_message |
20 | | { |
21 | | public: |
22 | | virtual const char* name() const _NOEXCEPT; |
23 | | virtual string message(int ev) const; |
24 | | }; |
25 | | |
26 | | const char* |
27 | | __future_error_category::name() const _NOEXCEPT |
28 | 0 | { |
29 | 0 | return "future"; |
30 | 0 | } |
31 | | |
32 | | #if defined(__clang__) |
33 | | #pragma clang diagnostic push |
34 | | #pragma clang diagnostic ignored "-Wswitch" |
35 | | #elif defined(__GNUC__) || defined(__GNUG__) |
36 | | #pragma GCC diagnostic push |
37 | | #pragma GCC diagnostic ignored "-Wswitch" |
38 | | #endif |
39 | | |
40 | | string |
41 | | __future_error_category::message(int ev) const |
42 | 0 | { |
43 | 0 | switch (static_cast<future_errc>(ev)) |
44 | 0 | { |
45 | 0 | case future_errc(0): // For backwards compatibility with C++11 (LWG 2056) |
46 | 0 | case future_errc::broken_promise: |
47 | 0 | return string("The associated promise has been destructed prior " |
48 | 0 | "to the associated state becoming ready."); |
49 | 0 | case future_errc::future_already_retrieved: |
50 | 0 | return string("The future has already been retrieved from " |
51 | 0 | "the promise or packaged_task."); |
52 | 0 | case future_errc::promise_already_satisfied: |
53 | 0 | return string("The state of the promise has already been set."); |
54 | 0 | case future_errc::no_state: |
55 | 0 | return string("Operation not permitted on an object without " |
56 | 0 | "an associated state."); |
57 | 0 | } |
58 | 0 | return string("unspecified future_errc value\n"); |
59 | 0 | } |
60 | | |
61 | | #if defined(__clang__) |
62 | | #pragma clang diagnostic pop |
63 | | #elif defined(__GNUC__) || defined(__GNUG__) |
64 | | #pragma GCC diagnostic pop |
65 | | #endif |
66 | | |
67 | | const error_category& |
68 | | future_category() _NOEXCEPT |
69 | 0 | { |
70 | 0 | static __future_error_category __f; |
71 | 0 | return __f; |
72 | 0 | } |
73 | | |
74 | | future_error::future_error(error_code __ec) |
75 | | : logic_error(__ec.message()), |
76 | | __ec_(__ec) |
77 | 0 | { |
78 | 0 | } |
79 | | |
80 | | future_error::~future_error() _NOEXCEPT |
81 | 0 | { |
82 | 0 | } |
83 | | |
84 | | void |
85 | | __assoc_sub_state::__on_zero_shared() _NOEXCEPT |
86 | 102 | { |
87 | 102 | delete this; |
88 | 102 | } |
89 | | |
90 | | void |
91 | | __assoc_sub_state::set_value() |
92 | 102 | { |
93 | 102 | unique_lock<mutex> __lk(__mut_); |
94 | 102 | if (__has_value()) |
95 | 0 | __throw_future_error(future_errc::promise_already_satisfied); |
96 | 102 | __state_ |= __constructed | ready; |
97 | 102 | __cv_.notify_all(); |
98 | 102 | } |
99 | | |
100 | | void |
101 | | __assoc_sub_state::set_value_at_thread_exit() |
102 | 0 | { |
103 | 0 | unique_lock<mutex> __lk(__mut_); |
104 | 0 | if (__has_value()) |
105 | 0 | __throw_future_error(future_errc::promise_already_satisfied); |
106 | 0 | __state_ |= __constructed; |
107 | 0 | __thread_local_data()->__make_ready_at_thread_exit(this); |
108 | 0 | } |
109 | | |
110 | | void |
111 | | __assoc_sub_state::set_exception(exception_ptr __p) |
112 | 0 | { |
113 | 0 | unique_lock<mutex> __lk(__mut_); |
114 | 0 | if (__has_value()) |
115 | 0 | __throw_future_error(future_errc::promise_already_satisfied); |
116 | 0 | __exception_ = __p; |
117 | 0 | __state_ |= ready; |
118 | 0 | __cv_.notify_all(); |
119 | 0 | } |
120 | | |
121 | | void |
122 | | __assoc_sub_state::set_exception_at_thread_exit(exception_ptr __p) |
123 | 0 | { |
124 | 0 | unique_lock<mutex> __lk(__mut_); |
125 | 0 | if (__has_value()) |
126 | 0 | __throw_future_error(future_errc::promise_already_satisfied); |
127 | 0 | __exception_ = __p; |
128 | 0 | __thread_local_data()->__make_ready_at_thread_exit(this); |
129 | 0 | } |
130 | | |
131 | | void |
132 | | __assoc_sub_state::__make_ready() |
133 | 0 | { |
134 | 0 | unique_lock<mutex> __lk(__mut_); |
135 | 0 | __state_ |= ready; |
136 | 0 | __cv_.notify_all(); |
137 | 0 | } |
138 | | |
139 | | void |
140 | | __assoc_sub_state::copy() |
141 | 0 | { |
142 | 0 | unique_lock<mutex> __lk(__mut_); |
143 | 0 | __sub_wait(__lk); |
144 | 0 | if (__exception_ != nullptr) |
145 | 0 | rethrow_exception(__exception_); |
146 | 0 | } |
147 | | |
148 | | void |
149 | | __assoc_sub_state::wait() |
150 | 0 | { |
151 | 0 | unique_lock<mutex> __lk(__mut_); |
152 | 0 | __sub_wait(__lk); |
153 | 0 | } |
154 | | |
155 | | void |
156 | | __assoc_sub_state::__sub_wait(unique_lock<mutex>& __lk) |
157 | 0 | { |
158 | 0 | if (!__is_ready()) |
159 | 0 | { |
160 | 0 | if (__state_ & static_cast<unsigned>(deferred)) |
161 | 0 | { |
162 | 0 | __state_ &= ~static_cast<unsigned>(deferred); |
163 | 0 | __lk.unlock(); |
164 | 0 | __execute(); |
165 | 0 | } |
166 | 0 | else |
167 | 0 | while (!__is_ready()) |
168 | 0 | __cv_.wait(__lk); |
169 | 0 | } |
170 | 0 | } |
171 | | |
172 | | void |
173 | | __assoc_sub_state::__execute() |
174 | 0 | { |
175 | 0 | __throw_future_error(future_errc::no_state); |
176 | 0 | } |
177 | | |
178 | | future<void>::future(__assoc_sub_state* __state) |
179 | | : __state_(__state) |
180 | 103 | { |
181 | 103 | __state_->__attach_future(); |
182 | 103 | } |
183 | | |
184 | | future<void>::~future() |
185 | 103 | { |
186 | 103 | if (__state_) |
187 | 0 | __state_->__release_shared(); |
188 | 103 | } |
189 | | |
190 | | void |
191 | | future<void>::get() |
192 | 0 | { |
193 | 0 | unique_ptr<__shared_count, __release_shared_count> __(__state_); |
194 | 0 | __assoc_sub_state* __s = __state_; |
195 | 0 | __state_ = nullptr; |
196 | 0 | __s->copy(); |
197 | 0 | } |
198 | | |
199 | | promise<void>::promise() |
200 | | : __state_(new __assoc_sub_state) |
201 | 103 | { |
202 | 103 | } |
203 | | |
204 | | promise<void>::~promise() |
205 | 443 | { |
206 | 443 | if (__state_) |
207 | 103 | { |
208 | 103 | #ifndef _LIBCPP_NO_EXCEPTIONS |
209 | 103 | if (!__state_->__has_value() && __state_->use_count() > 10 ) |
210 | 0 | __state_->set_exception(make_exception_ptr( |
211 | 0 | future_error(make_error_code(future_errc::broken_promise)) |
212 | 0 | )); |
213 | 103 | #endif // _LIBCPP_NO_EXCEPTIONS |
214 | 103 | __state_->__release_shared(); |
215 | 103 | } |
216 | 443 | } |
217 | | |
218 | | future<void> |
219 | | promise<void>::get_future() |
220 | 103 | { |
221 | 103 | if (__state_ == nullptr) |
222 | 0 | __throw_future_error(future_errc::no_state); |
223 | 103 | return future<void>(__state_); |
224 | 103 | } |
225 | | |
226 | | void |
227 | | promise<void>::set_value() |
228 | 102 | { |
229 | 102 | if (__state_ == nullptr) |
230 | 0 | __throw_future_error(future_errc::no_state); |
231 | 102 | __state_->set_value(); |
232 | 102 | } |
233 | | |
234 | | void |
235 | | promise<void>::set_exception(exception_ptr __p) |
236 | 0 | { |
237 | 0 | if (__state_ == nullptr) |
238 | 0 | __throw_future_error(future_errc::no_state); |
239 | 0 | __state_->set_exception(__p); |
240 | 0 | } |
241 | | |
242 | | void |
243 | | promise<void>::set_value_at_thread_exit() |
244 | 0 | { |
245 | 0 | if (__state_ == nullptr) |
246 | 0 | __throw_future_error(future_errc::no_state); |
247 | 0 | __state_->set_value_at_thread_exit(); |
248 | 0 | } |
249 | | |
250 | | void |
251 | | promise<void>::set_exception_at_thread_exit(exception_ptr __p) |
252 | 0 | { |
253 | 0 | if (__state_ == nullptr) |
254 | 0 | __throw_future_error(future_errc::no_state); |
255 | 0 | __state_->set_exception_at_thread_exit(__p); |
256 | 0 | } |
257 | | |
258 | | shared_future<void>::~shared_future() |
259 | 103 | { |
260 | 103 | if (__state_) |
261 | 103 | __state_->__release_shared(); |
262 | 103 | } |
263 | | |
264 | | shared_future<void>& |
265 | | shared_future<void>::operator=(const shared_future& __rhs) |
266 | 0 | { |
267 | 0 | if (__rhs.__state_) |
268 | 0 | __rhs.__state_->__add_shared(); |
269 | 0 | if (__state_) |
270 | 0 | __state_->__release_shared(); |
271 | 0 | __state_ = __rhs.__state_; |
272 | 0 | return *this; |
273 | 0 | } |
274 | | |
275 | | _LIBCPP_END_NAMESPACE_STD |
276 | | |
277 | | #endif // !_LIBCPP_HAS_NO_THREADS |