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 //===----------------------------------------------------------------------===//
12 _LIBCPP_BEGIN_NAMESPACE_STD
14 class _LIBCPP_HIDDEN __future_error_category
18 virtual const char* name() const noexcept
;
19 virtual string
message(int ev
) const;
23 __future_error_category::name() const noexcept
28 _LIBCPP_DIAGNOSTIC_PUSH
29 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wswitch")
30 _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wswitch")
33 __future_error_category::message(int ev
) const
35 switch (static_cast<future_errc
>(ev
))
37 case future_errc(0): // For backwards compatibility with C++11 (LWG 2056)
38 case future_errc::broken_promise
:
39 return string("The associated promise has been destructed prior "
40 "to the associated state becoming ready.");
41 case future_errc::future_already_retrieved
:
42 return string("The future has already been retrieved from "
43 "the promise or packaged_task.");
44 case future_errc::promise_already_satisfied
:
45 return string("The state of the promise has already been set.");
46 case future_errc::no_state
:
47 return string("Operation not permitted on an object without "
48 "an associated state.");
50 return string("unspecified future_errc value\n");
53 _LIBCPP_DIAGNOSTIC_POP
56 future_category() noexcept
58 union AvoidDestroyingFutureCategory
{
59 __future_error_category future_error_category
;
60 constexpr explicit AvoidDestroyingFutureCategory() : future_error_category() {}
61 ~AvoidDestroyingFutureCategory() {}
63 constinit
static AvoidDestroyingFutureCategory helper
;
64 return helper
.future_error_category
;
67 future_error::future_error(error_code __ec
)
68 : logic_error(__ec
.message()),
73 future_error::~future_error() noexcept
78 __assoc_sub_state::__on_zero_shared() noexcept
84 __assoc_sub_state::set_value()
86 unique_lock
<mutex
> __lk(__mut_
);
88 __throw_future_error(future_errc::promise_already_satisfied
);
89 __state_
|= __constructed
| ready
;
94 __assoc_sub_state::set_value_at_thread_exit()
96 unique_lock
<mutex
> __lk(__mut_
);
98 __throw_future_error(future_errc::promise_already_satisfied
);
99 __state_
|= __constructed
;
100 __thread_local_data()->__make_ready_at_thread_exit(this);
104 __assoc_sub_state::set_exception(exception_ptr __p
)
106 unique_lock
<mutex
> __lk(__mut_
);
108 __throw_future_error(future_errc::promise_already_satisfied
);
115 __assoc_sub_state::set_exception_at_thread_exit(exception_ptr __p
)
117 unique_lock
<mutex
> __lk(__mut_
);
119 __throw_future_error(future_errc::promise_already_satisfied
);
121 __thread_local_data()->__make_ready_at_thread_exit(this);
125 __assoc_sub_state::__make_ready()
127 unique_lock
<mutex
> __lk(__mut_
);
133 __assoc_sub_state::copy()
135 unique_lock
<mutex
> __lk(__mut_
);
137 if (__exception_
!= nullptr)
138 rethrow_exception(__exception_
);
142 __assoc_sub_state::wait()
144 unique_lock
<mutex
> __lk(__mut_
);
149 __assoc_sub_state::__sub_wait(unique_lock
<mutex
>& __lk
)
153 if (__state_
& static_cast<unsigned>(deferred
))
155 __state_
&= ~static_cast<unsigned>(deferred
);
160 while (!__is_ready())
166 __assoc_sub_state::__execute()
168 __throw_future_error(future_errc::no_state
);
171 future
<void>::future(__assoc_sub_state
* __state
)
174 __state_
->__attach_future();
177 future
<void>::~future()
180 __state_
->__release_shared();
186 unique_ptr
<__shared_count
, __release_shared_count
> __(__state_
);
187 __assoc_sub_state
* __s
= __state_
;
192 promise
<void>::promise()
193 : __state_(new __assoc_sub_state
)
197 promise
<void>::~promise()
201 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
202 if (!__state_
->__has_value() && __state_
->use_count() > 1)
203 __state_
->set_exception(make_exception_ptr(future_error(future_errc::broken_promise
)));
204 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
205 __state_
->__release_shared();
210 promise
<void>::get_future()
212 if (__state_
== nullptr)
213 __throw_future_error(future_errc::no_state
);
214 return future
<void>(__state_
);
218 promise
<void>::set_value()
220 if (__state_
== nullptr)
221 __throw_future_error(future_errc::no_state
);
222 __state_
->set_value();
226 promise
<void>::set_exception(exception_ptr __p
)
228 if (__state_
== nullptr)
229 __throw_future_error(future_errc::no_state
);
230 __state_
->set_exception(__p
);
234 promise
<void>::set_value_at_thread_exit()
236 if (__state_
== nullptr)
237 __throw_future_error(future_errc::no_state
);
238 __state_
->set_value_at_thread_exit();
242 promise
<void>::set_exception_at_thread_exit(exception_ptr __p
)
244 if (__state_
== nullptr)
245 __throw_future_error(future_errc::no_state
);
246 __state_
->set_exception_at_thread_exit(__p
);
249 shared_future
<void>::~shared_future()
252 __state_
->__release_shared();
256 shared_future
<void>::operator=(const shared_future
& __rhs
)
259 __rhs
.__state_
->__add_shared();
261 __state_
->__release_shared();
262 __state_
= __rhs
.__state_
;
266 _LIBCPP_END_NAMESPACE_STD