blob: 07e4602f567ff83de13c468fbdbbb74a911d2b22 [file] [log] [blame]
Howard Hinnant96803d92010-08-25 17:32:05 +00001//===------------------------- future.cpp ---------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnantee11c312010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant96803d92010-08-25 17:32:05 +00007//
8//===----------------------------------------------------------------------===//
9
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +000010#include "__config"
11
12#ifndef _LIBCPP_HAS_NO_THREADS
13
Howard Hinnant96803d92010-08-25 17:32:05 +000014#include "future"
15#include "string"
16
17_LIBCPP_BEGIN_NAMESPACE_STD
18
19class _LIBCPP_HIDDEN __future_error_category
20 : public __do_message
21{
22public:
Howard Hinnant1f098bc2011-05-26 19:48:01 +000023 virtual const char* name() const _NOEXCEPT;
Howard Hinnant96803d92010-08-25 17:32:05 +000024 virtual string message(int ev) const;
25};
26
27const char*
Howard Hinnant1f098bc2011-05-26 19:48:01 +000028__future_error_category::name() const _NOEXCEPT
Howard Hinnant96803d92010-08-25 17:32:05 +000029{
30 return "future";
31}
32
Logan Chien2929dfd2013-12-14 06:45:09 +000033#if defined(__clang__)
Howard Hinnantf2fd64d2013-09-14 18:20:10 +000034#pragma clang diagnostic push
35#pragma clang diagnostic ignored "-Wswitch"
Logan Chien2929dfd2013-12-14 06:45:09 +000036#elif defined(__GNUC__) || defined(__GNUG__)
37#pragma GCC diagnostic push
38#pragma GCC diagnostic ignored "-Wswitch"
39#endif
Howard Hinnantf2fd64d2013-09-14 18:20:10 +000040
Howard Hinnant96803d92010-08-25 17:32:05 +000041string
42__future_error_category::message(int ev) const
43{
Howard Hinnant5afe2522012-02-02 20:31:36 +000044 switch (static_cast<future_errc>(ev))
Howard Hinnant96803d92010-08-25 17:32:05 +000045 {
Howard Hinnantf2fd64d2013-09-14 18:20:10 +000046 case future_errc(0): // For backwards compatibility with C++11 (LWG 2056)
Howard Hinnant96803d92010-08-25 17:32:05 +000047 case future_errc::broken_promise:
48 return string("The associated promise has been destructed prior "
49 "to the associated state becoming ready.");
50 case future_errc::future_already_retrieved:
51 return string("The future has already been retrieved from "
52 "the promise or packaged_task.");
53 case future_errc::promise_already_satisfied:
54 return string("The state of the promise has already been set.");
55 case future_errc::no_state:
56 return string("Operation not permitted on an object without "
57 "an associated state.");
58 }
59 return string("unspecified future_errc value\n");
60}
61
Logan Chien2929dfd2013-12-14 06:45:09 +000062#if defined(__clang__)
Howard Hinnantf2fd64d2013-09-14 18:20:10 +000063#pragma clang diagnostic pop
Logan Chien2929dfd2013-12-14 06:45:09 +000064#elif defined(__GNUC__) || defined(__GNUG__)
65#pragma GCC diagnostic pop
66#endif
Howard Hinnantf2fd64d2013-09-14 18:20:10 +000067
Howard Hinnant96803d92010-08-25 17:32:05 +000068const error_category&
Howard Hinnant22448042012-07-21 17:46:55 +000069future_category() _NOEXCEPT
Howard Hinnant96803d92010-08-25 17:32:05 +000070{
71 static __future_error_category __f;
72 return __f;
73}
74
75future_error::future_error(error_code __ec)
76 : logic_error(__ec.message()),
77 __ec_(__ec)
78{
79}
80
Howard Hinnant5e0c5212011-07-08 00:04:40 +000081future_error::~future_error() _NOEXCEPT
82{
83}
84
Howard Hinnant3820f6b2010-08-27 20:10:19 +000085void
Howard Hinnant719bda32011-05-28 14:41:13 +000086__assoc_sub_state::__on_zero_shared() _NOEXCEPT
Howard Hinnant3820f6b2010-08-27 20:10:19 +000087{
88 delete this;
89}
90
91void
92__assoc_sub_state::set_value()
93{
94 unique_lock<mutex> __lk(__mut_);
95 if (__has_value())
Marshall Clow9ea94bc2018-07-30 23:33:48 +000096 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant3820f6b2010-08-27 20:10:19 +000097 __state_ |= __constructed | ready;
Howard Hinnant3820f6b2010-08-27 20:10:19 +000098 __cv_.notify_all();
99}
100
101void
102__assoc_sub_state::set_value_at_thread_exit()
103{
104 unique_lock<mutex> __lk(__mut_);
105 if (__has_value())
Marshall Clow9ea94bc2018-07-30 23:33:48 +0000106 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000107 __state_ |= __constructed;
Howard Hinnant15d55052010-10-14 19:18:04 +0000108 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000109}
110
111void
112__assoc_sub_state::set_exception(exception_ptr __p)
113{
114 unique_lock<mutex> __lk(__mut_);
115 if (__has_value())
Marshall Clow9ea94bc2018-07-30 23:33:48 +0000116 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000117 __exception_ = __p;
118 __state_ |= ready;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000119 __cv_.notify_all();
120}
121
122void
123__assoc_sub_state::set_exception_at_thread_exit(exception_ptr __p)
124{
125 unique_lock<mutex> __lk(__mut_);
126 if (__has_value())
Marshall Clow9ea94bc2018-07-30 23:33:48 +0000127 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000128 __exception_ = __p;
Howard Hinnant15d55052010-10-14 19:18:04 +0000129 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000130}
131
132void
133__assoc_sub_state::__make_ready()
134{
135 unique_lock<mutex> __lk(__mut_);
136 __state_ |= ready;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000137 __cv_.notify_all();
138}
139
140void
141__assoc_sub_state::copy()
142{
143 unique_lock<mutex> __lk(__mut_);
Howard Hinnantccdd2032010-08-30 18:46:21 +0000144 __sub_wait(__lk);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000145 if (__exception_ != nullptr)
146 rethrow_exception(__exception_);
147}
148
149void
Howard Hinnantccdd2032010-08-30 18:46:21 +0000150__assoc_sub_state::wait()
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000151{
152 unique_lock<mutex> __lk(__mut_);
Howard Hinnantccdd2032010-08-30 18:46:21 +0000153 __sub_wait(__lk);
154}
155
156void
157__assoc_sub_state::__sub_wait(unique_lock<mutex>& __lk)
158{
159 if (!__is_ready())
160 {
Howard Hinnant28b24882011-12-01 20:21:04 +0000161 if (__state_ & static_cast<unsigned>(deferred))
Howard Hinnantccdd2032010-08-30 18:46:21 +0000162 {
Howard Hinnant28b24882011-12-01 20:21:04 +0000163 __state_ &= ~static_cast<unsigned>(deferred);
Howard Hinnantccdd2032010-08-30 18:46:21 +0000164 __lk.unlock();
165 __execute();
166 }
167 else
168 while (!__is_ready())
169 __cv_.wait(__lk);
170 }
171}
172
173void
174__assoc_sub_state::__execute()
175{
Marshall Clow9ea94bc2018-07-30 23:33:48 +0000176 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000177}
178
179future<void>::future(__assoc_sub_state* __state)
180 : __state_(__state)
181{
182 if (__state_->__has_future_attached())
Marshall Clow9ea94bc2018-07-30 23:33:48 +0000183 __throw_future_error(future_errc::future_already_retrieved);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000184 __state_->__add_shared();
Howard Hinnantccdd2032010-08-30 18:46:21 +0000185 __state_->__set_future_attached();
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000186}
187
188future<void>::~future()
189{
190 if (__state_)
191 __state_->__release_shared();
192}
193
194void
195future<void>::get()
196{
Howard Hinnantccdd2032010-08-30 18:46:21 +0000197 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000198 __assoc_sub_state* __s = __state_;
199 __state_ = nullptr;
Howard Hinnantccdd2032010-08-30 18:46:21 +0000200 __s->copy();
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000201}
202
203promise<void>::promise()
204 : __state_(new __assoc_sub_state)
205{
206}
207
208promise<void>::~promise()
209{
210 if (__state_)
211 {
Asiri Rathnayakeeee23352015-11-10 11:41:22 +0000212#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000213 if (!__state_->__has_value() && __state_->use_count() > 1)
214 __state_->set_exception(make_exception_ptr(
215 future_error(make_error_code(future_errc::broken_promise))
216 ));
Asiri Rathnayakeeee23352015-11-10 11:41:22 +0000217#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000218 __state_->__release_shared();
219 }
220}
221
222future<void>
223promise<void>::get_future()
224{
225 if (__state_ == nullptr)
Marshall Clow9ea94bc2018-07-30 23:33:48 +0000226 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000227 return future<void>(__state_);
228}
229
230void
231promise<void>::set_value()
232{
233 if (__state_ == nullptr)
Marshall Clow9ea94bc2018-07-30 23:33:48 +0000234 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000235 __state_->set_value();
236}
237
238void
239promise<void>::set_exception(exception_ptr __p)
240{
241 if (__state_ == nullptr)
Marshall Clow9ea94bc2018-07-30 23:33:48 +0000242 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000243 __state_->set_exception(__p);
244}
245
246void
247promise<void>::set_value_at_thread_exit()
248{
249 if (__state_ == nullptr)
Marshall Clow9ea94bc2018-07-30 23:33:48 +0000250 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000251 __state_->set_value_at_thread_exit();
252}
253
254void
255promise<void>::set_exception_at_thread_exit(exception_ptr __p)
256{
257 if (__state_ == nullptr)
Marshall Clow9ea94bc2018-07-30 23:33:48 +0000258 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000259 __state_->set_exception_at_thread_exit(__p);
260}
261
Howard Hinnant92646c42010-09-03 18:39:25 +0000262shared_future<void>::~shared_future()
263{
264 if (__state_)
265 __state_->__release_shared();
266}
267
268shared_future<void>&
269shared_future<void>::operator=(const shared_future& __rhs)
270{
271 if (__rhs.__state_)
272 __rhs.__state_->__add_shared();
273 if (__state_)
274 __state_->__release_shared();
275 __state_ = __rhs.__state_;
276 return *this;
277}
278
Howard Hinnant96803d92010-08-25 17:32:05 +0000279_LIBCPP_END_NAMESPACE_STD
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000280
281#endif // !_LIBCPP_HAS_NO_THREADS