1 // -*- C++ -*- Implement the members of exception_ptr.
2 // Copyright (C) 2008, 2009 Free Software Foundation, Inc.
4 // This file is part of GCC.
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)
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.
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.
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.
30 #include <bits/c++config.h>
32 #ifdef _GLIBCXX_ATOMIC_BUILTINS_4
35 #include <exception_ptr.h>
36 #include "unwind-cxx.h"
38 using namespace __cxxabiv1;
40 std::__exception_ptr::exception_ptr::exception_ptr() throw()
41 : _M_exception_object(0)
46 std::__exception_ptr::exception_ptr::exception_ptr(void* obj) throw()
47 : _M_exception_object(obj)
53 std::__exception_ptr::exception_ptr::exception_ptr(__safe_bool) throw()
54 : _M_exception_object(0)
59 std::__exception_ptr::exception_ptr::exception_ptr(
60 const exception_ptr& other) throw()
61 : _M_exception_object(other._M_exception_object)
67 std::__exception_ptr::exception_ptr::~exception_ptr() throw()
73 std::__exception_ptr::exception_ptr&
74 std::__exception_ptr::exception_ptr::operator=(
75 const exception_ptr& other) throw()
77 exception_ptr(other).swap(*this);
83 std::__exception_ptr::exception_ptr::_M_addref() throw()
85 if (_M_exception_object)
87 __cxa_refcounted_exception *eh =
88 __get_refcounted_exception_header_from_obj (_M_exception_object);
89 __sync_add_and_fetch (&eh->referenceCount, 1);
95 std::__exception_ptr::exception_ptr::_M_release() throw()
97 if (_M_exception_object)
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)
103 if (eh->exc.exceptionDestructor)
104 eh->exc.exceptionDestructor (_M_exception_object);
106 __cxa_free_exception (_M_exception_object);
107 _M_exception_object = 0;
114 std::__exception_ptr::exception_ptr::_M_get() const throw()
116 return _M_exception_object;
121 std::__exception_ptr::exception_ptr::_M_safe_bool_dummy()
127 std::__exception_ptr::exception_ptr::swap(exception_ptr &other) throw()
129 void *tmp = _M_exception_object;
130 _M_exception_object = other._M_exception_object;
131 other._M_exception_object = tmp;
136 std::__exception_ptr::exception_ptr::operator!() const throw()
138 return _M_exception_object == 0;
142 std::__exception_ptr::exception_ptr::operator __safe_bool() const throw()
144 return _M_exception_object ? &exception_ptr::_M_safe_bool_dummy : 0;
148 const std::type_info*
149 std::__exception_ptr::exception_ptr::__cxa_exception_type() const throw()
151 __cxa_exception *eh = __get_exception_header_from_obj (_M_exception_object);
152 return eh->exceptionType;
156 bool std::__exception_ptr::operator==(const exception_ptr& lhs,
157 const exception_ptr& rhs) throw()
159 return lhs._M_exception_object == rhs._M_exception_object;
163 bool std::__exception_ptr::operator!=(const exception_ptr& lhs,
164 const exception_ptr& rhs) throw()
166 return !(lhs == rhs);
171 std::current_exception() throw()
173 __cxa_eh_globals *globals = __cxa_get_globals ();
174 __cxa_exception *header = globals->caughtExceptions;
177 return std::exception_ptr();
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();
183 return std::exception_ptr(
184 __get_object_from_ambiguous_exception (header));
189 __gxx_dependent_exception_cleanup (_Unwind_Reason_Code code,
190 _Unwind_Exception *exc)
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);
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);
204 __cxa_free_dependent_exception (dep);
206 if (__sync_sub_and_fetch (&header->referenceCount, 1) == 0)
208 if (header->exc.exceptionDestructor)
209 header->exc.exceptionDestructor (header + 1);
211 __cxa_free_exception (header + 1);
217 std::rethrow_exception(std::exception_ptr ep)
219 void *obj = ep._M_get();
220 __cxa_refcounted_exception *eh
221 = __get_refcounted_exception_header_from_obj (obj);
223 __cxa_dependent_exception *dep = __cxa_allocate_dependent_exception ();
224 dep->primaryException = obj;
225 __sync_add_and_fetch (&eh->referenceCount, 1);
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;
232 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
233 _Unwind_SjLj_RaiseException (&dep->unwindHeader);
235 _Unwind_RaiseException (&dep->unwindHeader);
238 // Some sort of unwinding error. Note that terminate is a handler.
239 __cxa_begin_catch (&dep->unwindHeader);