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 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()),
77 future_error::~future_error() noexcept
82 __assoc_sub_state::__on_zero_shared() noexcept
88 __assoc_sub_state::set_value()
90 unique_lock
<mutex
> __lk(__mut_
);
92 __throw_future_error(future_errc::promise_already_satisfied
);
93 __state_
|= __constructed
| ready
;
98 __assoc_sub_state::set_value_at_thread_exit()
100 unique_lock
<mutex
> __lk(__mut_
);
102 __throw_future_error(future_errc::promise_already_satisfied
);
103 __state_
|= __constructed
;
104 __thread_local_data()->__make_ready_at_thread_exit(this);
108 __assoc_sub_state::set_exception(exception_ptr __p
)
110 unique_lock
<mutex
> __lk(__mut_
);
112 __throw_future_error(future_errc::promise_already_satisfied
);
119 __assoc_sub_state::set_exception_at_thread_exit(exception_ptr __p
)
121 unique_lock
<mutex
> __lk(__mut_
);
123 __throw_future_error(future_errc::promise_already_satisfied
);
125 __thread_local_data()->__make_ready_at_thread_exit(this);
129 __assoc_sub_state::__make_ready()
131 unique_lock
<mutex
> __lk(__mut_
);
137 __assoc_sub_state::copy()
139 unique_lock
<mutex
> __lk(__mut_
);
141 if (__exception_
!= nullptr)
142 rethrow_exception(__exception_
);
146 __assoc_sub_state::wait()
148 unique_lock
<mutex
> __lk(__mut_
);
153 __assoc_sub_state::__sub_wait(unique_lock
<mutex
>& __lk
)
157 if (__state_
& static_cast<unsigned>(deferred
))
159 __state_
&= ~static_cast<unsigned>(deferred
);
164 while (!__is_ready())
170 __assoc_sub_state::__execute()
172 __throw_future_error(future_errc::no_state
);
175 future
<void>::future(__assoc_sub_state
* __state
)
178 __state_
->__attach_future();
181 future
<void>::~future()
184 __state_
->__release_shared();
190 unique_ptr
<__shared_count
, __release_shared_count
> __(__state_
);
191 __assoc_sub_state
* __s
= __state_
;
196 promise
<void>::promise()
197 : __state_(new __assoc_sub_state
)
201 promise
<void>::~promise()
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();
214 promise
<void>::get_future()
216 if (__state_
== nullptr)
217 __throw_future_error(future_errc::no_state
);
218 return future
<void>(__state_
);
222 promise
<void>::set_value()
224 if (__state_
== nullptr)
225 __throw_future_error(future_errc::no_state
);
226 __state_
->set_value();
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
);
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();
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()
256 __state_
->__release_shared();
260 shared_future
<void>::operator=(const shared_future
& __rhs
)
263 __rhs
.__state_
->__add_shared();
265 __state_
->__release_shared();
266 __state_
= __rhs
.__state_
;
270 _LIBCPP_END_NAMESPACE_STD
272 #endif // !_LIBCPP_HAS_NO_THREADS