]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/src/c++11/future.cc
Support exception propagation without lock-free atomic int
[thirdparty/gcc.git] / libstdc++-v3 / src / c++11 / future.cc
1 // future -*- C++ -*-
2
3 // Copyright (C) 2009-2017 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 #include <future>
26
27 namespace
28 {
29 struct future_error_category : public std::error_category
30 {
31 virtual const char*
32 name() const noexcept
33 { return "future"; }
34
35 _GLIBCXX_DEFAULT_ABI_TAG
36 virtual std::string message(int __ec) const
37 {
38 std::string __msg;
39 switch (std::future_errc(__ec))
40 {
41 case std::future_errc::broken_promise:
42 __msg = "Broken promise";
43 break;
44 case std::future_errc::future_already_retrieved:
45 __msg = "Future already retrieved";
46 break;
47 case std::future_errc::promise_already_satisfied:
48 __msg = "Promise already satisfied";
49 break;
50 case std::future_errc::no_state:
51 __msg = "No associated state";
52 break;
53 default:
54 __msg = "Unknown error";
55 break;
56 }
57 return __msg;
58 }
59 };
60
61 const future_error_category&
62 __future_category_instance() noexcept
63 {
64 static const future_error_category __fec{};
65 return __fec;
66 }
67 }
68
69 namespace std _GLIBCXX_VISIBILITY(default)
70 {
71 _GLIBCXX_BEGIN_NAMESPACE_VERSION
72
73 const error_category& future_category() noexcept
74 { return __future_category_instance(); }
75
76 future_error::~future_error() noexcept { }
77
78 const char*
79 future_error::what() const noexcept { return logic_error::what(); }
80
81 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
82 __future_base::_Result_base::_Result_base() = default;
83
84 __future_base::_Result_base::~_Result_base() = default;
85
86 void
87 __future_base::_State_baseV2::_Make_ready::_S_run(void* p)
88 {
89 unique_ptr<_Make_ready> mr{static_cast<_Make_ready*>(p)};
90 if (auto state = mr->_M_shared_state.lock())
91 {
92 // Use release MO to synchronize with observers of the ready state.
93 state->_M_status._M_store_notify_all(_Status::__ready,
94 memory_order_release);
95 }
96 }
97
98 // defined in src/c++11/condition_variable.cc
99 extern void
100 __at_thread_exit(__at_thread_exit_elt* elt);
101
102 void
103 __future_base::_State_baseV2::_Make_ready::_M_set()
104 {
105 _M_cb = &_Make_ready::_S_run;
106 __at_thread_exit(this);
107 }
108 #endif
109
110 _GLIBCXX_END_NAMESPACE_VERSION
111 } // namespace std