]>
Commit | Line | Data |
---|---|---|
7adcbafe | 1 | // Copyright (C) 2012-2022 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" | |
1ed3ba05 YS |
28 | #ifdef _GLIBCXX_THREAD_ATEXIT_WIN32 |
29 | #define WIN32_LEAN_AND_MEAN | |
30 | #include <windows.h> | |
31 | #endif | |
5b031b9b | 32 | |
7fc0f78c LH |
33 | // Simplify it a little for this file. |
34 | #ifndef _GLIBCXX_CDTOR_CALLABI | |
35 | # define _GLIBCXX_CDTOR_CALLABI | |
36 | #endif | |
37 | ||
2a792efe JW |
38 | #if _GLIBCXX_HAVE___CXA_THREAD_ATEXIT |
39 | ||
40 | // Libc provides __cxa_thread_atexit definition. | |
41 | ||
42 | #elif _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL | |
bed152e3 | 43 | |
7fc0f78c | 44 | extern "C" int __cxa_thread_atexit_impl (void (_GLIBCXX_CDTOR_CALLABI *func) (void *), |
bed152e3 JM |
45 | void *arg, void *d); |
46 | extern "C" int | |
7fc0f78c | 47 | __cxxabiv1::__cxa_thread_atexit (void (_GLIBCXX_CDTOR_CALLABI *dtor)(void *), |
bed152e3 JM |
48 | void *obj, void *dso_handle) |
49 | _GLIBCXX_NOTHROW | |
50 | { | |
51 | return __cxa_thread_atexit_impl (dtor, obj, dso_handle); | |
52 | } | |
53 | ||
a7f930e7 | 54 | #else /* _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL */ |
bed152e3 | 55 | |
5b031b9b | 56 | namespace { |
fe0f6df4 JM |
57 | // One element in a singly-linked stack of cleanups. |
58 | struct elt | |
5b031b9b | 59 | { |
7fc0f78c | 60 | void (_GLIBCXX_CDTOR_CALLABI *destructor)(void *); |
fe0f6df4 JM |
61 | void *object; |
62 | elt *next; | |
1ed3ba05 YS |
63 | #ifdef _GLIBCXX_THREAD_ATEXIT_WIN32 |
64 | HMODULE dll; | |
65 | #endif | |
5b031b9b JM |
66 | }; |
67 | ||
fe0f6df4 JM |
68 | // Keep a per-thread list of cleanups in gthread_key storage. |
69 | __gthread_key_t key; | |
70 | // But also support non-threaded mode. | |
71 | elt *single_thread; | |
5b031b9b | 72 | |
fe0f6df4 JM |
73 | // Run the specified stack of cleanups. |
74 | void run (void *p) | |
5b031b9b | 75 | { |
fe0f6df4 | 76 | elt *e = static_cast<elt*>(p); |
16a1d8fe JM |
77 | while (e) |
78 | { | |
79 | elt *old_e = e; | |
80 | e->destructor (e->object); | |
1ed3ba05 YS |
81 | #ifdef _GLIBCXX_THREAD_ATEXIT_WIN32 |
82 | /* Decrement DLL count */ | |
83 | if (e->dll) | |
84 | FreeLibrary (e->dll); | |
85 | #endif | |
16a1d8fe JM |
86 | e = e->next; |
87 | delete (old_e); | |
88 | } | |
5b031b9b JM |
89 | } |
90 | ||
fe0f6df4 JM |
91 | // Run the stack of cleanups for the current thread. |
92 | void run () | |
5b031b9b | 93 | { |
fe0f6df4 JM |
94 | void *e; |
95 | if (__gthread_active_p ()) | |
16a1d8fe JM |
96 | { |
97 | e = __gthread_getspecific (key); | |
98 | __gthread_setspecific (key, NULL); | |
99 | } | |
fe0f6df4 | 100 | else |
16a1d8fe JM |
101 | { |
102 | e = single_thread; | |
103 | single_thread = NULL; | |
104 | } | |
fe0f6df4 | 105 | run (e); |
5b031b9b JM |
106 | } |
107 | ||
fe0f6df4 JM |
108 | // Initialize the key for the cleanup stack. We use a static local for |
109 | // key init/delete rather than atexit so that delete is run on dlclose. | |
5b031b9b | 110 | void key_init() { |
fe0f6df4 JM |
111 | struct key_s { |
112 | key_s() { __gthread_key_create (&key, run); } | |
113 | ~key_s() { __gthread_key_delete (key); } | |
114 | }; | |
115 | static key_s ks; | |
5b031b9b JM |
116 | // Also make sure the destructors are run by std::exit. |
117 | // FIXME TLS cleanups should run before static cleanups and atexit | |
118 | // cleanups. | |
fe0f6df4 | 119 | std::atexit (run); |
5b031b9b | 120 | } |
fe0f6df4 JM |
121 | } |
122 | ||
123 | extern "C" int | |
7fc0f78c LH |
124 | __cxxabiv1::__cxa_thread_atexit (void (_GLIBCXX_CDTOR_CALLABI *dtor)(void *), |
125 | void *obj, void */*dso_handle*/) | |
fe0f6df4 JM |
126 | _GLIBCXX_NOTHROW |
127 | { | |
128 | // Do this initialization once. | |
129 | if (__gthread_active_p ()) | |
130 | { | |
131 | // When threads are active use __gthread_once. | |
132 | static __gthread_once_t once = __GTHREAD_ONCE_INIT; | |
133 | __gthread_once (&once, key_init); | |
134 | } | |
135 | else | |
5b031b9b | 136 | { |
fe0f6df4 JM |
137 | // And when threads aren't active use a static local guard. |
138 | static bool queued; | |
139 | if (!queued) | |
5b031b9b | 140 | { |
fe0f6df4 JM |
141 | queued = true; |
142 | std::atexit (run); | |
5b031b9b | 143 | } |
5b031b9b | 144 | } |
5b031b9b | 145 | |
fe0f6df4 JM |
146 | elt *first; |
147 | if (__gthread_active_p ()) | |
148 | first = static_cast<elt*>(__gthread_getspecific (key)); | |
149 | else | |
150 | first = single_thread; | |
5b031b9b | 151 | |
fe0f6df4 JM |
152 | elt *new_elt = new (std::nothrow) elt; |
153 | if (!new_elt) | |
154 | return -1; | |
155 | new_elt->destructor = dtor; | |
156 | new_elt->object = obj; | |
157 | new_elt->next = first; | |
1ed3ba05 YS |
158 | #ifdef _GLIBCXX_THREAD_ATEXIT_WIN32 |
159 | /* Store the DLL address for a later call to FreeLibrary in new_elt and | |
160 | increment DLL load count. This blocks the unloading of the DLL | |
161 | before the thread-local dtors have been called. This does NOT help | |
162 | if FreeLibrary/dlclose is called in excess. */ | |
163 | GetModuleHandleExW (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, | |
164 | (LPCWSTR) dtor, &new_elt->dll); | |
165 | #endif | |
fe0f6df4 JM |
166 | |
167 | if (__gthread_active_p ()) | |
168 | __gthread_setspecific (key, new_elt); | |
169 | else | |
170 | single_thread = new_elt; | |
171 | ||
172 | return 0; | |
5b031b9b | 173 | } |
bed152e3 | 174 | |
a7f930e7 | 175 | #endif /* _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL */ |