]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/libsupc++/atexit_thread.cc
58e611a3b5f7ddc4a3f93ffac953c1c471d5c234
[thirdparty/gcc.git] / libstdc++-v3 / libsupc++ / atexit_thread.cc
1 // Copyright (C) 2012-2025 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.
9
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.
14
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.
18
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/>.
23
24 #include <cxxabi.h>
25 #include <cstdlib>
26 #include <new>
27 #include "bits/gthr.h"
28
29 #ifdef __USING_MCFGTHREAD__
30
31 #include <mcfgthread/cxa.h>
32
33 namespace __cxxabiv1 {
34
35 extern "C" int
36 __cxa_thread_atexit (void (_GLIBCXX_CDTOR_CALLABI *dtor)(void *),
37 void *obj, void *dso_handle) _GLIBCXX_NOTHROW
38 {
39 return __MCF_cxa_thread_atexit (dtor, obj, dso_handle);
40 }
41
42 } // namespace __cxxabiv1
43
44 #else // __USING_MCFGTHREAD__
45
46 #ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
47 #define WIN32_LEAN_AND_MEAN
48 #include <windows.h>
49 #endif
50
51 // Simplify it a little for this file.
52 #ifndef _GLIBCXX_CDTOR_CALLABI
53 # define _GLIBCXX_CDTOR_CALLABI
54 #endif
55
56 #if _GLIBCXX_HAVE___CXA_THREAD_ATEXIT
57
58 // Libc provides __cxa_thread_atexit definition.
59
60 #elif _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL
61
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
68 {
69 return __cxa_thread_atexit_impl (dtor, obj, dso_handle);
70 }
71
72 #else /* _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL */
73
74 namespace {
75 // One element in a singly-linked stack of cleanups.
76 struct elt
77 {
78 void (_GLIBCXX_CDTOR_CALLABI *destructor)(void *);
79 void *object;
80 elt *next;
81 #ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
82 HMODULE dll;
83 #endif
84 };
85
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;
90
91 // Run the specified stack of cleanups.
92 void run (void *p)
93 {
94 elt *e = static_cast<elt*>(p);
95 while (e)
96 {
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);
106 }
107 }
108
109 // Run the stack of cleanups for the current thread.
110 void run ()
111 {
112 void *e;
113 if (__gthread_active_p ())
114 {
115 e = __gthread_getspecific (key);
116 __gthread_setspecific (key, NULL);
117 }
118 else
119 {
120 e = single_thread;
121 single_thread = NULL;
122 }
123 run (e);
124 }
125
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); }
132 };
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);
138 }
139 }
140
141 #if __GXX_WEAK__ && _GLIBCXX_MAY_HAVE___CXA_THREAD_ATEXIT_IMPL
142 extern "C"
143 int __attribute__ ((__weak__))
144 __cxa_thread_atexit_impl (void (_GLIBCXX_CDTOR_CALLABI *func) (void *),
145 void *arg, void *d);
146 #endif
147
148 // ??? We can't make it an ifunc, can we?
149 extern "C" int
150 __cxxabiv1::__cxa_thread_atexit (void (_GLIBCXX_CDTOR_CALLABI *dtor)(void *),
151 void *obj, [[maybe_unused]] void *dso_handle)
152 _GLIBCXX_NOTHROW
153 {
154 #if __GXX_WEAK__ && _GLIBCXX_MAY_HAVE___CXA_THREAD_ATEXIT_IMPL
155 if (__cxa_thread_atexit_impl)
156 // Rely on a (presumably libc-provided) __cxa_thread_atexit_impl,
157 // if it happens to be defined, even if configure couldn't find it
158 // during the build. _GLIBCXX_MAY_HAVE___CXA_THREAD_ATEXIT_IMPL
159 // may be defined e.g. in os_defines.h on platforms where some
160 // versions of libc have a __cxa_thread_atexit_impl definition,
161 // but whose earlier versions didn't. This enables programs build
162 // by toolchains compatible with earlier libc versions to still
163 // benefit from a libc-provided __cxa_thread_atexit_impl.
164 return __cxa_thread_atexit_impl (dtor, obj, dso_handle);
165 #endif
166
167 // Do this initialization once.
168 if (__gthread_active_p ())
169 {
170 // When threads are active use __gthread_once.
171 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
172 __gthread_once (&once, key_init);
173 }
174 else
175 {
176 // And when threads aren't active use a static local guard.
177 static bool queued;
178 if (!queued)
179 {
180 queued = true;
181 std::atexit (run);
182 }
183 }
184
185 elt *first;
186 if (__gthread_active_p ())
187 first = static_cast<elt*>(__gthread_getspecific (key));
188 else
189 first = single_thread;
190
191 elt *new_elt = new (std::nothrow) elt;
192 if (!new_elt)
193 return -1;
194 new_elt->destructor = dtor;
195 new_elt->object = obj;
196 new_elt->next = first;
197 #ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
198 /* Store the DLL address for a later call to FreeLibrary in new_elt and
199 increment DLL load count. This blocks the unloading of the DLL
200 before the thread-local dtors have been called. This does NOT help
201 if FreeLibrary/dlclose is called in excess. */
202 GetModuleHandleExW (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
203 (LPCWSTR) dtor, &new_elt->dll);
204 #endif
205
206 if (__gthread_active_p ())
207 __gthread_setspecific (key, new_elt);
208 else
209 single_thread = new_elt;
210
211 return 0;
212 }
213
214 #endif /* _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL */
215
216 #endif // __USING_MCFGTHREAD__