]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.6/libsupc++/eh_ptr.cc
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.6 / libsupc++ / eh_ptr.cc
1 // -*- C++ -*- Implement the members of exception_ptr.
2 // Copyright (C) 2008, 2009, 2010 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 <bits/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 std::__exception_ptr::exception_ptr::exception_ptr(void* obj) throw()
42 : _M_exception_object(obj)  { _M_addref(); }
43
44
45 std::__exception_ptr::exception_ptr::exception_ptr(__safe_bool) throw()
46 : _M_exception_object(0) { }
47
48
49 std::__exception_ptr::
50 exception_ptr::exception_ptr(const exception_ptr& other) throw()
51 : _M_exception_object(other._M_exception_object)
52 { _M_addref(); }
53
54
55 std::__exception_ptr::exception_ptr::~exception_ptr() throw()
56 { _M_release(); }
57
58
59 std::__exception_ptr::exception_ptr&
60 std::__exception_ptr::
61 exception_ptr::operator=(const exception_ptr& other) throw()
62 {
63   exception_ptr(other).swap(*this);
64   return *this;
65 }
66
67
68 void
69 std::__exception_ptr::exception_ptr::_M_addref() throw()
70 {
71   if (_M_exception_object)
72     {
73       __cxa_refcounted_exception *eh =
74         __get_refcounted_exception_header_from_obj (_M_exception_object);
75       __sync_add_and_fetch (&eh->referenceCount, 1);
76     }
77 }
78
79
80 void
81 std::__exception_ptr::exception_ptr::_M_release() throw()
82 {
83   if (_M_exception_object)
84     {
85       __cxa_refcounted_exception *eh =
86         __get_refcounted_exception_header_from_obj (_M_exception_object);
87       if (__sync_sub_and_fetch (&eh->referenceCount, 1) == 0)
88         {
89           if (eh->exc.exceptionDestructor)
90             eh->exc.exceptionDestructor (_M_exception_object);
91
92           __cxa_free_exception (_M_exception_object);
93           _M_exception_object = 0;
94         }
95     }
96 }
97
98
99 void*
100 std::__exception_ptr::exception_ptr::_M_get() const throw()
101 { return _M_exception_object; }
102
103
104 void
105 std::__exception_ptr::exception_ptr::swap(exception_ptr &other) throw()
106 {
107   void *tmp = _M_exception_object;
108   _M_exception_object = other._M_exception_object;
109   other._M_exception_object = tmp;
110 }
111
112
113 // Retained for compatibility with CXXABI_1.3.
114 void
115 std::__exception_ptr::exception_ptr::_M_safe_bool_dummy() throw () { }
116
117
118 // Retained for compatibility with CXXABI_1.3.
119 bool
120 std::__exception_ptr::exception_ptr::operator!() const throw()
121 { return _M_exception_object == 0; }
122
123
124 // Retained for compatibility with CXXABI_1.3.
125 std::__exception_ptr::exception_ptr::operator __safe_bool() const throw()
126 {
127   return _M_exception_object ? &exception_ptr::_M_safe_bool_dummy : 0;
128 }
129
130
131 const std::type_info*
132 std::__exception_ptr::exception_ptr::__cxa_exception_type() const throw()
133 {
134   __cxa_exception *eh = __get_exception_header_from_obj (_M_exception_object);
135   return eh->exceptionType;
136 }
137
138
139 bool std::__exception_ptr::operator==(const exception_ptr& lhs,
140                                       const exception_ptr& rhs) throw()
141 { return lhs._M_exception_object == rhs._M_exception_object; }
142
143
144 bool std::__exception_ptr::operator!=(const exception_ptr& lhs,
145                                       const exception_ptr& rhs) throw()
146 { return !(lhs == rhs);}
147
148
149 std::exception_ptr
150 std::current_exception() throw()
151 {
152   __cxa_eh_globals *globals = __cxa_get_globals ();
153   __cxa_exception *header = globals->caughtExceptions;
154
155   if (!header)
156     return std::exception_ptr();
157
158   // Since foreign exceptions can't be counted, we can't return them.
159   if (!__is_gxx_exception_class (header->unwindHeader.exception_class))
160     return std::exception_ptr();
161
162   return std::exception_ptr(
163     __get_object_from_ambiguous_exception (header));
164 }
165
166
167 static void
168 __gxx_dependent_exception_cleanup(_Unwind_Reason_Code code,
169                                   _Unwind_Exception *exc)
170 {
171   // This cleanup is set only for dependents.
172   __cxa_dependent_exception *dep = __get_dependent_exception_from_ue (exc);
173   __cxa_refcounted_exception *header =
174     __get_refcounted_exception_header_from_obj (dep->primaryException);
175
176   // We only want to be called through _Unwind_DeleteException.
177   // _Unwind_DeleteException in the HP-UX IA64 libunwind library
178   // returns _URC_NO_REASON and not _URC_FOREIGN_EXCEPTION_CAUGHT
179   // like the GCC _Unwind_DeleteException function does.
180   if (code != _URC_FOREIGN_EXCEPTION_CAUGHT && code != _URC_NO_REASON)
181     __terminate (header->exc.terminateHandler);
182
183   __cxa_free_dependent_exception (dep);
184
185   if (__sync_sub_and_fetch (&header->referenceCount, 1) == 0)
186     {
187       if (header->exc.exceptionDestructor)
188         header->exc.exceptionDestructor (header + 1);
189
190       __cxa_free_exception (header + 1);
191     }
192 }
193
194
195 void
196 std::rethrow_exception(std::exception_ptr ep)
197 {
198   void *obj = ep._M_get();
199   __cxa_refcounted_exception *eh
200     = __get_refcounted_exception_header_from_obj (obj);
201
202   __cxa_dependent_exception *dep = __cxa_allocate_dependent_exception ();
203   dep->primaryException = obj;
204   __sync_add_and_fetch (&eh->referenceCount, 1);
205
206   dep->unexpectedHandler = __unexpected_handler;
207   dep->terminateHandler = __terminate_handler;
208   __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(dep->unwindHeader.exception_class);
209   dep->unwindHeader.exception_cleanup = __gxx_dependent_exception_cleanup;
210
211 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
212   _Unwind_SjLj_RaiseException (&dep->unwindHeader);
213 #else
214   _Unwind_RaiseException (&dep->unwindHeader);
215 #endif
216
217   // Some sort of unwinding error.  Note that terminate is a handler.
218   __cxa_begin_catch (&dep->unwindHeader);
219   std::terminate();
220 }
221
222 #undef _GLIBCXX_EH_PTR_COMPAT
223
224 #endif