Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / src / future.cpp
blob3383b506a2fab3d7e78c1a3263fe0bbae85b64b4
1 //===----------------------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include <__config>
11 #ifndef _LIBCPP_HAS_NO_THREADS
13 #include <future>
14 #include <string>
16 _LIBCPP_BEGIN_NAMESPACE_STD
18 class _LIBCPP_HIDDEN __future_error_category
19 : public __do_message
21 public:
22 virtual const char* name() const noexcept;
23 virtual string message(int ev) const;
26 const char*
27 __future_error_category::name() const noexcept
29 return "future";
32 _LIBCPP_DIAGNOSTIC_PUSH
33 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wswitch")
34 _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wswitch")
36 string
37 __future_error_category::message(int ev) const
39 switch (static_cast<future_errc>(ev))
41 case future_errc(0): // For backwards compatibility with C++11 (LWG 2056)
42 case future_errc::broken_promise:
43 return string("The associated promise has been destructed prior "
44 "to the associated state becoming ready.");
45 case future_errc::future_already_retrieved:
46 return string("The future has already been retrieved from "
47 "the promise or packaged_task.");
48 case future_errc::promise_already_satisfied:
49 return string("The state of the promise has already been set.");
50 case future_errc::no_state:
51 return string("Operation not permitted on an object without "
52 "an associated state.");
54 return string("unspecified future_errc value\n");
57 _LIBCPP_DIAGNOSTIC_POP
59 const error_category&
60 future_category() noexcept
62 union AvoidDestroyingFutureCategory {
63 __future_error_category future_error_category;
64 constexpr explicit AvoidDestroyingFutureCategory() : future_error_category() {}
65 ~AvoidDestroyingFutureCategory() {}
67 constinit static AvoidDestroyingFutureCategory helper;
68 return helper.future_error_category;
71 future_error::future_error(error_code __ec)
72 : logic_error(__ec.message()),
73 __ec_(__ec)
77 future_error::~future_error() noexcept
81 void
82 __assoc_sub_state::__on_zero_shared() noexcept
84 delete this;
87 void
88 __assoc_sub_state::set_value()
90 unique_lock<mutex> __lk(__mut_);
91 if (__has_value())
92 __throw_future_error(future_errc::promise_already_satisfied);
93 __state_ |= __constructed | ready;
94 __cv_.notify_all();
97 void
98 __assoc_sub_state::set_value_at_thread_exit()
100 unique_lock<mutex> __lk(__mut_);
101 if (__has_value())
102 __throw_future_error(future_errc::promise_already_satisfied);
103 __state_ |= __constructed;
104 __thread_local_data()->__make_ready_at_thread_exit(this);
107 void
108 __assoc_sub_state::set_exception(exception_ptr __p)
110 unique_lock<mutex> __lk(__mut_);
111 if (__has_value())
112 __throw_future_error(future_errc::promise_already_satisfied);
113 __exception_ = __p;
114 __state_ |= ready;
115 __cv_.notify_all();
118 void
119 __assoc_sub_state::set_exception_at_thread_exit(exception_ptr __p)
121 unique_lock<mutex> __lk(__mut_);
122 if (__has_value())
123 __throw_future_error(future_errc::promise_already_satisfied);
124 __exception_ = __p;
125 __thread_local_data()->__make_ready_at_thread_exit(this);
128 void
129 __assoc_sub_state::__make_ready()
131 unique_lock<mutex> __lk(__mut_);
132 __state_ |= ready;
133 __cv_.notify_all();
136 void
137 __assoc_sub_state::copy()
139 unique_lock<mutex> __lk(__mut_);
140 __sub_wait(__lk);
141 if (__exception_ != nullptr)
142 rethrow_exception(__exception_);
145 void
146 __assoc_sub_state::wait()
148 unique_lock<mutex> __lk(__mut_);
149 __sub_wait(__lk);
152 void
153 __assoc_sub_state::__sub_wait(unique_lock<mutex>& __lk)
155 if (!__is_ready())
157 if (__state_ & static_cast<unsigned>(deferred))
159 __state_ &= ~static_cast<unsigned>(deferred);
160 __lk.unlock();
161 __execute();
163 else
164 while (!__is_ready())
165 __cv_.wait(__lk);
169 void
170 __assoc_sub_state::__execute()
172 __throw_future_error(future_errc::no_state);
175 future<void>::future(__assoc_sub_state* __state)
176 : __state_(__state)
178 __state_->__attach_future();
181 future<void>::~future()
183 if (__state_)
184 __state_->__release_shared();
187 void
188 future<void>::get()
190 unique_ptr<__shared_count, __release_shared_count> __(__state_);
191 __assoc_sub_state* __s = __state_;
192 __state_ = nullptr;
193 __s->copy();
196 promise<void>::promise()
197 : __state_(new __assoc_sub_state)
201 promise<void>::~promise()
203 if (__state_)
205 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
206 if (!__state_->__has_value() && __state_->use_count() > 1)
207 __state_->set_exception(make_exception_ptr(future_error(future_errc::broken_promise)));
208 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
209 __state_->__release_shared();
213 future<void>
214 promise<void>::get_future()
216 if (__state_ == nullptr)
217 __throw_future_error(future_errc::no_state);
218 return future<void>(__state_);
221 void
222 promise<void>::set_value()
224 if (__state_ == nullptr)
225 __throw_future_error(future_errc::no_state);
226 __state_->set_value();
229 void
230 promise<void>::set_exception(exception_ptr __p)
232 if (__state_ == nullptr)
233 __throw_future_error(future_errc::no_state);
234 __state_->set_exception(__p);
237 void
238 promise<void>::set_value_at_thread_exit()
240 if (__state_ == nullptr)
241 __throw_future_error(future_errc::no_state);
242 __state_->set_value_at_thread_exit();
245 void
246 promise<void>::set_exception_at_thread_exit(exception_ptr __p)
248 if (__state_ == nullptr)
249 __throw_future_error(future_errc::no_state);
250 __state_->set_exception_at_thread_exit(__p);
253 shared_future<void>::~shared_future()
255 if (__state_)
256 __state_->__release_shared();
259 shared_future<void>&
260 shared_future<void>::operator=(const shared_future& __rhs)
262 if (__rhs.__state_)
263 __rhs.__state_->__add_shared();
264 if (__state_)
265 __state_->__release_shared();
266 __state_ = __rhs.__state_;
267 return *this;
270 _LIBCPP_END_NAMESPACE_STD
272 #endif // !_LIBCPP_HAS_NO_THREADS