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.
11//
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.
16//
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.
21
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.
30
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"
37
38#if _GLIBCXX_HOSTED
39using std::free;
40using std::malloc;
41#else
42// In a freestanding environment, these functions may not be
43// available -- but for now, we assume that they are.
44extern "C" void *malloc (std::size_t);
45extern "C" void free(void *);
46#endif
47
48using namespace __cxxabiv1;
49
50#if _GLIBCXX_HAVE_TLS
51
52namespace
53{
54  abi::__cxa_eh_globals*
55  get_global() throw()
56  {
57    static __thread abi::__cxa_eh_globals global;
58    return &global;
59  }
60} // anonymous namespace
61
62extern "C" __cxa_eh_globals*
63__cxxabiv1::__cxa_get_globals_fast() throw()
64{ return get_global(); }
65
66extern "C" __cxa_eh_globals*
67__cxxabiv1::__cxa_get_globals() throw()
68{ return get_global(); }
69
70
71#else
72
73// Single-threaded fallback buffer.
74static __cxa_eh_globals eh_globals;
75
76#if __GTHREADS
77
78static void
79eh_globals_dtor(void* ptr)
80{
81  if (ptr)
82    {
83      __cxa_eh_globals* g = reinterpret_cast<__cxa_eh_globals*>(ptr);
84      __cxa_exception* exn = g->caughtExceptions;
85      __cxa_exception* next;
86      while (exn)
87	{
88	  next = exn->nextException;
89	  _Unwind_DeleteException(&exn->unwindHeader);
90	  exn = next;
91	}
92      free(ptr);
93    }
94}
95
96struct __eh_globals_init
97{
98  __gthread_key_t  	_M_key;
99  bool 			_M_init;
100
101  __eh_globals_init() : _M_init(false)
102  {
103    if (__gthread_active_p())
104      _M_init = __gthread_key_create(&_M_key, eh_globals_dtor) == 0;
105  }
106
107  ~__eh_globals_init()
108  {
109    if (_M_init)
110      __gthread_key_delete(_M_key);
111    _M_init = false;
112  }
113};
114
115static __eh_globals_init init;
116
117extern "C" __cxa_eh_globals*
118__cxxabiv1::__cxa_get_globals_fast() throw()
119{
120  __cxa_eh_globals* g;
121  if (init._M_init)
122    g = static_cast<__cxa_eh_globals*>(__gthread_getspecific(init._M_key));
123  else
124    g = &eh_globals;
125  return g;
126}
127
128extern "C" __cxa_eh_globals*
129__cxxabiv1::__cxa_get_globals() throw()
130{
131  __cxa_eh_globals* g;
132  if (init._M_init)
133    {
134      g = static_cast<__cxa_eh_globals*>(__gthread_getspecific(init._M_key));
135      if (!g)
136	{
137	  void* v = malloc(sizeof(__cxa_eh_globals));
138	  if (v == 0 || __gthread_setspecific(init._M_key, v) != 0)
139	    std::terminate();
140	  g = static_cast<__cxa_eh_globals*>(v);
141	  g->caughtExceptions = 0;
142	  g->uncaughtExceptions = 0;
143	}
144    }
145  else
146    g = &eh_globals;
147  return g;
148}
149
150#else
151
152extern "C" __cxa_eh_globals*
153__cxxabiv1::__cxa_get_globals_fast() throw()
154{ return &eh_globals; }
155
156extern "C" __cxa_eh_globals*
157__cxxabiv1::__cxa_get_globals() throw()
158{ return &eh_globals; }
159
160#endif
161
162#endif
163