Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: base/win/dllmain.cc

Issue 8550009: Provide DllMain, and hook to support TLS callbacks. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/base.gyp ('k') | chrome/browser/chrome_browser_main.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 // Windows doesn't support pthread_key_create's destr_function, and in fact
6 // it's a bit tricky to get code to run when a thread exits. This is
7 // cargo-cult magic from http://www.codeproject.com/threads/tls.asp.
8 // We are trying to be compatible with both a LoadLibrary style invocation, as
9 // well as static linking. This code only needs to be included if we use
10 // LoadLibrary, but it hooks into the "standard" set of TLS callbacks that are
11 // provided for static linking.
12
13 // Force a reference to _tls_used to make the linker create the TLS directory
14 // if it's not already there (that is, even if __declspec(thread) is not used).
15 // To make sure we really traverse the whole list, we put down two elements, and
16 // then check that we called both functions when we scanned the list.
17 // Force a reference to p_thread_callback_dllmain_first_entry and
18 // p_thread_callback_dllmain_first_entry to prevent whole program optimization
19 // from discarding the variables.
20
21 #include <windows.h>
22
23 #include "base/logging.h"
24
25 // Indicate if another service is scanning the callbacks.
26 static bool linker_notifications_are_active = false;
27
28 // This will be our mostly no-op callback that we'll list (twice). We won't
29 // deliberately call it, and if it is called, that means we don't need to do any
30 // of the scanning anymore. We expect such a call to arrive via a
31 // THREAD_ATTACH message, long before we'd have to perform our THREAD_DETACH
32 // callback scans.
33 static void NTAPI on_callback(HINSTANCE h, DWORD dwReason, PVOID pv);
cpu_(ooo_6.6-7.5) 2011/12/06 19:15:50 use same notation as thread_local_storage_win.cc
jar (doing other things) 2011/12/06 19:56:26 Done.
34
35 #ifdef _WIN64
36
37 #pragma comment(linker, "/INCLUDE:_tls_used")
38 #pragma comment(linker, "/INCLUDE:p_thread_callback_dllmain_first_entry")
39 #pragma comment(linker, "/INCLUDE:p_thread_callback_dllmain_later_entry")
40
41 #else // _WIN64
42
43 #pragma comment(linker, "/INCLUDE:__tls_used")
44 #pragma comment(linker, "/INCLUDE:_p_thread_callback_dllmain_first_entry")
45 #pragma comment(linker, "/INCLUDE:_p_thread_callback_dllmain_later_entry")
46
47 #endif // _WIN64
48
49 // extern "C" suppresses C++ name mangling so we know the symbol names for the
50 // linker /INCLUDE:symbol pragmas above.
51 extern "C" {
52 typedef void (NTAPI *CallbackPointer)(HINSTANCE h, DWORD dwReason, PVOID pv);
53 // This tells the linker to run these functions.
54 #pragma data_seg(push, old_seg)
55 // Use earliest possible name in the .CRT$XL? list of segments.
56 #pragma data_seg(".CRT$XLA")
57 CallbackPointer p_thread_callback_dllmain_first_entry = on_callback;
cpu_(ooo_6.6-7.5) 2011/12/06 19:15:50 this must be const in x64
jar (doing other things) 2011/12/06 19:56:26 Done.
58 // The latest possible name in the .CRT$XL? (other than *Z, which has a NULL).
59 #pragma data_seg(".CRT$XLY")
60 CallbackPointer p_thread_callback_dllmain_later_entry = on_callback;
cpu_(ooo_6.6-7.5) 2011/12/06 19:15:50 use thread_local_storage_win.cc notation like PIMA
jar (doing other things) 2011/12/06 19:56:26 Done.
61 #pragma data_seg(pop, old_seg)
62 } // extern "C"
63
64
65 // Make DllMain call the listed callbacks. This way any third parties that are
66 // linked in will also be called.
67 BOOL WINAPI DllMain(HINSTANCE h, DWORD reason, PVOID pv) {
68 if (DLL_THREAD_DETACH != reason && DLL_PROCESS_DETACH != reason)
69 return true; // We won't service THREAD_ATTACH calls.
70
71 if (linker_notifications_are_active)
72 return true; // Some other service is doing this work.
73
74 // Use our first entry as a starting point, but don't call it.
75 CallbackPointer* it = &p_thread_callback_dllmain_first_entry;
76 while(*++it) {
77 if (*it == p_thread_callback_dllmain_later_entry)
78 continue; // Don't bother to call our own callbacks.
79 (*it)(h, reason, pv);
80 }
81 // Guarantee that we did a full list, and didn't get "stuck" on some NULL
82 // ahead of time. The fear is that there migtht be another null in ".CRT$XLA"
83 // the way there is guaranteed to be a NULL in ".CRT$XLZ"
84 // We use a CHECK to ensure that the release bulid, no matter how created, is
85 // compliant with this arrangement.
86 CHECK_GT(*it, p_thread_callback_dllmain_later_entry);
87 return true;
88 }
89
90 static void NTAPI on_callback(HINSTANCE h, DWORD dwReason, PVOID pv) {
91 // Do nothing. We were just a place holder in the list used to test that we
92 // call all items.
93 // If we are called, it means that some other system is scanning the callbacks
94 // and we don't need to do so in DllMain().
95 linker_notifications_are_active = true;
96 // Note: If some other routine some how plays this same game... we could both
97 // decide not to do the scanning <sigh>, but this trick should suppress
98 // duplicate calls on Vista, where the runtime takes care of the callbacks,
99 // and allow us to do the callbacks on XP, where we are currently devoid of
100 // callbacks (due to an explicit LoadLibrary call).
101 }
OLDNEW
« no previous file with comments | « base/base.gyp ('k') | chrome/browser/chrome_browser_main.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698