annotate libstdc++-v3/libsupc++/atexit_thread.cc @ 143:76e1cf5455ef

add cbc_gc test
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 23 Dec 2018 19:24:05 +0900
parents 84e7813d76e9
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1 // Copyright (C) 2012-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
2 //
kono
parents:
diff changeset
3 // This file is part of GCC.
kono
parents:
diff changeset
4 //
kono
parents:
diff changeset
5 // GCC is free software; you can redistribute it and/or modify
kono
parents:
diff changeset
6 // it under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
7 // the Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
8 // any later version.
kono
parents:
diff changeset
9
kono
parents:
diff changeset
10 // GCC is distributed in the hope that it will be useful,
kono
parents:
diff changeset
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents:
diff changeset
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kono
parents:
diff changeset
13 // GNU General Public License for more details.
kono
parents:
diff changeset
14
kono
parents:
diff changeset
15 // Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
16 // permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
17 // 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
18
kono
parents:
diff changeset
19 // You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
20 // a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
21 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
22 // <http://www.gnu.org/licenses/>.
kono
parents:
diff changeset
23
kono
parents:
diff changeset
24 #include <cxxabi.h>
kono
parents:
diff changeset
25 #include <cstdlib>
kono
parents:
diff changeset
26 #include <new>
kono
parents:
diff changeset
27 #include "bits/gthr.h"
kono
parents:
diff changeset
28 #ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
kono
parents:
diff changeset
29 #define WIN32_LEAN_AND_MEAN
kono
parents:
diff changeset
30 #include <windows.h>
kono
parents:
diff changeset
31 #endif
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 #if _GLIBCXX_HAVE___CXA_THREAD_ATEXIT
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 // Libc provides __cxa_thread_atexit definition.
kono
parents:
diff changeset
36
kono
parents:
diff changeset
37 #elif _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 extern "C" int __cxa_thread_atexit_impl (void (*func) (void *),
kono
parents:
diff changeset
40 void *arg, void *d);
kono
parents:
diff changeset
41 extern "C" int
kono
parents:
diff changeset
42 __cxxabiv1::__cxa_thread_atexit (void (*dtor)(void *),
kono
parents:
diff changeset
43 void *obj, void *dso_handle)
kono
parents:
diff changeset
44 _GLIBCXX_NOTHROW
kono
parents:
diff changeset
45 {
kono
parents:
diff changeset
46 return __cxa_thread_atexit_impl (dtor, obj, dso_handle);
kono
parents:
diff changeset
47 }
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 #else /* _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL */
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 namespace {
kono
parents:
diff changeset
52 // One element in a singly-linked stack of cleanups.
kono
parents:
diff changeset
53 struct elt
kono
parents:
diff changeset
54 {
kono
parents:
diff changeset
55 void (*destructor)(void *);
kono
parents:
diff changeset
56 void *object;
kono
parents:
diff changeset
57 elt *next;
kono
parents:
diff changeset
58 #ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
kono
parents:
diff changeset
59 HMODULE dll;
kono
parents:
diff changeset
60 #endif
kono
parents:
diff changeset
61 };
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 // Keep a per-thread list of cleanups in gthread_key storage.
kono
parents:
diff changeset
64 __gthread_key_t key;
kono
parents:
diff changeset
65 // But also support non-threaded mode.
kono
parents:
diff changeset
66 elt *single_thread;
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 // Run the specified stack of cleanups.
kono
parents:
diff changeset
69 void run (void *p)
kono
parents:
diff changeset
70 {
kono
parents:
diff changeset
71 elt *e = static_cast<elt*>(p);
kono
parents:
diff changeset
72 while (e)
kono
parents:
diff changeset
73 {
kono
parents:
diff changeset
74 elt *old_e = e;
kono
parents:
diff changeset
75 e->destructor (e->object);
kono
parents:
diff changeset
76 #ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
kono
parents:
diff changeset
77 /* Decrement DLL count */
kono
parents:
diff changeset
78 if (e->dll)
kono
parents:
diff changeset
79 FreeLibrary (e->dll);
kono
parents:
diff changeset
80 #endif
kono
parents:
diff changeset
81 e = e->next;
kono
parents:
diff changeset
82 delete (old_e);
kono
parents:
diff changeset
83 }
kono
parents:
diff changeset
84 }
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 // Run the stack of cleanups for the current thread.
kono
parents:
diff changeset
87 void run ()
kono
parents:
diff changeset
88 {
kono
parents:
diff changeset
89 void *e;
kono
parents:
diff changeset
90 if (__gthread_active_p ())
kono
parents:
diff changeset
91 {
kono
parents:
diff changeset
92 e = __gthread_getspecific (key);
kono
parents:
diff changeset
93 __gthread_setspecific (key, NULL);
kono
parents:
diff changeset
94 }
kono
parents:
diff changeset
95 else
kono
parents:
diff changeset
96 {
kono
parents:
diff changeset
97 e = single_thread;
kono
parents:
diff changeset
98 single_thread = NULL;
kono
parents:
diff changeset
99 }
kono
parents:
diff changeset
100 run (e);
kono
parents:
diff changeset
101 }
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 // Initialize the key for the cleanup stack. We use a static local for
kono
parents:
diff changeset
104 // key init/delete rather than atexit so that delete is run on dlclose.
kono
parents:
diff changeset
105 void key_init() {
kono
parents:
diff changeset
106 struct key_s {
kono
parents:
diff changeset
107 key_s() { __gthread_key_create (&key, run); }
kono
parents:
diff changeset
108 ~key_s() { __gthread_key_delete (key); }
kono
parents:
diff changeset
109 };
kono
parents:
diff changeset
110 static key_s ks;
kono
parents:
diff changeset
111 // Also make sure the destructors are run by std::exit.
kono
parents:
diff changeset
112 // FIXME TLS cleanups should run before static cleanups and atexit
kono
parents:
diff changeset
113 // cleanups.
kono
parents:
diff changeset
114 std::atexit (run);
kono
parents:
diff changeset
115 }
kono
parents:
diff changeset
116 }
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 extern "C" int
kono
parents:
diff changeset
119 __cxxabiv1::__cxa_thread_atexit (void (*dtor)(void *), void *obj, void */*dso_handle*/)
kono
parents:
diff changeset
120 _GLIBCXX_NOTHROW
kono
parents:
diff changeset
121 {
kono
parents:
diff changeset
122 // Do this initialization once.
kono
parents:
diff changeset
123 if (__gthread_active_p ())
kono
parents:
diff changeset
124 {
kono
parents:
diff changeset
125 // When threads are active use __gthread_once.
kono
parents:
diff changeset
126 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
kono
parents:
diff changeset
127 __gthread_once (&once, key_init);
kono
parents:
diff changeset
128 }
kono
parents:
diff changeset
129 else
kono
parents:
diff changeset
130 {
kono
parents:
diff changeset
131 // And when threads aren't active use a static local guard.
kono
parents:
diff changeset
132 static bool queued;
kono
parents:
diff changeset
133 if (!queued)
kono
parents:
diff changeset
134 {
kono
parents:
diff changeset
135 queued = true;
kono
parents:
diff changeset
136 std::atexit (run);
kono
parents:
diff changeset
137 }
kono
parents:
diff changeset
138 }
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 elt *first;
kono
parents:
diff changeset
141 if (__gthread_active_p ())
kono
parents:
diff changeset
142 first = static_cast<elt*>(__gthread_getspecific (key));
kono
parents:
diff changeset
143 else
kono
parents:
diff changeset
144 first = single_thread;
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 elt *new_elt = new (std::nothrow) elt;
kono
parents:
diff changeset
147 if (!new_elt)
kono
parents:
diff changeset
148 return -1;
kono
parents:
diff changeset
149 new_elt->destructor = dtor;
kono
parents:
diff changeset
150 new_elt->object = obj;
kono
parents:
diff changeset
151 new_elt->next = first;
kono
parents:
diff changeset
152 #ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
kono
parents:
diff changeset
153 /* Store the DLL address for a later call to FreeLibrary in new_elt and
kono
parents:
diff changeset
154 increment DLL load count. This blocks the unloading of the DLL
kono
parents:
diff changeset
155 before the thread-local dtors have been called. This does NOT help
kono
parents:
diff changeset
156 if FreeLibrary/dlclose is called in excess. */
kono
parents:
diff changeset
157 GetModuleHandleExW (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
kono
parents:
diff changeset
158 (LPCWSTR) dtor, &new_elt->dll);
kono
parents:
diff changeset
159 #endif
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 if (__gthread_active_p ())
kono
parents:
diff changeset
162 __gthread_setspecific (key, new_elt);
kono
parents:
diff changeset
163 else
kono
parents:
diff changeset
164 single_thread = new_elt;
kono
parents:
diff changeset
165
kono
parents:
diff changeset
166 return 0;
kono
parents:
diff changeset
167 }
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 #endif /* _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL */