ipc/chromium/src/base/thread_local_storage_win.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/thread_local_storage_win.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,186 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#include "base/thread_local_storage.h"
     1.9 +
    1.10 +#include <windows.h>
    1.11 +
    1.12 +#include "base/logging.h"
    1.13 +
    1.14 +// In order to make TLS destructors work, we need to keep function
    1.15 +// pointers to the destructor for each TLS that we allocate.
    1.16 +// We make this work by allocating a single OS-level TLS, which
    1.17 +// contains an array of slots for the application to use.  In
    1.18 +// parallel, we also allocate an array of destructors, which we
    1.19 +// keep track of and call when threads terminate.
    1.20 +
    1.21 +// tls_key_ is the one native TLS that we use.  It stores our
    1.22 +// table.
    1.23 +long ThreadLocalStorage::tls_key_ = TLS_OUT_OF_INDEXES;
    1.24 +
    1.25 +// tls_max_ is the high-water-mark of allocated thread local storage.
    1.26 +// We intentionally skip 0 so that it is not confused with an
    1.27 +// unallocated TLS slot.
    1.28 +long ThreadLocalStorage::tls_max_ = 1;
    1.29 +
    1.30 +// An array of destructor function pointers for the slots.  If
    1.31 +// a slot has a destructor, it will be stored in its corresponding
    1.32 +// entry in this array.
    1.33 +ThreadLocalStorage::TLSDestructorFunc
    1.34 +  ThreadLocalStorage::tls_destructors_[kThreadLocalStorageSize];
    1.35 +
    1.36 +void** ThreadLocalStorage::Initialize() {
    1.37 +  if (tls_key_ == TLS_OUT_OF_INDEXES) {
    1.38 +    long value = TlsAlloc();
    1.39 +    DCHECK(value != TLS_OUT_OF_INDEXES);
    1.40 +
    1.41 +    // Atomically test-and-set the tls_key.  If the key is TLS_OUT_OF_INDEXES,
    1.42 +    // go ahead and set it.  Otherwise, do nothing, as another
    1.43 +    // thread already did our dirty work.
    1.44 +    if (InterlockedCompareExchange(&tls_key_, value, TLS_OUT_OF_INDEXES) !=
    1.45 +            TLS_OUT_OF_INDEXES) {
    1.46 +      // We've been shortcut. Another thread replaced tls_key_ first so we need
    1.47 +      // to destroy our index and use the one the other thread got first.
    1.48 +      TlsFree(value);
    1.49 +    }
    1.50 +  }
    1.51 +  DCHECK(TlsGetValue(tls_key_) == NULL);
    1.52 +
    1.53 +  // Create an array to store our data.
    1.54 +  void** tls_data = new void*[kThreadLocalStorageSize];
    1.55 +  memset(tls_data, 0, sizeof(void*[kThreadLocalStorageSize]));
    1.56 +  TlsSetValue(tls_key_, tls_data);
    1.57 +  return tls_data;
    1.58 +}
    1.59 +
    1.60 +ThreadLocalStorage::Slot::Slot(TLSDestructorFunc destructor)
    1.61 +    : initialized_(false) {
    1.62 +  Initialize(destructor);
    1.63 +}
    1.64 +
    1.65 +bool ThreadLocalStorage::Slot::Initialize(TLSDestructorFunc destructor) {
    1.66 +  if (tls_key_ == TLS_OUT_OF_INDEXES || !TlsGetValue(tls_key_))
    1.67 +    ThreadLocalStorage::Initialize();
    1.68 +
    1.69 +  // Grab a new slot.
    1.70 +  slot_ = InterlockedIncrement(&tls_max_) - 1;
    1.71 +  if (slot_ >= kThreadLocalStorageSize) {
    1.72 +    NOTREACHED();
    1.73 +    return false;
    1.74 +  }
    1.75 +
    1.76 +  // Setup our destructor.
    1.77 +  tls_destructors_[slot_] = destructor;
    1.78 +  initialized_ = true;
    1.79 +  return true;
    1.80 +}
    1.81 +
    1.82 +void ThreadLocalStorage::Slot::Free() {
    1.83 +  // At this time, we don't reclaim old indices for TLS slots.
    1.84 +  // So all we need to do is wipe the destructor.
    1.85 +  tls_destructors_[slot_] = NULL;
    1.86 +  initialized_ = false;
    1.87 +}
    1.88 +
    1.89 +void* ThreadLocalStorage::Slot::Get() const {
    1.90 +  void** tls_data = static_cast<void**>(TlsGetValue(tls_key_));
    1.91 +  if (!tls_data)
    1.92 +    tls_data = ThreadLocalStorage::Initialize();
    1.93 +  DCHECK(slot_ >= 0 && slot_ < kThreadLocalStorageSize);
    1.94 +  return tls_data[slot_];
    1.95 +}
    1.96 +
    1.97 +void ThreadLocalStorage::Slot::Set(void* value) {
    1.98 +  void** tls_data = static_cast<void**>(TlsGetValue(tls_key_));
    1.99 +  if (!tls_data)
   1.100 +    tls_data = ThreadLocalStorage::Initialize();
   1.101 +  DCHECK(slot_ >= 0 && slot_ < kThreadLocalStorageSize);
   1.102 +  tls_data[slot_] = value;
   1.103 +}
   1.104 +
   1.105 +void ThreadLocalStorage::ThreadExit() {
   1.106 +  if (tls_key_ == TLS_OUT_OF_INDEXES)
   1.107 +    return;
   1.108 +
   1.109 +  void** tls_data = static_cast<void**>(TlsGetValue(tls_key_));
   1.110 +
   1.111 +  // Maybe we have never initialized TLS for this thread.
   1.112 +  if (!tls_data)
   1.113 +    return;
   1.114 +
   1.115 +  for (int slot = 0; slot < tls_max_; slot++) {
   1.116 +    if (tls_destructors_[slot] != NULL) {
   1.117 +      void* value = tls_data[slot];
   1.118 +      tls_destructors_[slot](value);
   1.119 +    }
   1.120 +  }
   1.121 +
   1.122 +  delete[] tls_data;
   1.123 +
   1.124 +  // In case there are other "onexit" handlers...
   1.125 +  TlsSetValue(tls_key_, NULL);
   1.126 +}
   1.127 +
   1.128 +// Thread Termination Callbacks.
   1.129 +// Windows doesn't support a per-thread destructor with its
   1.130 +// TLS primitives.  So, we build it manually by inserting a
   1.131 +// function to be called on each thread's exit.
   1.132 +// This magic is from http://www.codeproject.com/threads/tls.asp
   1.133 +// and it works for VC++ 7.0 and later.
   1.134 +
   1.135 +#ifdef _WIN64
   1.136 +
   1.137 +// This makes the linker create the TLS directory if it's not already
   1.138 +// there.  (e.g. if __declspec(thread) is not used).
   1.139 +#pragma comment(linker, "/INCLUDE:_tls_used")
   1.140 +
   1.141 +#else  // _WIN64
   1.142 +
   1.143 +// This makes the linker create the TLS directory if it's not already
   1.144 +// there.  (e.g. if __declspec(thread) is not used).
   1.145 +#pragma comment(linker, "/INCLUDE:__tls_used")
   1.146 +
   1.147 +#endif  // _WIN64
   1.148 +
   1.149 +// Static callback function to call with each thread termination.
   1.150 +void NTAPI OnThreadExit(PVOID module, DWORD reason, PVOID reserved)
   1.151 +{
   1.152 +  // On XP SP0 & SP1, the DLL_PROCESS_ATTACH is never seen. It is sent on SP2+
   1.153 +  // and on W2K and W2K3. So don't assume it is sent.
   1.154 +  if (DLL_THREAD_DETACH == reason || DLL_PROCESS_DETACH == reason)
   1.155 +    ThreadLocalStorage::ThreadExit();
   1.156 +}
   1.157 +
   1.158 +// .CRT$XLA to .CRT$XLZ is an array of PIMAGE_TLS_CALLBACK pointers that are
   1.159 +// called automatically by the OS loader code (not the CRT) when the module is
   1.160 +// loaded and on thread creation. They are NOT called if the module has been
   1.161 +// loaded by a LoadLibrary() call. It must have implicitly been loaded at
   1.162 +// process startup.
   1.163 +// By implicitly loaded, I mean that it is directly referenced by the main EXE
   1.164 +// or by one of its dependent DLLs. Delay-loaded DLL doesn't count as being
   1.165 +// implicitly loaded.
   1.166 +//
   1.167 +// See VC\crt\src\tlssup.c for reference.
   1.168 +#ifdef _WIN64
   1.169 +
   1.170 +// .CRT section is merged with .rdata on x64 so it must be constant data.
   1.171 +#pragma const_seg(".CRT$XLB")
   1.172 +// When defining a const variable, it must have external linkage to be sure the
   1.173 +// linker doesn't discard it. If this value is discarded, the OnThreadExit
   1.174 +// function will never be called.
   1.175 +extern const PIMAGE_TLS_CALLBACK p_thread_callback;
   1.176 +const PIMAGE_TLS_CALLBACK p_thread_callback = OnThreadExit;
   1.177 +
   1.178 +// Reset the default section.
   1.179 +#pragma const_seg()
   1.180 +
   1.181 +#else  // _WIN64
   1.182 +
   1.183 +#pragma data_seg(".CRT$XLB")
   1.184 +PIMAGE_TLS_CALLBACK p_thread_callback = OnThreadExit;
   1.185 +
   1.186 +// Reset the default section.
   1.187 +#pragma data_seg()
   1.188 +
   1.189 +#endif  // _WIN64

mercurial