OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // This code provides Windows callback notification when a thread terminates. | |
6 // It is currently hard-wired to call into base::ThreadLocalStorage, which is | |
7 // currently the only service that needs such notification. | |
8 | |
9 | |
rvargas (doing something else)
2011/11/30 04:00:23
nit: remove two lines
jar (doing other things)
2011/11/30 07:31:30
Done.
| |
10 | |
11 #include <windows.h> | |
12 | |
13 #include "base/threading/thread_local_storage.h" | |
14 | |
15 // The DllMain() approach works for all recent varieties of Windows, while the | |
16 // alternative approach does not get callbacks on XP. The problem is that | |
17 // Chromium uses an explicit LoadLibrary() call, and that (as noted below) | |
18 // will cause the linker-table based approach to fail. | |
19 #define USE_DLL_MAIN_FOR_THREAD_TERMINATION_NOTIFICATIONS | |
20 | |
21 namespace { | |
22 void OnThreadTermination() { | |
23 base::ThreadLocalStorage::ThreadExit(); | |
24 } | |
25 } // namespace anonymous | |
rvargas (doing something else)
2011/11/30 04:00:23
nit: just "namespace", and add a line before and a
jar (doing other things)
2011/11/30 07:31:30
Done.
| |
26 | |
27 #if defined(USE_DLL_MAIN_FOR_THREAD_TERMINATION_NOTIFICATIONS) | |
28 // This mechanism works on XP, and on more recent versions of Windows. | |
rvargas (doing something else)
2011/11/30 04:00:23
nit: ...works on all versions of Windows
jar (doing other things)
2011/11/30 07:31:30
Done.
| |
29 // Make DllMain call the listed callbacks. | |
30 BOOL WINAPI DllMain(HINSTANCE h, DWORD reason, PVOID pv) { | |
31 if (DLL_THREAD_DETACH == reason) | |
32 OnThreadTermination(); | |
33 return true; // We won't service THREAD_ATTACH etc. calls. | |
34 } | |
35 | |
36 #else // USE_DLL_MAIN_FOR_THREAD_TERMINATION_NOTIFICATIONS | |
37 // This mechanism does not work on XP, but does work on more recent versions of | |
38 // windows. | |
39 | |
40 // Thread Termination Callbacks. | |
41 // Windows doesn't support a per-thread destructor with its | |
42 // TLS primitives. So, we build it manually by inserting a | |
43 // function to be called on each thread's exit. | |
44 // This magic is from http://www.codeproject.com/threads/tls.asp | |
45 // and it works for VC++ 7.0 and later. | |
46 | |
47 // Force a reference to _tls_used to make the linker create the TLS directory | |
48 // if it's not already there. (e.g. if __declspec(thread) is not used). | |
49 // Force a reference to p_thread_callback_base to prevent whole program | |
50 // optimization from discarding the variable. | |
51 #ifdef _WIN64 | |
52 | |
53 #pragma comment(linker, "/INCLUDE:_tls_used") | |
54 #pragma comment(linker, "/INCLUDE:p_thread_callback_base") | |
55 | |
56 #else // _WIN64 | |
57 | |
58 #pragma comment(linker, "/INCLUDE:__tls_used") | |
59 #pragma comment(linker, "/INCLUDE:_p_thread_callback_base") | |
60 | |
61 #endif // _WIN64 | |
62 | |
63 // Static callback function to call with each thread termination. | |
64 void NTAPI OnThreadExit(PVOID module, DWORD reason, PVOID reserved) { | |
65 if (DLL_THREAD_DETACH == reason) | |
66 OnThreadTermination(); | |
67 } | |
68 | |
69 // .CRT$XLA to .CRT$XLZ is an array of PIMAGE_TLS_CALLBACK pointers that are | |
70 // called automatically by the OS loader code (not the CRT) when the module is | |
71 // loaded and on thread creation. They are NOT called if the module has been | |
72 // loaded by a LoadLibrary() call. It must have implicitly been loaded at | |
73 // process startup. | |
74 // By implicitly loaded, I mean that it is directly referenced by the main EXE | |
75 // or by one of its dependent DLLs. Delay-loaded DLL doesn't count as being | |
76 // implicitly loaded. | |
77 // | |
78 // See VC\crt\src\tlssup.c for reference. | |
79 | |
80 // extern "C" suppresses C++ name mangling so we know the symbol name for the | |
81 // linker /INCLUDE:symbol pragma above. | |
82 extern "C" { | |
83 // The linker must not discard p_thread_callback_base. (We force a reference | |
84 // to this variable with a linker /INCLUDE:symbol pragma to ensure that.) If | |
85 // this variable is discarded, the OnThreadExit function will never be called. | |
86 #ifdef _WIN64 | |
87 | |
88 // .CRT section is merged with .rdata on x64 so it must be constant data. | |
89 #pragma const_seg(".CRT$XLB") | |
90 // When defining a const variable, it must have external linkage to be sure the | |
91 // linker doesn't discard it. | |
92 extern const PIMAGE_TLS_CALLBACK p_thread_callback_base; | |
93 const PIMAGE_TLS_CALLBACK p_thread_callback_base = OnThreadExit; | |
94 | |
95 // Reset the default section. | |
96 #pragma const_seg() | |
97 | |
98 #else // _WIN64 | |
99 | |
100 #pragma data_seg(".CRT$XLB") | |
101 PIMAGE_TLS_CALLBACK p_thread_callback_base = OnThreadExit; | |
102 | |
103 // Reset the default section. | |
104 #pragma data_seg() | |
105 | |
106 #endif // _WIN64 | |
107 } // extern "C" | |
108 | |
109 #endif // USE_DLL_MAIN_FOR_THREAD_TERMINATION_NOTIFICATIONS | |
OLD | NEW |