]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/libsupc++/atexit_thread.cc
testsuite: Rename a test
[thirdparty/gcc.git] / libstdc++-v3 / libsupc++ / atexit_thread.cc
CommitLineData
6441eb6d 1// Copyright (C) 2012-2025 Free Software Foundation, Inc.
5b031b9b
JM
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"
f036d759
LH
28
29#ifdef __USING_MCFGTHREAD__
30
31#include <mcfgthread/cxa.h>
32
33namespace __cxxabiv1 {
34
35extern "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
1ed3ba05
YS
46#ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
47#define WIN32_LEAN_AND_MEAN
48#include <windows.h>
49#endif
5b031b9b 50
7fc0f78c
LH
51// Simplify it a little for this file.
52#ifndef _GLIBCXX_CDTOR_CALLABI
53# define _GLIBCXX_CDTOR_CALLABI
54#endif
55
2a792efe
JW
56#if _GLIBCXX_HAVE___CXA_THREAD_ATEXIT
57
58// Libc provides __cxa_thread_atexit definition.
59
60#elif _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL
bed152e3 61
7fc0f78c 62extern "C" int __cxa_thread_atexit_impl (void (_GLIBCXX_CDTOR_CALLABI *func) (void *),
bed152e3
JM
63 void *arg, void *d);
64extern "C" int
7fc0f78c 65__cxxabiv1::__cxa_thread_atexit (void (_GLIBCXX_CDTOR_CALLABI *dtor)(void *),
bed152e3
JM
66 void *obj, void *dso_handle)
67 _GLIBCXX_NOTHROW
68{
69 return __cxa_thread_atexit_impl (dtor, obj, dso_handle);
70}
71
a7f930e7 72#else /* _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL */
bed152e3 73
5b031b9b 74namespace {
fe0f6df4
JM
75 // One element in a singly-linked stack of cleanups.
76 struct elt
5b031b9b 77 {
7fc0f78c 78 void (_GLIBCXX_CDTOR_CALLABI *destructor)(void *);
fe0f6df4
JM
79 void *object;
80 elt *next;
1ed3ba05
YS
81#ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
82 HMODULE dll;
83#endif
5b031b9b
JM
84 };
85
fe0f6df4
JM
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;
5b031b9b 90
fe0f6df4
JM
91 // Run the specified stack of cleanups.
92 void run (void *p)
5b031b9b 93 {
fe0f6df4 94 elt *e = static_cast<elt*>(p);
16a1d8fe
JM
95 while (e)
96 {
97 elt *old_e = e;
98 e->destructor (e->object);
1ed3ba05
YS
99#ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
100 /* Decrement DLL count */
101 if (e->dll)
102 FreeLibrary (e->dll);
103#endif
16a1d8fe
JM
104 e = e->next;
105 delete (old_e);
106 }
5b031b9b
JM
107 }
108
fe0f6df4
JM
109 // Run the stack of cleanups for the current thread.
110 void run ()
5b031b9b 111 {
fe0f6df4
JM
112 void *e;
113 if (__gthread_active_p ())
16a1d8fe
JM
114 {
115 e = __gthread_getspecific (key);
116 __gthread_setspecific (key, NULL);
117 }
fe0f6df4 118 else
16a1d8fe
JM
119 {
120 e = single_thread;
121 single_thread = NULL;
122 }
fe0f6df4 123 run (e);
5b031b9b
JM
124 }
125
fe0f6df4
JM
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.
5b031b9b 128 void key_init() {
fe0f6df4
JM
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;
5b031b9b
JM
134 // Also make sure the destructors are run by std::exit.
135 // FIXME TLS cleanups should run before static cleanups and atexit
136 // cleanups.
fe0f6df4 137 std::atexit (run);
5b031b9b 138 }
fe0f6df4
JM
139}
140
3d0f3382
AO
141#if __GXX_WEAK__ && _GLIBCXX_MAY_HAVE___CXA_THREAD_ATEXIT_IMPL
142extern "C"
143int __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?
fe0f6df4 149extern "C" int
7fc0f78c 150__cxxabiv1::__cxa_thread_atexit (void (_GLIBCXX_CDTOR_CALLABI *dtor)(void *),
3d0f3382 151 void *obj, [[maybe_unused]] void *dso_handle)
fe0f6df4
JM
152 _GLIBCXX_NOTHROW
153{
3d0f3382
AO
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
fe0f6df4
JM
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
5b031b9b 175 {
fe0f6df4
JM
176 // And when threads aren't active use a static local guard.
177 static bool queued;
178 if (!queued)
5b031b9b 179 {
fe0f6df4
JM
180 queued = true;
181 std::atexit (run);
5b031b9b 182 }
5b031b9b 183 }
5b031b9b 184
fe0f6df4
JM
185 elt *first;
186 if (__gthread_active_p ())
187 first = static_cast<elt*>(__gthread_getspecific (key));
188 else
189 first = single_thread;
5b031b9b 190
fe0f6df4
JM
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;
1ed3ba05
YS
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
fe0f6df4
JM
205
206 if (__gthread_active_p ())
207 __gthread_setspecific (key, new_elt);
208 else
209 single_thread = new_elt;
210
211 return 0;
5b031b9b 212}
bed152e3 213
a7f930e7 214#endif /* _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL */
f036d759
LH
215
216#endif // __USING_MCFGTHREAD__