1 // Copyright (C) 2012-2018 Free Software Foundation, Inc.
3 // This file is part of GCC.
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)
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.
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.
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/>.
27 #include "bits/gthr.h"
28 #ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
29 #define WIN32_LEAN_AND_MEAN
33 #if _GLIBCXX_HAVE___CXA_THREAD_ATEXIT
35 // Libc provides __cxa_thread_atexit definition.
37 #elif _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL
39 extern "C" int __cxa_thread_atexit_impl (void (*func
) (void *),
42 __cxxabiv1::__cxa_thread_atexit (void (*dtor
)(void *),
43 void *obj
, void *dso_handle
)
46 return __cxa_thread_atexit_impl (dtor
, obj
, dso_handle
);
49 #else /* _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL */
52 // One element in a singly-linked stack of cleanups.
55 void (*destructor
)(void *);
58 #ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
63 // Keep a per-thread list of cleanups in gthread_key storage.
65 // But also support non-threaded mode.
68 // Run the specified stack of cleanups.
71 elt
*e
= static_cast<elt
*>(p
);
75 e
->destructor (e
->object
);
76 #ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
77 /* Decrement DLL count */
86 // Run the stack of cleanups for the current thread.
90 if (__gthread_active_p ())
92 e
= __gthread_getspecific (key
);
93 __gthread_setspecific (key
, NULL
);
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.
107 key_s() { __gthread_key_create (&key
, run
); }
108 ~key_s() { __gthread_key_delete (key
); }
111 // Also make sure the destructors are run by std::exit.
112 // FIXME TLS cleanups should run before static cleanups and atexit
119 __cxxabiv1::__cxa_thread_atexit (void (*dtor
)(void *), void *obj
, void */
*dso_handle*/
)
122 // Do this initialization once.
123 if (__gthread_active_p ())
125 // When threads are active use __gthread_once.
126 static __gthread_once_t once
= __GTHREAD_ONCE_INIT
;
127 __gthread_once (&once
, key_init
);
131 // And when threads aren't active use a static local guard.
141 if (__gthread_active_p ())
142 first
= static_cast<elt
*>(__gthread_getspecific (key
));
144 first
= single_thread
;
146 elt
*new_elt
= new (std::nothrow
) elt
;
149 new_elt
->destructor
= dtor
;
150 new_elt
->object
= obj
;
151 new_elt
->next
= first
;
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
);
161 if (__gthread_active_p ())
162 __gthread_setspecific (key
, new_elt
);
164 single_thread
= new_elt
;
169 #endif /* _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL */