gcc50/csu: Skip depends step to avoid possible race
[dragonfly.git] / contrib / gcc-4.4 / libstdc++-v3 / libsupc++ / eh_ptr.cc
1 // -*- C++ -*- Implement the members of exception_ptr.
2 // Copyright (C) 2008, 2009 Free Software Foundation, Inc.
3 //
4 // This file is part of GCC.
5 //
6 // GCC is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 //
11 // GCC 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 <bits/c++config.h>
26
27 #ifdef _GLIBCXX_ATOMIC_BUILTINS_4
28
29 #define _GLIBCXX_EH_PTR_COMPAT
30
31 #include <exception>
32 #include <exception_ptr.h>
33 #include "unwind-cxx.h"
34
35 using namespace __cxxabiv1;
36
37 std::__exception_ptr::exception_ptr::exception_ptr() throw()
38   : _M_exception_object(0)
39 {
40 }
41
42
43 std::__exception_ptr::exception_ptr::exception_ptr(void* obj) throw()
44   : _M_exception_object(obj)
45 {
46   _M_addref();
47 }
48
49
50 std::__exception_ptr::exception_ptr::exception_ptr(__safe_bool) throw()
51   : _M_exception_object(0)
52 {
53 }
54
55
56 std::__exception_ptr::exception_ptr::exception_ptr(
57                         const exception_ptr& other) throw()
58   : _M_exception_object(other._M_exception_object)
59 {
60   _M_addref();
61 }
62
63
64 std::__exception_ptr::exception_ptr::~exception_ptr() throw()
65 {
66   _M_release();
67 }
68
69
70 std::__exception_ptr::exception_ptr&
71 std::__exception_ptr::exception_ptr::operator=(
72                     const exception_ptr& other) throw()
73 {
74   exception_ptr(other).swap(*this);
75   return *this;
76 }
77
78
79 void
80 std::__exception_ptr::exception_ptr::_M_addref() throw()
81 {
82   if (_M_exception_object)
83     {
84       __cxa_refcounted_exception *eh =
85         __get_refcounted_exception_header_from_obj (_M_exception_object);
86       __sync_add_and_fetch (&eh->referenceCount, 1);
87     }
88 }
89
90
91 void
92 std::__exception_ptr::exception_ptr::_M_release() throw()
93 {
94   if (_M_exception_object)
95     {
96       __cxa_refcounted_exception *eh =
97         __get_refcounted_exception_header_from_obj (_M_exception_object);
98       if (__sync_sub_and_fetch (&eh->referenceCount, 1) == 0)
99         {
100           if (eh->exc.exceptionDestructor)
101             eh->exc.exceptionDestructor (_M_exception_object);
102
103           __cxa_free_exception (_M_exception_object);
104           _M_exception_object = 0;
105         }
106     }
107 }
108
109
110 void*
111 std::__exception_ptr::exception_ptr::_M_get() const throw()
112 {
113   return _M_exception_object;
114 }
115
116
117 void
118 std::__exception_ptr::exception_ptr::_M_safe_bool_dummy()
119 {
120 }
121
122
123 void
124 std::__exception_ptr::exception_ptr::swap(exception_ptr &other) throw()
125 {
126   void *tmp = _M_exception_object;
127   _M_exception_object = other._M_exception_object;
128   other._M_exception_object = tmp;
129 }
130
131
132 // Retained for compatibility with CXXABI_1.3.
133 bool
134 std::__exception_ptr::exception_ptr::operator!() const throw()
135 {
136   return _M_exception_object == 0;
137 }
138
139
140 // Retained for compatibility with CXXABI_1.3.
141 std::__exception_ptr::exception_ptr::operator __safe_bool() const throw()
142 {
143   return _M_exception_object ? &exception_ptr::_M_safe_bool_dummy : 0;
144 }
145
146
147 const std::type_info*
148 std::__exception_ptr::exception_ptr::__cxa_exception_type() const throw()
149 {
150   __cxa_exception *eh = __get_exception_header_from_obj (_M_exception_object);
151   return eh->exceptionType;
152 }
153
154
155 bool std::__exception_ptr::operator==(const exception_ptr& lhs,
156                                       const exception_ptr& rhs) throw()
157 {
158   return lhs._M_exception_object == rhs._M_exception_object;
159 }
160
161
162 bool std::__exception_ptr::operator!=(const exception_ptr& lhs,
163                                       const exception_ptr& rhs) throw()
164 {
165   return !(lhs == rhs);
166 }
167
168
169 std::exception_ptr
170 std::current_exception() throw()
171 {
172   __cxa_eh_globals *globals = __cxa_get_globals ();
173   __cxa_exception *header = globals->caughtExceptions;
174
175   if (!header)
176     return std::exception_ptr();
177
178   // Since foreign exceptions can't be counted, we can't return them.
179   if (!__is_gxx_exception_class (header->unwindHeader.exception_class))
180     return std::exception_ptr();
181
182   return std::exception_ptr(
183     __get_object_from_ambiguous_exception (header));
184 }
185
186
187 static void
188 __gxx_dependent_exception_cleanup (_Unwind_Reason_Code code,
189                                    _Unwind_Exception *exc)
190 {
191   // This cleanup is set only for dependents.
192   __cxa_dependent_exception *dep = __get_dependent_exception_from_ue (exc);
193   __cxa_refcounted_exception *header =
194     __get_refcounted_exception_header_from_obj (dep->primaryException);
195
196   // We only want to be called through _Unwind_DeleteException.
197   // _Unwind_DeleteException in the HP-UX IA64 libunwind library
198   // returns _URC_NO_REASON and not _URC_FOREIGN_EXCEPTION_CAUGHT
199   // like the GCC _Unwind_DeleteException function does.
200   if (code != _URC_FOREIGN_EXCEPTION_CAUGHT && code != _URC_NO_REASON)
201     __terminate (header->exc.terminateHandler);
202
203   __cxa_free_dependent_exception (dep);
204
205   if (__sync_sub_and_fetch (&header->referenceCount, 1) == 0)
206     {
207       if (header->exc.exceptionDestructor)
208         header->exc.exceptionDestructor (header + 1);
209
210       __cxa_free_exception (header + 1);
211     }
212 }
213
214
215 void
216 std::rethrow_exception(std::exception_ptr ep)
217 {
218   void *obj = ep._M_get();
219   __cxa_refcounted_exception *eh
220     = __get_refcounted_exception_header_from_obj (obj);
221
222   __cxa_dependent_exception *dep = __cxa_allocate_dependent_exception ();
223   dep->primaryException = obj;
224   __sync_add_and_fetch (&eh->referenceCount, 1);
225
226   dep->unexpectedHandler = __unexpected_handler;
227   dep->terminateHandler = __terminate_handler;
228   __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(dep->unwindHeader.exception_class);
229   dep->unwindHeader.exception_cleanup = __gxx_dependent_exception_cleanup;
230
231 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
232   _Unwind_SjLj_RaiseException (&dep->unwindHeader);
233 #else
234   _Unwind_RaiseException (&dep->unwindHeader);
235 #endif
236
237   // Some sort of unwinding error.  Note that terminate is a handler.
238   __cxa_begin_catch (&dep->unwindHeader);
239   std::terminate ();
240 }
241
242 #undef _GLIBCXX_EH_PTR_COMPAT
243
244 #endif