xref: /netbsd-src/external/gpl3/gcc/dist/libstdc++-v3/libsupc++/eh_globals.cc (revision 0a3071956a3a9fdebdbf7f338cf2d439b45fc728)
1 // -*- C++ -*- Manage the thread-local exception globals.
2 // Copyright (C) 2001-2022 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 #include <bits/c++config.h>
26 #include <exception>
27 #include <cstdlib>
28 #include "cxxabi.h"
29 #include "unwind-cxx.h"
30 #include "bits/gthr.h"
31 
32 #if _GLIBCXX_HOSTED
33 using std::free;
34 using std::malloc;
35 #else
36 // In a freestanding environment, these functions may not be
37 // available -- but for now, we assume that they are.
38 extern "C" void *malloc (std::size_t);
39 extern "C" void free(void *);
40 #endif
41 
42 using namespace __cxxabiv1;
43 
44 #if _GLIBCXX_HAVE_TLS
45 
46 namespace
47 {
48   abi::__cxa_eh_globals*
get_global()49   get_global() _GLIBCXX_NOTHROW
50   {
51     static __thread abi::__cxa_eh_globals global;
52     return &global;
53   }
54 } // anonymous namespace
55 
56 extern "C" __cxa_eh_globals*
__cxa_get_globals_fast()57 __cxxabiv1::__cxa_get_globals_fast() _GLIBCXX_NOTHROW
58 { return get_global(); }
59 
60 extern "C" __cxa_eh_globals*
__cxa_get_globals()61 __cxxabiv1::__cxa_get_globals() _GLIBCXX_NOTHROW
62 { return get_global(); }
63 
64 
65 #else
66 
67 #if __has_cpp_attribute(clang::require_constant_initialization)
68 #  define __constinit [[clang::require_constant_initialization]]
69 #endif
70 
71 namespace
72 {
73   // Single-threaded fallback buffer.
74   __constinit __cxa_eh_globals eh_globals;
75 }
76 
77 #if __GTHREADS
78 
79 static void
eh_globals_dtor(void * ptr)80 eh_globals_dtor(void* ptr)
81 {
82   if (ptr)
83     {
84       __cxa_eh_globals* g = reinterpret_cast<__cxa_eh_globals*>(ptr);
85       __cxa_exception* exn = g->caughtExceptions;
86       __cxa_exception* next;
87       while (exn)
88 	{
89 	  next = exn->nextException;
90 	  _Unwind_DeleteException(&exn->unwindHeader);
91 	  exn = next;
92 	}
93       free(ptr);
94     }
95 }
96 
97 struct __eh_globals_init
98 {
99   __gthread_key_t  	_M_key;
100   static bool 		_S_init;
101 
__eh_globals_init__eh_globals_init102   __eh_globals_init()
103   {
104     if (__gthread_active_p())
105       _S_init = __gthread_key_create(&_M_key, eh_globals_dtor) == 0;
106   }
107 
~__eh_globals_init__eh_globals_init108   ~__eh_globals_init()
109   {
110     if (_S_init)
111       {
112 	/* Set it before the call, so that, should
113 	   __gthread_key_delete throw an exception, it won't rely on
114 	   the key being deleted.  */
115 	_S_init = false;
116 	__gthread_key_delete(_M_key);
117       }
118   }
119 
120   __eh_globals_init(const __eh_globals_init&) = delete;
121   __eh_globals_init& operator=(const __eh_globals_init&) = delete;
122 };
123 
124 bool __eh_globals_init::_S_init = false;
125 
126 static __eh_globals_init init;
127 
128 extern "C" __cxa_eh_globals*
__cxa_get_globals_fast()129 __cxxabiv1::__cxa_get_globals_fast() _GLIBCXX_NOTHROW
130 {
131   __cxa_eh_globals* g;
132   if (init._S_init)
133     g = static_cast<__cxa_eh_globals*>(__gthread_getspecific(init._M_key));
134   else
135     g = &eh_globals;
136   return g;
137 }
138 
139 extern "C" __cxa_eh_globals*
__cxa_get_globals()140 __cxxabiv1::__cxa_get_globals() _GLIBCXX_NOTHROW
141 {
142   __cxa_eh_globals* g;
143   if (init._S_init)
144     {
145       g = static_cast<__cxa_eh_globals*>(__gthread_getspecific(init._M_key));
146       if (!g)
147 	{
148 	  void* v = malloc(sizeof(__cxa_eh_globals));
149 	  if (v == 0 || __gthread_setspecific(init._M_key, v) != 0)
150 	    std::terminate();
151 	  g = static_cast<__cxa_eh_globals*>(v);
152 	  g->caughtExceptions = 0;
153 	  g->uncaughtExceptions = 0;
154 #ifdef __ARM_EABI_UNWINDER__
155 	  g->propagatingExceptions = 0;
156 #endif
157 	}
158     }
159   else
160     g = &eh_globals;
161   return g;
162 }
163 
164 #else
165 
166 extern "C" __cxa_eh_globals*
__cxa_get_globals_fast()167 __cxxabiv1::__cxa_get_globals_fast() _GLIBCXX_NOTHROW
168 { return &eh_globals; }
169 
170 extern "C" __cxa_eh_globals*
__cxa_get_globals()171 __cxxabiv1::__cxa_get_globals() _GLIBCXX_NOTHROW
172 { return &eh_globals; }
173 
174 #endif
175 
176 #endif
177