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

Unified Diff: chrome/app/chrome_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, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: chrome/app/chrome_dllmain.cc
===================================================================
--- chrome/app/chrome_dllmain.cc (revision 0)
+++ chrome/app/chrome_dllmain.cc (revision 0)
@@ -0,0 +1,109 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This code provides Windows callback notification when a thread terminates.
+// It is currently hard-wired to call into base::ThreadLocalStorage, which is
+// currently the only service that needs such notification.
+
+
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.
+
+#include <windows.h>
+
+#include "base/threading/thread_local_storage.h"
+
+// The DllMain() approach works for all recent varieties of Windows, while the
+// alternative approach does not get callbacks on XP. The problem is that
+// Chromium uses an explicit LoadLibrary() call, and that (as noted below)
+// will cause the linker-table based approach to fail.
+#define USE_DLL_MAIN_FOR_THREAD_TERMINATION_NOTIFICATIONS
+
+namespace {
+void OnThreadTermination() {
+ base::ThreadLocalStorage::ThreadExit();
+}
+} // 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.
+
+#if defined(USE_DLL_MAIN_FOR_THREAD_TERMINATION_NOTIFICATIONS)
+// 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.
+// Make DllMain call the listed callbacks.
+BOOL WINAPI DllMain(HINSTANCE h, DWORD reason, PVOID pv) {
+ if (DLL_THREAD_DETACH == reason)
+ OnThreadTermination();
+ return true; // We won't service THREAD_ATTACH etc. calls.
+}
+
+#else // USE_DLL_MAIN_FOR_THREAD_TERMINATION_NOTIFICATIONS
+// This mechanism does not work on XP, but does work on more recent versions of
+// windows.
+
+// Thread Termination Callbacks.
+// Windows doesn't support a per-thread destructor with its
+// TLS primitives. So, we build it manually by inserting a
+// function to be called on each thread's exit.
+// This magic is from http://www.codeproject.com/threads/tls.asp
+// and it works for VC++ 7.0 and later.
+
+// Force a reference to _tls_used to make the linker create the TLS directory
+// if it's not already there. (e.g. if __declspec(thread) is not used).
+// Force a reference to p_thread_callback_base to prevent whole program
+// optimization from discarding the variable.
+#ifdef _WIN64
+
+#pragma comment(linker, "/INCLUDE:_tls_used")
+#pragma comment(linker, "/INCLUDE:p_thread_callback_base")
+
+#else // _WIN64
+
+#pragma comment(linker, "/INCLUDE:__tls_used")
+#pragma comment(linker, "/INCLUDE:_p_thread_callback_base")
+
+#endif // _WIN64
+
+// Static callback function to call with each thread termination.
+void NTAPI OnThreadExit(PVOID module, DWORD reason, PVOID reserved) {
+ if (DLL_THREAD_DETACH == reason)
+ OnThreadTermination();
+}
+
+// .CRT$XLA to .CRT$XLZ is an array of PIMAGE_TLS_CALLBACK pointers that are
+// called automatically by the OS loader code (not the CRT) when the module is
+// loaded and on thread creation. They are NOT called if the module has been
+// loaded by a LoadLibrary() call. It must have implicitly been loaded at
+// process startup.
+// By implicitly loaded, I mean that it is directly referenced by the main EXE
+// or by one of its dependent DLLs. Delay-loaded DLL doesn't count as being
+// implicitly loaded.
+//
+// See VC\crt\src\tlssup.c for reference.
+
+// extern "C" suppresses C++ name mangling so we know the symbol name for the
+// linker /INCLUDE:symbol pragma above.
+extern "C" {
+// The linker must not discard p_thread_callback_base. (We force a reference
+// to this variable with a linker /INCLUDE:symbol pragma to ensure that.) If
+// this variable is discarded, the OnThreadExit function will never be called.
+#ifdef _WIN64
+
+// .CRT section is merged with .rdata on x64 so it must be constant data.
+#pragma const_seg(".CRT$XLB")
+// When defining a const variable, it must have external linkage to be sure the
+// linker doesn't discard it.
+extern const PIMAGE_TLS_CALLBACK p_thread_callback_base;
+const PIMAGE_TLS_CALLBACK p_thread_callback_base = OnThreadExit;
+
+// Reset the default section.
+#pragma const_seg()
+
+#else // _WIN64
+
+#pragma data_seg(".CRT$XLB")
+PIMAGE_TLS_CALLBACK p_thread_callback_base = OnThreadExit;
+
+// Reset the default section.
+#pragma data_seg()
+
+#endif // _WIN64
+} // extern "C"
+
+#endif // USE_DLL_MAIN_FOR_THREAD_TERMINATION_NOTIFICATIONS

Powered by Google App Engine
This is Rietveld 408576698