1*404b540aSrobert // -*- C++ -*- Manage the thread-local exception globals.
2*404b540aSrobert // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
3*404b540aSrobert // Free Software Foundation, Inc.
4*404b540aSrobert //
5*404b540aSrobert // This file is part of GCC.
6*404b540aSrobert //
7*404b540aSrobert // GCC is free software; you can redistribute it and/or modify
8*404b540aSrobert // it under the terms of the GNU General Public License as published by
9*404b540aSrobert // the Free Software Foundation; either version 2, or (at your option)
10*404b540aSrobert // any later version.
11*404b540aSrobert //
12*404b540aSrobert // GCC is distributed in the hope that it will be useful,
13*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
14*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*404b540aSrobert // GNU General Public License for more details.
16*404b540aSrobert //
17*404b540aSrobert // You should have received a copy of the GNU General Public License
18*404b540aSrobert // along with GCC; see the file COPYING. If not, write to
19*404b540aSrobert // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20*404b540aSrobert // Boston, MA 02110-1301, USA.
21*404b540aSrobert
22*404b540aSrobert // As a special exception, you may use this file as part of a free software
23*404b540aSrobert // library without restriction. Specifically, if other files instantiate
24*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
25*404b540aSrobert // this file and link it with other files to produce an executable, this
26*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
27*404b540aSrobert // the GNU General Public License. This exception does not however
28*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
29*404b540aSrobert // the GNU General Public License.
30*404b540aSrobert
31*404b540aSrobert #include <bits/c++config.h>
32*404b540aSrobert #include <exception>
33*404b540aSrobert #include <cstdlib>
34*404b540aSrobert #include "cxxabi.h"
35*404b540aSrobert #include "unwind-cxx.h"
36*404b540aSrobert #include "bits/gthr.h"
37*404b540aSrobert
38*404b540aSrobert #if _GLIBCXX_HOSTED
39*404b540aSrobert using std::free;
40*404b540aSrobert using std::malloc;
41*404b540aSrobert #else
42*404b540aSrobert // In a freestanding environment, these functions may not be
43*404b540aSrobert // available -- but for now, we assume that they are.
44*404b540aSrobert extern "C" void *malloc (std::size_t);
45*404b540aSrobert extern "C" void free(void *);
46*404b540aSrobert #endif
47*404b540aSrobert
48*404b540aSrobert using namespace __cxxabiv1;
49*404b540aSrobert
50*404b540aSrobert #if _GLIBCXX_HAVE_TLS
51*404b540aSrobert
52*404b540aSrobert namespace
53*404b540aSrobert {
54*404b540aSrobert abi::__cxa_eh_globals*
get_global()55*404b540aSrobert get_global() throw()
56*404b540aSrobert {
57*404b540aSrobert static __thread abi::__cxa_eh_globals global;
58*404b540aSrobert return &global;
59*404b540aSrobert }
60*404b540aSrobert } // anonymous namespace
61*404b540aSrobert
62*404b540aSrobert extern "C" __cxa_eh_globals*
__cxa_get_globals_fast()63*404b540aSrobert __cxxabiv1::__cxa_get_globals_fast() throw()
64*404b540aSrobert { return get_global(); }
65*404b540aSrobert
66*404b540aSrobert extern "C" __cxa_eh_globals*
__cxa_get_globals()67*404b540aSrobert __cxxabiv1::__cxa_get_globals() throw()
68*404b540aSrobert { return get_global(); }
69*404b540aSrobert
70*404b540aSrobert
71*404b540aSrobert #else
72*404b540aSrobert
73*404b540aSrobert // Single-threaded fallback buffer.
74*404b540aSrobert static __cxa_eh_globals eh_globals;
75*404b540aSrobert
76*404b540aSrobert #if __GTHREADS
77*404b540aSrobert
78*404b540aSrobert static void
eh_globals_dtor(void * ptr)79*404b540aSrobert eh_globals_dtor(void* ptr)
80*404b540aSrobert {
81*404b540aSrobert if (ptr)
82*404b540aSrobert {
83*404b540aSrobert __cxa_eh_globals* g = reinterpret_cast<__cxa_eh_globals*>(ptr);
84*404b540aSrobert __cxa_exception* exn = g->caughtExceptions;
85*404b540aSrobert __cxa_exception* next;
86*404b540aSrobert while (exn)
87*404b540aSrobert {
88*404b540aSrobert next = exn->nextException;
89*404b540aSrobert _Unwind_DeleteException(&exn->unwindHeader);
90*404b540aSrobert exn = next;
91*404b540aSrobert }
92*404b540aSrobert free(ptr);
93*404b540aSrobert }
94*404b540aSrobert }
95*404b540aSrobert
96*404b540aSrobert struct __eh_globals_init
97*404b540aSrobert {
98*404b540aSrobert __gthread_key_t _M_key;
99*404b540aSrobert bool _M_init;
100*404b540aSrobert
__eh_globals_init__eh_globals_init101*404b540aSrobert __eh_globals_init() : _M_init(false)
102*404b540aSrobert {
103*404b540aSrobert if (__gthread_active_p())
104*404b540aSrobert _M_init = __gthread_key_create(&_M_key, eh_globals_dtor) == 0;
105*404b540aSrobert }
106*404b540aSrobert
~__eh_globals_init__eh_globals_init107*404b540aSrobert ~__eh_globals_init()
108*404b540aSrobert {
109*404b540aSrobert if (_M_init)
110*404b540aSrobert __gthread_key_delete(_M_key);
111*404b540aSrobert _M_init = false;
112*404b540aSrobert }
113*404b540aSrobert };
114*404b540aSrobert
115*404b540aSrobert static __eh_globals_init init;
116*404b540aSrobert
117*404b540aSrobert extern "C" __cxa_eh_globals*
__cxa_get_globals_fast()118*404b540aSrobert __cxxabiv1::__cxa_get_globals_fast() throw()
119*404b540aSrobert {
120*404b540aSrobert __cxa_eh_globals* g;
121*404b540aSrobert if (init._M_init)
122*404b540aSrobert g = static_cast<__cxa_eh_globals*>(__gthread_getspecific(init._M_key));
123*404b540aSrobert else
124*404b540aSrobert g = &eh_globals;
125*404b540aSrobert return g;
126*404b540aSrobert }
127*404b540aSrobert
128*404b540aSrobert extern "C" __cxa_eh_globals*
__cxa_get_globals()129*404b540aSrobert __cxxabiv1::__cxa_get_globals() throw()
130*404b540aSrobert {
131*404b540aSrobert __cxa_eh_globals* g;
132*404b540aSrobert if (init._M_init)
133*404b540aSrobert {
134*404b540aSrobert g = static_cast<__cxa_eh_globals*>(__gthread_getspecific(init._M_key));
135*404b540aSrobert if (!g)
136*404b540aSrobert {
137*404b540aSrobert void* v = malloc(sizeof(__cxa_eh_globals));
138*404b540aSrobert if (v == 0 || __gthread_setspecific(init._M_key, v) != 0)
139*404b540aSrobert std::terminate();
140*404b540aSrobert g = static_cast<__cxa_eh_globals*>(v);
141*404b540aSrobert g->caughtExceptions = 0;
142*404b540aSrobert g->uncaughtExceptions = 0;
143*404b540aSrobert }
144*404b540aSrobert }
145*404b540aSrobert else
146*404b540aSrobert g = &eh_globals;
147*404b540aSrobert return g;
148*404b540aSrobert }
149*404b540aSrobert
150*404b540aSrobert #else
151*404b540aSrobert
152*404b540aSrobert extern "C" __cxa_eh_globals*
__cxa_get_globals_fast()153*404b540aSrobert __cxxabiv1::__cxa_get_globals_fast() throw()
154*404b540aSrobert { return &eh_globals; }
155*404b540aSrobert
156*404b540aSrobert extern "C" __cxa_eh_globals*
__cxa_get_globals()157*404b540aSrobert __cxxabiv1::__cxa_get_globals() throw()
158*404b540aSrobert { return &eh_globals; }
159*404b540aSrobert
160*404b540aSrobert #endif
161*404b540aSrobert
162*404b540aSrobert #endif
163