]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/libsupc++/atexit_thread.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / libsupc++ / atexit_thread.cc
CommitLineData
83ffe9cd 1// Copyright (C) 2012-2023 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
141extern "C" int
7fc0f78c
LH
142__cxxabiv1::__cxa_thread_atexit (void (_GLIBCXX_CDTOR_CALLABI *dtor)(void *),
143 void *obj, void */*dso_handle*/)
fe0f6df4
JM
144 _GLIBCXX_NOTHROW
145{
146 // Do this initialization once.
147 if (__gthread_active_p ())
148 {
149 // When threads are active use __gthread_once.
150 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
151 __gthread_once (&once, key_init);
152 }
153 else
5b031b9b 154 {
fe0f6df4
JM
155 // And when threads aren't active use a static local guard.
156 static bool queued;
157 if (!queued)
5b031b9b 158 {
fe0f6df4
JM
159 queued = true;
160 std::atexit (run);
5b031b9b 161 }
5b031b9b 162 }
5b031b9b 163
fe0f6df4
JM
164 elt *first;
165 if (__gthread_active_p ())
166 first = static_cast<elt*>(__gthread_getspecific (key));
167 else
168 first = single_thread;
5b031b9b 169
fe0f6df4
JM
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;
1ed3ba05
YS
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
fe0f6df4
JM
184
185 if (__gthread_active_p ())
186 __gthread_setspecific (key, new_elt);
187 else
188 single_thread = new_elt;
189
190 return 0;
5b031b9b 191}
bed152e3 192
a7f930e7 193#endif /* _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL */
f036d759
LH
194
195#endif // __USING_MCFGTHREAD__