1 //===------------------------- future.cpp ---------------------------------===//
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
7 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_HAS_NO_THREADS
16 _LIBCPP_BEGIN_NAMESPACE_STD
18 class _LIBCPP_HIDDEN __future_error_category
22 virtual const char* name() const _NOEXCEPT;
23 virtual string message(int ev) const;
27 __future_error_category::name() const _NOEXCEPT
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"
41 __future_error_category::message(int ev) const
43 switch (static_cast<future_errc>(ev))
45 case future_errc(0): // For backwards compatibility with C++11 (LWG 2056)
46 case future_errc::broken_promise:
47 return string("The associated promise has been destructed prior "
48 "to the associated state becoming ready.");
49 case future_errc::future_already_retrieved:
50 return string("The future has already been retrieved from "
51 "the promise or packaged_task.");
52 case future_errc::promise_already_satisfied:
53 return string("The state of the promise has already been set.");
54 case future_errc::no_state:
55 return string("Operation not permitted on an object without "
56 "an associated state.");
58 return string("unspecified future_errc value\n");
61 #if defined(__clang__)
62 #pragma clang diagnostic pop
63 #elif defined(__GNUC__) || defined(__GNUG__)
64 #pragma GCC diagnostic pop
68 future_category() _NOEXCEPT
70 static __future_error_category __f;
74 future_error::future_error(error_code __ec)
75 : logic_error(__ec.message()),
80 future_error::~future_error() _NOEXCEPT
85 __assoc_sub_state::__on_zero_shared() _NOEXCEPT
91 __assoc_sub_state::set_value()
93 unique_lock<mutex> __lk(__mut_);
95 __throw_future_error(future_errc::promise_already_satisfied);
96 __state_ |= __constructed | ready;
101 __assoc_sub_state::set_value_at_thread_exit()
103 unique_lock<mutex> __lk(__mut_);
105 __throw_future_error(future_errc::promise_already_satisfied);
106 __state_ |= __constructed;
107 __thread_local_data()->__make_ready_at_thread_exit(this);
111 __assoc_sub_state::set_exception(exception_ptr __p)
113 unique_lock<mutex> __lk(__mut_);
115 __throw_future_error(future_errc::promise_already_satisfied);
122 __assoc_sub_state::set_exception_at_thread_exit(exception_ptr __p)
124 unique_lock<mutex> __lk(__mut_);
126 __throw_future_error(future_errc::promise_already_satisfied);
128 __thread_local_data()->__make_ready_at_thread_exit(this);
132 __assoc_sub_state::__make_ready()
134 unique_lock<mutex> __lk(__mut_);
140 __assoc_sub_state::copy()
142 unique_lock<mutex> __lk(__mut_);
144 if (__exception_ != nullptr)
145 rethrow_exception(__exception_);
149 __assoc_sub_state::wait()
151 unique_lock<mutex> __lk(__mut_);
156 __assoc_sub_state::__sub_wait(unique_lock<mutex>& __lk)
160 if (__state_ & static_cast<unsigned>(deferred))
162 __state_ &= ~static_cast<unsigned>(deferred);
167 while (!__is_ready())
173 __assoc_sub_state::__execute()
175 __throw_future_error(future_errc::no_state);
178 future<void>::future(__assoc_sub_state* __state)
181 __state_->__attach_future();
184 future<void>::~future()
187 __state_->__release_shared();
193 unique_ptr<__shared_count, __release_shared_count> __(__state_);
194 __assoc_sub_state* __s = __state_;
199 promise<void>::promise()
200 : __state_(new __assoc_sub_state)
204 promise<void>::~promise()
208 #ifndef _LIBCPP_NO_EXCEPTIONS
209 if (!__state_->__has_value() && __state_->use_count() > 1)
210 __state_->set_exception(make_exception_ptr(
211 future_error(make_error_code(future_errc::broken_promise))
213 #endif // _LIBCPP_NO_EXCEPTIONS
214 __state_->__release_shared();
219 promise<void>::get_future()
221 if (__state_ == nullptr)
222 __throw_future_error(future_errc::no_state);
223 return future<void>(__state_);
227 promise<void>::set_value()
229 if (__state_ == nullptr)
230 __throw_future_error(future_errc::no_state);
231 __state_->set_value();
235 promise<void>::set_exception(exception_ptr __p)
237 if (__state_ == nullptr)
238 __throw_future_error(future_errc::no_state);
239 __state_->set_exception(__p);
243 promise<void>::set_value_at_thread_exit()
245 if (__state_ == nullptr)
246 __throw_future_error(future_errc::no_state);
247 __state_->set_value_at_thread_exit();
251 promise<void>::set_exception_at_thread_exit(exception_ptr __p)
253 if (__state_ == nullptr)
254 __throw_future_error(future_errc::no_state);
255 __state_->set_exception_at_thread_exit(__p);
258 shared_future<void>::~shared_future()
261 __state_->__release_shared();
265 shared_future<void>::operator=(const shared_future& __rhs)
268 __rhs.__state_->__add_shared();
270 __state_->__release_shared();
271 __state_ = __rhs.__state_;
275 _LIBCPP_END_NAMESPACE_STD
277 #endif // !_LIBCPP_HAS_NO_THREADS