thread_local_storage_win.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (c) 2012 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. #include "base/threading/thread_local_storage.h"
  5. #include <windows.h>
  6. #include "base/check.h"
  7. namespace base {
  8. namespace internal {
  9. bool PlatformThreadLocalStorage::AllocTLS(TLSKey* key) {
  10. TLSKey value = TlsAlloc();
  11. if (value != TLS_OUT_OF_INDEXES) {
  12. *key = value;
  13. return true;
  14. }
  15. return false;
  16. }
  17. void PlatformThreadLocalStorage::FreeTLS(TLSKey key) {
  18. BOOL ret = TlsFree(key);
  19. DCHECK(ret);
  20. }
  21. void PlatformThreadLocalStorage::SetTLSValue(TLSKey key, void* value) {
  22. BOOL ret = TlsSetValue(key, value);
  23. DCHECK(ret);
  24. }
  25. } // namespace internal
  26. } // namespace base
  27. // Thread Termination Callbacks.
  28. // Windows doesn't support a per-thread destructor with its
  29. // TLS primitives. So, we build it manually by inserting a
  30. // function to be called on each thread's exit.
  31. // This magic is from http://www.codeproject.com/threads/tls.asp
  32. // and it works for VC++ 7.0 and later.
  33. // Force a reference to _tls_used to make the linker create the TLS directory
  34. // if it's not already there. (e.g. if __declspec(thread) is not used).
  35. // Force a reference to p_thread_callback_base to prevent whole program
  36. // optimization from discarding the variable.
  37. #ifdef _WIN64
  38. #pragma comment(linker, "/INCLUDE:_tls_used")
  39. #pragma comment(linker, "/INCLUDE:p_thread_callback_base")
  40. #else // _WIN64
  41. #pragma comment(linker, "/INCLUDE:__tls_used")
  42. #pragma comment(linker, "/INCLUDE:_p_thread_callback_base")
  43. #endif // _WIN64
  44. // Static callback function to call with each thread termination.
  45. void NTAPI OnThreadExit(PVOID module, DWORD reason, PVOID reserved) {
  46. // On XP SP0 & SP1, the DLL_PROCESS_ATTACH is never seen. It is sent on SP2+
  47. // and on W2K and W2K3. So don't assume it is sent.
  48. if (DLL_THREAD_DETACH == reason || DLL_PROCESS_DETACH == reason)
  49. base::internal::PlatformThreadLocalStorage::OnThreadExit();
  50. }
  51. // .CRT$XLA to .CRT$XLZ is an array of PIMAGE_TLS_CALLBACK pointers that are
  52. // called automatically by the OS loader code (not the CRT) when the module is
  53. // loaded and on thread creation. They are NOT called if the module has been
  54. // loaded by a LoadLibrary() call. It must have implicitly been loaded at
  55. // process startup.
  56. // By implicitly loaded, I mean that it is directly referenced by the main EXE
  57. // or by one of its dependent DLLs. Delay-loaded DLL doesn't count as being
  58. // implicitly loaded.
  59. //
  60. // See VC\crt\src\tlssup.c for reference.
  61. // extern "C" suppresses C++ name mangling so we know the symbol name for the
  62. // linker /INCLUDE:symbol pragma above.
  63. extern "C" {
  64. // The linker must not discard p_thread_callback_base. (We force a reference
  65. // to this variable with a linker /INCLUDE:symbol pragma to ensure that.) If
  66. // this variable is discarded, the OnThreadExit function will never be called.
  67. #ifdef _WIN64
  68. // .CRT section is merged with .rdata on x64 so it must be constant data.
  69. #pragma const_seg(".CRT$XLB")
  70. // When defining a const variable, it must have external linkage to be sure the
  71. // linker doesn't discard it.
  72. extern const PIMAGE_TLS_CALLBACK p_thread_callback_base;
  73. const PIMAGE_TLS_CALLBACK p_thread_callback_base = OnThreadExit;
  74. // Reset the default section.
  75. #pragma const_seg()
  76. #else // _WIN64
  77. #pragma data_seg(".CRT$XLB")
  78. PIMAGE_TLS_CALLBACK p_thread_callback_base = OnThreadExit;
  79. // Reset the default section.
  80. #pragma data_seg()
  81. #endif // _WIN64
  82. } // extern "C"