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