1//===----------------------------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include <future>
10#include <string>
11
12_LIBCPP_BEGIN_NAMESPACE_STD
13_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
14
15class _LIBCPP_HIDDEN __future_error_category : public __do_message {
16public:
17 virtual const char* name() const noexcept;
18 virtual string message(int ev) const;
19};
20
21const char* __future_error_category::name() const noexcept { return "future"; }
22
23_LIBCPP_DIAGNOSTIC_PUSH
24_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wswitch")
25_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wswitch")
26
27string __future_error_category::message(int ev) const {
28 switch (static_cast<future_errc>(ev)) {
29 case future_errc(0): // For backwards compatibility with C++11 (LWG 2056)
30 case future_errc::broken_promise:
31 return string("The associated promise has been destructed prior "
32 "to the associated state becoming ready.");
33 case future_errc::future_already_retrieved:
34 return string("The future has already been retrieved from "
35 "the promise or packaged_task.");
36 case future_errc::promise_already_satisfied:
37 return string("The state of the promise has already been set.");
38 case future_errc::no_state:
39 return string("Operation not permitted on an object without "
40 "an associated state.");
41 }
42 return string("unspecified future_errc value\n");
43}
44
45_LIBCPP_DIAGNOSTIC_POP
46
47const error_category& future_category() noexcept {
48 union AvoidDestroyingFutureCategory {
49 __future_error_category future_error_category;
50 constexpr explicit AvoidDestroyingFutureCategory() : future_error_category() {}
51 ~AvoidDestroyingFutureCategory() {}
52 };
53 // Use no-destroy since this removes the function local static guard variable.
54 _LIBCPP_NO_DESTROY constinit static AvoidDestroyingFutureCategory helper;
55 return helper.future_error_category;
56}
57
58future_error::future_error(error_code __ec) : logic_error(__ec.message()), __ec_(__ec) {}
59
60future_error::~future_error() noexcept {}
61
62void __assoc_sub_state::__on_zero_shared() noexcept { delete this; }
63
64void __assoc_sub_state::set_value() {
65 unique_lock<mutex> __lk(__mut_);
66 if (__has_value())
67 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
68 __state_ |= __constructed | ready;
69 __cv_.notify_all();
70}
71
72void __assoc_sub_state::set_value_at_thread_exit() {
73 unique_lock<mutex> __lk(__mut_);
74 if (__has_value())
75 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
76 __state_ |= __constructed;
77 __thread_local_data()->__make_ready_at_thread_exit(this);
78}
79
80void __assoc_sub_state::set_exception(exception_ptr __p) {
81 unique_lock<mutex> __lk(__mut_);
82 if (__has_value())
83 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
84 __exception_ = __p;
85 __state_ |= ready;
86 __cv_.notify_all();
87}
88
89void __assoc_sub_state::set_exception_at_thread_exit(exception_ptr __p) {
90 unique_lock<mutex> __lk(__mut_);
91 if (__has_value())
92 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
93 __exception_ = __p;
94 __thread_local_data()->__make_ready_at_thread_exit(this);
95}
96
97void __assoc_sub_state::__make_ready() {
98 unique_lock<mutex> __lk(__mut_);
99 __state_ |= ready;
100 __cv_.notify_all();
101}
102
103void __assoc_sub_state::copy() {
104 unique_lock<mutex> __lk(__mut_);
105 __sub_wait(__lk);
106 if (__exception_ != nullptr)
107 rethrow_exception(__exception_);
108}
109
110void __assoc_sub_state::wait() {
111 unique_lock<mutex> __lk(__mut_);
112 __sub_wait(__lk);
113}
114
115void __assoc_sub_state::__sub_wait(unique_lock<mutex>& __lk) {
116 if (!__is_ready()) {
117 if (__state_ & static_cast<unsigned>(deferred)) {
118 __state_ &= ~static_cast<unsigned>(deferred);
119 __lk.unlock();
120 __execute();
121 } else
122 while (!__is_ready())
123 __cv_.wait(__lk);
124 }
125}
126
127void __assoc_sub_state::__execute() { std::__throw_future_error(ev: future_errc::no_state); }
128
129future<void>::future(__assoc_sub_state* __state) : __state_(__state) { __state_->__attach_future(); }
130
131future<void>::~future() {
132 if (__state_)
133 __state_->__release_shared();
134}
135
136void future<void>::get() {
137 unique_ptr<__shared_count, __release_shared_count> __(__state_);
138 __assoc_sub_state* __s = __state_;
139 __state_ = nullptr;
140 __s->copy();
141}
142
143promise<void>::promise() : __state_(new __assoc_sub_state) {}
144
145promise<void>::~promise() {
146 if (__state_) {
147#if _LIBCPP_HAS_EXCEPTIONS
148 if (!__state_->__has_value() && __state_->use_count() > 1)
149 __state_->set_exception(make_exception_ptr(e: future_error(future_errc::broken_promise)));
150#endif // _LIBCPP_HAS_EXCEPTIONS
151 __state_->__release_shared();
152 }
153}
154
155future<void> promise<void>::get_future() {
156 if (__state_ == nullptr)
157 std::__throw_future_error(ev: future_errc::no_state);
158 return future<void>(__state_);
159}
160
161void promise<void>::set_value() {
162 if (__state_ == nullptr)
163 std::__throw_future_error(ev: future_errc::no_state);
164 __state_->set_value();
165}
166
167void promise<void>::set_exception(exception_ptr __p) {
168 if (__state_ == nullptr)
169 std::__throw_future_error(ev: future_errc::no_state);
170 __state_->set_exception(__p);
171}
172
173void promise<void>::set_value_at_thread_exit() {
174 if (__state_ == nullptr)
175 std::__throw_future_error(ev: future_errc::no_state);
176 __state_->set_value_at_thread_exit();
177}
178
179void promise<void>::set_exception_at_thread_exit(exception_ptr __p) {
180 if (__state_ == nullptr)
181 std::__throw_future_error(ev: future_errc::no_state);
182 __state_->set_exception_at_thread_exit(__p);
183}
184
185shared_future<void>::~shared_future() {
186 if (__state_)
187 __state_->__release_shared();
188}
189
190shared_future<void>& shared_future<void>::operator=(const shared_future& __rhs) {
191 if (__rhs.__state_)
192 __rhs.__state_->__add_shared();
193 if (__state_)
194 __state_->__release_shared();
195 __state_ = __rhs.__state_;
196 return *this;
197}
198
199_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
200_LIBCPP_END_NAMESPACE_STD
201