blob: 4c59f89e56a8eabbe0feca4fedb20a3cb4dc7e08 [file] [log] [blame]
Howard Hinnant96803d92010-08-25 17:32:05 +00001//===------------------------- future.cpp ---------------------------------===//
2//
Chandler Carruthd2012102019-01-19 10:56:40 +00003// 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
Howard Hinnant96803d92010-08-25 17:32:05 +00006//
7//===----------------------------------------------------------------------===//
8
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +00009#include "__config"
10
11#ifndef _LIBCPP_HAS_NO_THREADS
12
Howard Hinnant96803d92010-08-25 17:32:05 +000013#include "future"
14#include "string"
15
16_LIBCPP_BEGIN_NAMESPACE_STD
17
18class _LIBCPP_HIDDEN __future_error_category
19 : public __do_message
20{
21public:
Louis Dionne65358e12021-03-01 12:09:45 -050022 virtual const char* name() const noexcept;
Howard Hinnant96803d92010-08-25 17:32:05 +000023 virtual string message(int ev) const;
24};
25
26const char*
Louis Dionne65358e12021-03-01 12:09:45 -050027__future_error_category::name() const noexcept
Howard Hinnant96803d92010-08-25 17:32:05 +000028{
29 return "future";
30}
31
Logan Chien2929dfd2013-12-14 06:45:09 +000032#if defined(__clang__)
Howard Hinnantf2fd64d2013-09-14 18:20:10 +000033#pragma clang diagnostic push
34#pragma clang diagnostic ignored "-Wswitch"
Logan Chien2929dfd2013-12-14 06:45:09 +000035#elif defined(__GNUC__) || defined(__GNUG__)
36#pragma GCC diagnostic push
37#pragma GCC diagnostic ignored "-Wswitch"
38#endif
Howard Hinnantf2fd64d2013-09-14 18:20:10 +000039
Howard Hinnant96803d92010-08-25 17:32:05 +000040string
41__future_error_category::message(int ev) const
42{
Howard Hinnant5afe2522012-02-02 20:31:36 +000043 switch (static_cast<future_errc>(ev))
Howard Hinnant96803d92010-08-25 17:32:05 +000044 {
Howard Hinnantf2fd64d2013-09-14 18:20:10 +000045 case future_errc(0): // For backwards compatibility with C++11 (LWG 2056)
Howard Hinnant96803d92010-08-25 17:32:05 +000046 case future_errc::broken_promise:
47 return string("The associated promise has been destructed prior "
48 "to the associated state becoming ready.");
49 case future_errc::future_already_retrieved:
50 return string("The future has already been retrieved from "
51 "the promise or packaged_task.");
52 case future_errc::promise_already_satisfied:
53 return string("The state of the promise has already been set.");
54 case future_errc::no_state:
55 return string("Operation not permitted on an object without "
56 "an associated state.");
57 }
58 return string("unspecified future_errc value\n");
59}
60
Logan Chien2929dfd2013-12-14 06:45:09 +000061#if defined(__clang__)
Howard Hinnantf2fd64d2013-09-14 18:20:10 +000062#pragma clang diagnostic pop
Logan Chien2929dfd2013-12-14 06:45:09 +000063#elif defined(__GNUC__) || defined(__GNUG__)
64#pragma GCC diagnostic pop
65#endif
Howard Hinnantf2fd64d2013-09-14 18:20:10 +000066
Howard Hinnant96803d92010-08-25 17:32:05 +000067const error_category&
Louis Dionne65358e12021-03-01 12:09:45 -050068future_category() noexcept
Howard Hinnant96803d92010-08-25 17:32:05 +000069{
70 static __future_error_category __f;
71 return __f;
72}
73
74future_error::future_error(error_code __ec)
75 : logic_error(__ec.message()),
76 __ec_(__ec)
77{
78}
79
Louis Dionne65358e12021-03-01 12:09:45 -050080future_error::~future_error() noexcept
Howard Hinnant5e0c5212011-07-08 00:04:40 +000081{
82}
83
Howard Hinnant3820f6b2010-08-27 20:10:19 +000084void
Louis Dionne65358e12021-03-01 12:09:45 -050085__assoc_sub_state::__on_zero_shared() noexcept
Howard Hinnant3820f6b2010-08-27 20:10:19 +000086{
87 delete this;
88}
89
90void
91__assoc_sub_state::set_value()
92{
93 unique_lock<mutex> __lk(__mut_);
94 if (__has_value())
Marshall Clow9ea94bc2018-07-30 23:33:48 +000095 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant3820f6b2010-08-27 20:10:19 +000096 __state_ |= __constructed | ready;
Howard Hinnant3820f6b2010-08-27 20:10:19 +000097 __cv_.notify_all();
98}
99
100void
101__assoc_sub_state::set_value_at_thread_exit()
102{
103 unique_lock<mutex> __lk(__mut_);
104 if (__has_value())
Marshall Clow9ea94bc2018-07-30 23:33:48 +0000105 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000106 __state_ |= __constructed;
Howard Hinnant15d55052010-10-14 19:18:04 +0000107 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000108}
109
110void
111__assoc_sub_state::set_exception(exception_ptr __p)
112{
113 unique_lock<mutex> __lk(__mut_);
114 if (__has_value())
Marshall Clow9ea94bc2018-07-30 23:33:48 +0000115 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000116 __exception_ = __p;
117 __state_ |= ready;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000118 __cv_.notify_all();
119}
120
121void
122__assoc_sub_state::set_exception_at_thread_exit(exception_ptr __p)
123{
124 unique_lock<mutex> __lk(__mut_);
125 if (__has_value())
Marshall Clow9ea94bc2018-07-30 23:33:48 +0000126 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000127 __exception_ = __p;
Howard Hinnant15d55052010-10-14 19:18:04 +0000128 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000129}
130
131void
132__assoc_sub_state::__make_ready()
133{
134 unique_lock<mutex> __lk(__mut_);
135 __state_ |= ready;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000136 __cv_.notify_all();
137}
138
139void
140__assoc_sub_state::copy()
141{
142 unique_lock<mutex> __lk(__mut_);
Howard Hinnantccdd2032010-08-30 18:46:21 +0000143 __sub_wait(__lk);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000144 if (__exception_ != nullptr)
145 rethrow_exception(__exception_);
146}
147
148void
Howard Hinnantccdd2032010-08-30 18:46:21 +0000149__assoc_sub_state::wait()
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000150{
151 unique_lock<mutex> __lk(__mut_);
Howard Hinnantccdd2032010-08-30 18:46:21 +0000152 __sub_wait(__lk);
153}
154
155void
156__assoc_sub_state::__sub_wait(unique_lock<mutex>& __lk)
157{
158 if (!__is_ready())
159 {
Howard Hinnant28b24882011-12-01 20:21:04 +0000160 if (__state_ & static_cast<unsigned>(deferred))
Howard Hinnantccdd2032010-08-30 18:46:21 +0000161 {
Howard Hinnant28b24882011-12-01 20:21:04 +0000162 __state_ &= ~static_cast<unsigned>(deferred);
Howard Hinnantccdd2032010-08-30 18:46:21 +0000163 __lk.unlock();
164 __execute();
165 }
166 else
167 while (!__is_ready())
168 __cv_.wait(__lk);
169 }
170}
171
172void
173__assoc_sub_state::__execute()
174{
Marshall Clow9ea94bc2018-07-30 23:33:48 +0000175 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000176}
177
178future<void>::future(__assoc_sub_state* __state)
179 : __state_(__state)
180{
Louis Dionne2c0d2262018-08-24 14:00:59 +0000181 __state_->__attach_future();
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000182}
183
184future<void>::~future()
185{
186 if (__state_)
187 __state_->__release_shared();
188}
189
190void
191future<void>::get()
192{
Howard Hinnantccdd2032010-08-30 18:46:21 +0000193 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000194 __assoc_sub_state* __s = __state_;
195 __state_ = nullptr;
Howard Hinnantccdd2032010-08-30 18:46:21 +0000196 __s->copy();
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000197}
198
199promise<void>::promise()
200 : __state_(new __assoc_sub_state)
201{
202}
203
204promise<void>::~promise()
205{
206 if (__state_)
207 {
Asiri Rathnayakeeee23352015-11-10 11:41:22 +0000208#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000209 if (!__state_->__has_value() && __state_->use_count() > 1)
210 __state_->set_exception(make_exception_ptr(
211 future_error(make_error_code(future_errc::broken_promise))
212 ));
Asiri Rathnayakeeee23352015-11-10 11:41:22 +0000213#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000214 __state_->__release_shared();
215 }
216}
217
218future<void>
219promise<void>::get_future()
220{
221 if (__state_ == nullptr)
Marshall Clow9ea94bc2018-07-30 23:33:48 +0000222 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000223 return future<void>(__state_);
224}
225
226void
227promise<void>::set_value()
228{
229 if (__state_ == nullptr)
Marshall Clow9ea94bc2018-07-30 23:33:48 +0000230 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000231 __state_->set_value();
232}
233
234void
235promise<void>::set_exception(exception_ptr __p)
236{
237 if (__state_ == nullptr)
Marshall Clow9ea94bc2018-07-30 23:33:48 +0000238 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000239 __state_->set_exception(__p);
240}
241
242void
243promise<void>::set_value_at_thread_exit()
244{
245 if (__state_ == nullptr)
Marshall Clow9ea94bc2018-07-30 23:33:48 +0000246 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000247 __state_->set_value_at_thread_exit();
248}
249
250void
251promise<void>::set_exception_at_thread_exit(exception_ptr __p)
252{
253 if (__state_ == nullptr)
Marshall Clow9ea94bc2018-07-30 23:33:48 +0000254 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000255 __state_->set_exception_at_thread_exit(__p);
256}
257
Howard Hinnant92646c42010-09-03 18:39:25 +0000258shared_future<void>::~shared_future()
259{
260 if (__state_)
261 __state_->__release_shared();
262}
263
264shared_future<void>&
265shared_future<void>::operator=(const shared_future& __rhs)
266{
267 if (__rhs.__state_)
268 __rhs.__state_->__add_shared();
269 if (__state_)
270 __state_->__release_shared();
271 __state_ = __rhs.__state_;
272 return *this;
273}
274
Howard Hinnant96803d92010-08-25 17:32:05 +0000275_LIBCPP_END_NAMESPACE_STD
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000276
277#endif // !_LIBCPP_HAS_NO_THREADS