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