1 //===----------------------------------------------------------------------===//
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 _LIBCPP_DIAGNOSTIC_PUSH
33 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wswitch")
34 _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wswitch")
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
60 future_category() noexcept
62 static __future_error_category __f
;
66 future_error::future_error(error_code __ec
)
67 : logic_error(__ec
.message()),
72 future_error::~future_error() noexcept
77 __assoc_sub_state::__on_zero_shared() noexcept
83 __assoc_sub_state::set_value()
85 unique_lock
<mutex
> __lk(__mut_
);
87 __throw_future_error(future_errc::promise_already_satisfied
);
88 __state_
|= __constructed
| ready
;
93 __assoc_sub_state::set_value_at_thread_exit()
95 unique_lock
<mutex
> __lk(__mut_
);
97 __throw_future_error(future_errc::promise_already_satisfied
);
98 __state_
|= __constructed
;
99 __thread_local_data()->__make_ready_at_thread_exit(this);
103 __assoc_sub_state::set_exception(exception_ptr __p
)
105 unique_lock
<mutex
> __lk(__mut_
);
107 __throw_future_error(future_errc::promise_already_satisfied
);
114 __assoc_sub_state::set_exception_at_thread_exit(exception_ptr __p
)
116 unique_lock
<mutex
> __lk(__mut_
);
118 __throw_future_error(future_errc::promise_already_satisfied
);
120 __thread_local_data()->__make_ready_at_thread_exit(this);
124 __assoc_sub_state::__make_ready()
126 unique_lock
<mutex
> __lk(__mut_
);
132 __assoc_sub_state::copy()
134 unique_lock
<mutex
> __lk(__mut_
);
136 if (__exception_
!= nullptr)
137 rethrow_exception(__exception_
);
141 __assoc_sub_state::wait()
143 unique_lock
<mutex
> __lk(__mut_
);
148 __assoc_sub_state::__sub_wait(unique_lock
<mutex
>& __lk
)
152 if (__state_
& static_cast<unsigned>(deferred
))
154 __state_
&= ~static_cast<unsigned>(deferred
);
159 while (!__is_ready())
165 __assoc_sub_state::__execute()
167 __throw_future_error(future_errc::no_state
);
170 future
<void>::future(__assoc_sub_state
* __state
)
173 __state_
->__attach_future();
176 future
<void>::~future()
179 __state_
->__release_shared();
185 unique_ptr
<__shared_count
, __release_shared_count
> __(__state_
);
186 __assoc_sub_state
* __s
= __state_
;
191 promise
<void>::promise()
192 : __state_(new __assoc_sub_state
)
196 promise
<void>::~promise()
200 #ifndef _LIBCPP_NO_EXCEPTIONS
201 if (!__state_
->__has_value() && __state_
->use_count() > 1)
202 __state_
->set_exception(make_exception_ptr(
203 future_error(make_error_code(future_errc::broken_promise
))
205 #endif // _LIBCPP_NO_EXCEPTIONS
206 __state_
->__release_shared();
211 promise
<void>::get_future()
213 if (__state_
== nullptr)
214 __throw_future_error(future_errc::no_state
);
215 return future
<void>(__state_
);
219 promise
<void>::set_value()
221 if (__state_
== nullptr)
222 __throw_future_error(future_errc::no_state
);
223 __state_
->set_value();
227 promise
<void>::set_exception(exception_ptr __p
)
229 if (__state_
== nullptr)
230 __throw_future_error(future_errc::no_state
);
231 __state_
->set_exception(__p
);
235 promise
<void>::set_value_at_thread_exit()
237 if (__state_
== nullptr)
238 __throw_future_error(future_errc::no_state
);
239 __state_
->set_value_at_thread_exit();
243 promise
<void>::set_exception_at_thread_exit(exception_ptr __p
)
245 if (__state_
== nullptr)
246 __throw_future_error(future_errc::no_state
);
247 __state_
->set_exception_at_thread_exit(__p
);
250 shared_future
<void>::~shared_future()
253 __state_
->__release_shared();
257 shared_future
<void>::operator=(const shared_future
& __rhs
)
260 __rhs
.__state_
->__add_shared();
262 __state_
->__release_shared();
263 __state_
= __rhs
.__state_
;
267 _LIBCPP_END_NAMESPACE_STD
269 #endif // !_LIBCPP_HAS_NO_THREADS