[flang] Fix length handling in character kind implicit conversion (#74586)
[llvm-project.git] / libcxx / src / future.cpp
blob0e3f7b12b859e4bd6571383989c1f354fd431ab3
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 //===----------------------------------------------------------------------===//
9 #include <future>
10 #include <string>
12 _LIBCPP_BEGIN_NAMESPACE_STD
14 class _LIBCPP_HIDDEN __future_error_category
15 : public __do_message
17 public:
18 virtual const char* name() const noexcept;
19 virtual string message(int ev) const;
22 const char*
23 __future_error_category::name() const noexcept
25 return "future";
28 _LIBCPP_DIAGNOSTIC_PUSH
29 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wswitch")
30 _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wswitch")
32 string
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
55 const error_category&
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()),
69 __ec_(__ec)
73 future_error::~future_error() noexcept
77 void
78 __assoc_sub_state::__on_zero_shared() noexcept
80 delete this;
83 void
84 __assoc_sub_state::set_value()
86 unique_lock<mutex> __lk(__mut_);
87 if (__has_value())
88 __throw_future_error(future_errc::promise_already_satisfied);
89 __state_ |= __constructed | ready;
90 __cv_.notify_all();
93 void
94 __assoc_sub_state::set_value_at_thread_exit()
96 unique_lock<mutex> __lk(__mut_);
97 if (__has_value())
98 __throw_future_error(future_errc::promise_already_satisfied);
99 __state_ |= __constructed;
100 __thread_local_data()->__make_ready_at_thread_exit(this);
103 void
104 __assoc_sub_state::set_exception(exception_ptr __p)
106 unique_lock<mutex> __lk(__mut_);
107 if (__has_value())
108 __throw_future_error(future_errc::promise_already_satisfied);
109 __exception_ = __p;
110 __state_ |= ready;
111 __cv_.notify_all();
114 void
115 __assoc_sub_state::set_exception_at_thread_exit(exception_ptr __p)
117 unique_lock<mutex> __lk(__mut_);
118 if (__has_value())
119 __throw_future_error(future_errc::promise_already_satisfied);
120 __exception_ = __p;
121 __thread_local_data()->__make_ready_at_thread_exit(this);
124 void
125 __assoc_sub_state::__make_ready()
127 unique_lock<mutex> __lk(__mut_);
128 __state_ |= ready;
129 __cv_.notify_all();
132 void
133 __assoc_sub_state::copy()
135 unique_lock<mutex> __lk(__mut_);
136 __sub_wait(__lk);
137 if (__exception_ != nullptr)
138 rethrow_exception(__exception_);
141 void
142 __assoc_sub_state::wait()
144 unique_lock<mutex> __lk(__mut_);
145 __sub_wait(__lk);
148 void
149 __assoc_sub_state::__sub_wait(unique_lock<mutex>& __lk)
151 if (!__is_ready())
153 if (__state_ & static_cast<unsigned>(deferred))
155 __state_ &= ~static_cast<unsigned>(deferred);
156 __lk.unlock();
157 __execute();
159 else
160 while (!__is_ready())
161 __cv_.wait(__lk);
165 void
166 __assoc_sub_state::__execute()
168 __throw_future_error(future_errc::no_state);
171 future<void>::future(__assoc_sub_state* __state)
172 : __state_(__state)
174 __state_->__attach_future();
177 future<void>::~future()
179 if (__state_)
180 __state_->__release_shared();
183 void
184 future<void>::get()
186 unique_ptr<__shared_count, __release_shared_count> __(__state_);
187 __assoc_sub_state* __s = __state_;
188 __state_ = nullptr;
189 __s->copy();
192 promise<void>::promise()
193 : __state_(new __assoc_sub_state)
197 promise<void>::~promise()
199 if (__state_)
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();
209 future<void>
210 promise<void>::get_future()
212 if (__state_ == nullptr)
213 __throw_future_error(future_errc::no_state);
214 return future<void>(__state_);
217 void
218 promise<void>::set_value()
220 if (__state_ == nullptr)
221 __throw_future_error(future_errc::no_state);
222 __state_->set_value();
225 void
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);
233 void
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();
241 void
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()
251 if (__state_)
252 __state_->__release_shared();
255 shared_future<void>&
256 shared_future<void>::operator=(const shared_future& __rhs)
258 if (__rhs.__state_)
259 __rhs.__state_->__add_shared();
260 if (__state_)
261 __state_->__release_shared();
262 __state_ = __rhs.__state_;
263 return *this;
266 _LIBCPP_END_NAMESPACE_STD