1 //===------------------------- future.cpp ---------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #ifndef _LIBCPP_HAS_NO_THREADS
17 _LIBCPP_BEGIN_NAMESPACE_STD
19 class _LIBCPP_HIDDEN __future_error_category
23 virtual const char* name() const _NOEXCEPT;
24 virtual string message(int ev) const;
28 __future_error_category::name() const _NOEXCEPT
33 #if defined(__clang__)
34 #pragma clang diagnostic push
35 #pragma clang diagnostic ignored "-Wswitch"
36 #elif defined(__GNUC__) || defined(__GNUG__)
37 #pragma GCC diagnostic push
38 #pragma GCC diagnostic ignored "-Wswitch"
42 __future_error_category::message(int ev) const
44 switch (static_cast<future_errc>(ev))
46 case future_errc(0): // For backwards compatibility with C++11 (LWG 2056)
47 case future_errc::broken_promise:
48 return string("The associated promise has been destructed prior "
49 "to the associated state becoming ready.");
50 case future_errc::future_already_retrieved:
51 return string("The future has already been retrieved from "
52 "the promise or packaged_task.");
53 case future_errc::promise_already_satisfied:
54 return string("The state of the promise has already been set.");
55 case future_errc::no_state:
56 return string("Operation not permitted on an object without "
57 "an associated state.");
59 return string("unspecified future_errc value\n");
62 #if defined(__clang__)
63 #pragma clang diagnostic pop
64 #elif defined(__GNUC__) || defined(__GNUG__)
65 #pragma GCC diagnostic pop
69 future_category() _NOEXCEPT
71 static __future_error_category __f;
75 future_error::future_error(error_code __ec)
76 : logic_error(__ec.message()),
81 future_error::~future_error() _NOEXCEPT
86 __assoc_sub_state::__on_zero_shared() _NOEXCEPT
92 __assoc_sub_state::set_value()
94 unique_lock<mutex> __lk(__mut_);
95 #ifndef _LIBCPP_NO_EXCEPTIONS
97 throw future_error(make_error_code(future_errc::promise_already_satisfied));
99 __state_ |= __constructed | ready;
105 __assoc_sub_state::set_value_at_thread_exit()
107 unique_lock<mutex> __lk(__mut_);
108 #ifndef _LIBCPP_NO_EXCEPTIONS
110 throw future_error(make_error_code(future_errc::promise_already_satisfied));
112 __state_ |= __constructed;
113 __thread_local_data()->__make_ready_at_thread_exit(this);
118 __assoc_sub_state::set_exception(exception_ptr __p)
120 unique_lock<mutex> __lk(__mut_);
121 #ifndef _LIBCPP_NO_EXCEPTIONS
123 throw future_error(make_error_code(future_errc::promise_already_satisfied));
132 __assoc_sub_state::set_exception_at_thread_exit(exception_ptr __p)
134 unique_lock<mutex> __lk(__mut_);
135 #ifndef _LIBCPP_NO_EXCEPTIONS
137 throw future_error(make_error_code(future_errc::promise_already_satisfied));
140 __thread_local_data()->__make_ready_at_thread_exit(this);
145 __assoc_sub_state::__make_ready()
147 unique_lock<mutex> __lk(__mut_);
154 __assoc_sub_state::copy()
156 unique_lock<mutex> __lk(__mut_);
158 if (__exception_ != nullptr)
159 rethrow_exception(__exception_);
163 __assoc_sub_state::wait()
165 unique_lock<mutex> __lk(__mut_);
170 __assoc_sub_state::__sub_wait(unique_lock<mutex>& __lk)
174 if (__state_ & static_cast<unsigned>(deferred))
176 __state_ &= ~static_cast<unsigned>(deferred);
181 while (!__is_ready())
187 __assoc_sub_state::__execute()
189 #ifndef _LIBCPP_NO_EXCEPTIONS
190 throw future_error(make_error_code(future_errc::no_state));
194 future<void>::future(__assoc_sub_state* __state)
197 #ifndef _LIBCPP_NO_EXCEPTIONS
198 if (__state_->__has_future_attached())
199 throw future_error(make_error_code(future_errc::future_already_retrieved));
201 __state_->__add_shared();
202 __state_->__set_future_attached();
205 future<void>::~future()
208 __state_->__release_shared();
214 unique_ptr<__shared_count, __release_shared_count> __(__state_);
215 __assoc_sub_state* __s = __state_;
220 promise<void>::promise()
221 : __state_(new __assoc_sub_state)
225 promise<void>::~promise()
229 if (!__state_->__has_value() && __state_->use_count() > 1)
230 __state_->set_exception(make_exception_ptr(
231 future_error(make_error_code(future_errc::broken_promise))
233 __state_->__release_shared();
238 promise<void>::get_future()
240 #ifndef _LIBCPP_NO_EXCEPTIONS
241 if (__state_ == nullptr)
242 throw future_error(make_error_code(future_errc::no_state));
244 return future<void>(__state_);
248 promise<void>::set_value()
250 #ifndef _LIBCPP_NO_EXCEPTIONS
251 if (__state_ == nullptr)
252 throw future_error(make_error_code(future_errc::no_state));
254 __state_->set_value();
258 promise<void>::set_exception(exception_ptr __p)
260 #ifndef _LIBCPP_NO_EXCEPTIONS
261 if (__state_ == nullptr)
262 throw future_error(make_error_code(future_errc::no_state));
264 __state_->set_exception(__p);
268 promise<void>::set_value_at_thread_exit()
270 #ifndef _LIBCPP_NO_EXCEPTIONS
271 if (__state_ == nullptr)
272 throw future_error(make_error_code(future_errc::no_state));
274 __state_->set_value_at_thread_exit();
278 promise<void>::set_exception_at_thread_exit(exception_ptr __p)
280 #ifndef _LIBCPP_NO_EXCEPTIONS
281 if (__state_ == nullptr)
282 throw future_error(make_error_code(future_errc::no_state));
284 __state_->set_exception_at_thread_exit(__p);
287 shared_future<void>::~shared_future()
290 __state_->__release_shared();
294 shared_future<void>::operator=(const shared_future& __rhs)
297 __rhs.__state_->__add_shared();
299 __state_->__release_shared();
300 __state_ = __rhs.__state_;
304 _LIBCPP_END_NAMESPACE_STD
306 #endif // !_LIBCPP_HAS_NO_THREADS