Revert "libsupc++: try cxa_thread_atexit_impl at runtime"
[official-gcc.git] / libstdc++-v3 / libsupc++ / atexit_thread.cc
blob9346d50f5dafef90c753d6ca67394516d23a5427
1 // Copyright (C) 2012-2023 Free Software Foundation, Inc.
2 //
3 // This file is part of GCC.
4 //
5 // GCC is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3, or (at your option)
8 // any later version.
10 // GCC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // Under Section 7 of GPL version 3, you are granted additional
16 // permissions described in the GCC Runtime Library Exception, version
17 // 3.1, as published by the Free Software Foundation.
19 // You should have received a copy of the GNU General Public License and
20 // a copy of the GCC Runtime Library Exception along with this program;
21 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
22 // <http://www.gnu.org/licenses/>.
24 #include <cxxabi.h>
25 #include <cstdlib>
26 #include <new>
27 #include "bits/gthr.h"
29 #ifdef __USING_MCFGTHREAD__
31 #include <mcfgthread/cxa.h>
33 namespace __cxxabiv1 {
35 extern "C" int
36 __cxa_thread_atexit (void (_GLIBCXX_CDTOR_CALLABI *dtor)(void *),
37 void *obj, void *dso_handle) _GLIBCXX_NOTHROW
39 return __MCF_cxa_thread_atexit (dtor, obj, dso_handle);
42 } // namespace __cxxabiv1
44 #else // __USING_MCFGTHREAD__
46 #ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
47 #define WIN32_LEAN_AND_MEAN
48 #include <windows.h>
49 #endif
51 // Simplify it a little for this file.
52 #ifndef _GLIBCXX_CDTOR_CALLABI
53 # define _GLIBCXX_CDTOR_CALLABI
54 #endif
56 #if _GLIBCXX_HAVE___CXA_THREAD_ATEXIT
58 // Libc provides __cxa_thread_atexit definition.
60 #elif _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL
62 extern "C" int __cxa_thread_atexit_impl (void (_GLIBCXX_CDTOR_CALLABI *func) (void *),
63 void *arg, void *d);
64 extern "C" int
65 __cxxabiv1::__cxa_thread_atexit (void (_GLIBCXX_CDTOR_CALLABI *dtor)(void *),
66 void *obj, void *dso_handle)
67 _GLIBCXX_NOTHROW
69 return __cxa_thread_atexit_impl (dtor, obj, dso_handle);
72 #else /* _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL */
74 namespace {
75 // One element in a singly-linked stack of cleanups.
76 struct elt
78 void (_GLIBCXX_CDTOR_CALLABI *destructor)(void *);
79 void *object;
80 elt *next;
81 #ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
82 HMODULE dll;
83 #endif
86 // Keep a per-thread list of cleanups in gthread_key storage.
87 __gthread_key_t key;
88 // But also support non-threaded mode.
89 elt *single_thread;
91 // Run the specified stack of cleanups.
92 void run (void *p)
94 elt *e = static_cast<elt*>(p);
95 while (e)
97 elt *old_e = e;
98 e->destructor (e->object);
99 #ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
100 /* Decrement DLL count */
101 if (e->dll)
102 FreeLibrary (e->dll);
103 #endif
104 e = e->next;
105 delete (old_e);
109 // Run the stack of cleanups for the current thread.
110 void run ()
112 void *e;
113 if (__gthread_active_p ())
115 e = __gthread_getspecific (key);
116 __gthread_setspecific (key, NULL);
118 else
120 e = single_thread;
121 single_thread = NULL;
123 run (e);
126 // Initialize the key for the cleanup stack. We use a static local for
127 // key init/delete rather than atexit so that delete is run on dlclose.
128 void key_init() {
129 struct key_s {
130 key_s() { __gthread_key_create (&key, run); }
131 ~key_s() { __gthread_key_delete (key); }
133 static key_s ks;
134 // Also make sure the destructors are run by std::exit.
135 // FIXME TLS cleanups should run before static cleanups and atexit
136 // cleanups.
137 std::atexit (run);
141 extern "C" int
142 __cxxabiv1::__cxa_thread_atexit (void (_GLIBCXX_CDTOR_CALLABI *dtor)(void *),
143 void *obj, void */*dso_handle*/)
144 _GLIBCXX_NOTHROW
146 // Do this initialization once.
147 if (__gthread_active_p ())
149 // When threads are active use __gthread_once.
150 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
151 __gthread_once (&once, key_init);
153 else
155 // And when threads aren't active use a static local guard.
156 static bool queued;
157 if (!queued)
159 queued = true;
160 std::atexit (run);
164 elt *first;
165 if (__gthread_active_p ())
166 first = static_cast<elt*>(__gthread_getspecific (key));
167 else
168 first = single_thread;
170 elt *new_elt = new (std::nothrow) elt;
171 if (!new_elt)
172 return -1;
173 new_elt->destructor = dtor;
174 new_elt->object = obj;
175 new_elt->next = first;
176 #ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
177 /* Store the DLL address for a later call to FreeLibrary in new_elt and
178 increment DLL load count. This blocks the unloading of the DLL
179 before the thread-local dtors have been called. This does NOT help
180 if FreeLibrary/dlclose is called in excess. */
181 GetModuleHandleExW (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
182 (LPCWSTR) dtor, &new_elt->dll);
183 #endif
185 if (__gthread_active_p ())
186 __gthread_setspecific (key, new_elt);
187 else
188 single_thread = new_elt;
190 return 0;
193 #endif /* _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL */
195 #endif // __USING_MCFGTHREAD__