OSDN Git Service

2009-02-03 Paolo Carlini <paolo.carlini@oracle.com>
[pf3gnuchains/gcc-fork.git] / 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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with GCC; see the file COPYING.  If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 #include <bits/c++config.h>
31
32 #ifdef _GLIBCXX_ATOMIC_BUILTINS_4
33
34 #include <exception>
35 #include <exception_ptr.h>
36 #include "unwind-cxx.h"
37
38 using namespace __cxxabiv1;
39
40 std::__exception_ptr::exception_ptr::exception_ptr() throw()
41   : _M_exception_object(0)
42 {
43 }
44
45
46 std::__exception_ptr::exception_ptr::exception_ptr(void* obj) throw()
47   : _M_exception_object(obj)
48 {
49   _M_addref();
50 }
51
52
53 std::__exception_ptr::exception_ptr::exception_ptr(__safe_bool) throw()
54   : _M_exception_object(0)
55 {
56 }
57
58
59 std::__exception_ptr::exception_ptr::exception_ptr(
60                         const exception_ptr& other) throw()
61   : _M_exception_object(other._M_exception_object)
62 {
63   _M_addref();
64 }
65
66
67 std::__exception_ptr::exception_ptr::~exception_ptr() throw()
68 {
69   _M_release();
70 }
71
72
73 std::__exception_ptr::exception_ptr&
74 std::__exception_ptr::exception_ptr::operator=(
75                     const exception_ptr& other) throw()
76 {
77   exception_ptr(other).swap(*this);
78   return *this;
79 }
80
81
82 void
83 std::__exception_ptr::exception_ptr::_M_addref() throw()
84 {
85   if (_M_exception_object)
86     {
87       __cxa_refcounted_exception *eh =
88         __get_refcounted_exception_header_from_obj (_M_exception_object);
89       __sync_add_and_fetch (&eh->referenceCount, 1);
90     }
91 }
92
93
94 void
95 std::__exception_ptr::exception_ptr::_M_release() throw()
96 {
97   if (_M_exception_object)
98     {
99       __cxa_refcounted_exception *eh =
100         __get_refcounted_exception_header_from_obj (_M_exception_object);
101       if (__sync_sub_and_fetch (&eh->referenceCount, 1) == 0)
102         {
103           if (eh->exc.exceptionDestructor)
104             eh->exc.exceptionDestructor (_M_exception_object);
105
106           __cxa_free_exception (_M_exception_object);
107           _M_exception_object = 0;
108         }
109     }
110 }
111
112
113 void*
114 std::__exception_ptr::exception_ptr::_M_get() const throw()
115 {
116   return _M_exception_object;
117 }
118
119
120 void
121 std::__exception_ptr::exception_ptr::_M_safe_bool_dummy()
122 {
123 }
124
125
126 void
127 std::__exception_ptr::exception_ptr::swap(exception_ptr &other) throw()
128 {
129   void *tmp = _M_exception_object;
130   _M_exception_object = other._M_exception_object;
131   other._M_exception_object = tmp;
132 }
133
134
135 bool
136 std::__exception_ptr::exception_ptr::operator!() const throw()
137 {
138   return _M_exception_object == 0;
139 }
140
141
142 std::__exception_ptr::exception_ptr::operator __safe_bool() const throw()
143 {
144   return _M_exception_object ? &exception_ptr::_M_safe_bool_dummy : 0;
145 }
146
147
148 const std::type_info*
149 std::__exception_ptr::exception_ptr::__cxa_exception_type() const throw()
150 {
151   __cxa_exception *eh = __get_exception_header_from_obj (_M_exception_object);
152   return eh->exceptionType;
153 }
154
155
156 bool std::__exception_ptr::operator==(const exception_ptr& lhs,
157                                       const exception_ptr& rhs) throw()
158 {
159   return lhs._M_exception_object == rhs._M_exception_object;
160 }
161
162
163 bool std::__exception_ptr::operator!=(const exception_ptr& lhs,
164                                       const exception_ptr& rhs) throw()
165 {
166   return !(lhs == rhs);
167 }
168
169
170 std::exception_ptr
171 std::current_exception() throw()
172 {
173   __cxa_eh_globals *globals = __cxa_get_globals ();
174   __cxa_exception *header = globals->caughtExceptions;
175
176   if (!header)
177     return std::exception_ptr();
178
179   // Since foreign exceptions can't be counted, we can't return them.
180   if (!__is_gxx_exception_class (header->unwindHeader.exception_class))
181     return std::exception_ptr();
182
183   return std::exception_ptr(
184     __get_object_from_ambiguous_exception (header));
185 }
186
187
188 static void
189 __gxx_dependent_exception_cleanup (_Unwind_Reason_Code code,
190                                    _Unwind_Exception *exc)
191 {
192   // This cleanup is set only for dependents.
193   __cxa_dependent_exception *dep = __get_dependent_exception_from_ue (exc);
194   __cxa_refcounted_exception *header =
195     __get_refcounted_exception_header_from_obj (dep->primaryException);
196
197   // We only want to be called through _Unwind_DeleteException.
198   // _Unwind_DeleteException in the HP-UX IA64 libunwind library
199   // returns _URC_NO_REASON and not _URC_FOREIGN_EXCEPTION_CAUGHT
200   // like the GCC _Unwind_DeleteException function does.
201   if (code != _URC_FOREIGN_EXCEPTION_CAUGHT && code != _URC_NO_REASON)
202     __terminate (header->exc.terminateHandler);
203
204   __cxa_free_dependent_exception (dep);
205
206   if (__sync_sub_and_fetch (&header->referenceCount, 1) == 0)
207     {
208       if (header->exc.exceptionDestructor)
209         header->exc.exceptionDestructor (header + 1);
210
211       __cxa_free_exception (header + 1);
212     }
213 }
214
215
216 void
217 std::rethrow_exception(std::exception_ptr ep)
218 {
219   void *obj = ep._M_get();
220   __cxa_refcounted_exception *eh
221     = __get_refcounted_exception_header_from_obj (obj);
222
223   __cxa_dependent_exception *dep = __cxa_allocate_dependent_exception ();
224   dep->primaryException = obj;
225   __sync_add_and_fetch (&eh->referenceCount, 1);
226
227   dep->unexpectedHandler = __unexpected_handler;
228   dep->terminateHandler = __terminate_handler;
229   __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(dep->unwindHeader.exception_class);
230   dep->unwindHeader.exception_cleanup = __gxx_dependent_exception_cleanup;
231
232 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
233   _Unwind_SjLj_RaiseException (&dep->unwindHeader);
234 #else
235   _Unwind_RaiseException (&dep->unwindHeader);
236 #endif
237
238   // Some sort of unwinding error.  Note that terminate is a handler.
239   __cxa_begin_catch (&dep->unwindHeader);
240   std::terminate ();
241 }
242
243 #endif