From 764435b11584cdb52218575095e44366d84290f3 Mon Sep 17 00:00:00 2001 From: "michaelbai@chromium.org" Date: Wed, 5 Mar 2014 09:52:49 +0000 Subject: Switch thread_local to use chromium's TLS implementation This patch switch thread_local to use ThreadLocalStorage::Slot, so it get the TLS slot from the chromium's TLS instead of OS's. There is debate whether the Windows' TLS's destructor implementation is reliable or not, if we are able to remove all the use of TLS destructor, only ThreadLocalStorage::Slot needs to change, as thread_local doesn't use TLS destructor at all. BUG=264406 Review URL: https://codereview.chromium.org/139633003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@254986 0039d316-1c4b-4281-b951-d872f2087c98 --- base/base.gypi | 2 -- base/threading/thread_local.h | 40 +++++----------------------------- base/threading/thread_local_posix.cc | 38 -------------------------------- base/threading/thread_local_storage.cc | 2 +- base/threading/thread_local_win.cc | 40 ---------------------------------- 5 files changed, 7 insertions(+), 115 deletions(-) delete mode 100644 base/threading/thread_local_posix.cc delete mode 100644 base/threading/thread_local_win.cc diff --git a/base/base.gypi b/base/base.gypi index 79d7324..34b09f2 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -616,12 +616,10 @@ 'threading/thread_id_name_manager.cc', 'threading/thread_id_name_manager.h', 'threading/thread_local.h', - 'threading/thread_local_posix.cc', 'threading/thread_local_storage.cc', 'threading/thread_local_storage.h', 'threading/thread_local_storage_posix.cc', 'threading/thread_local_storage_win.cc', - 'threading/thread_local_win.cc', 'threading/thread_restrictions.h', 'threading/thread_restrictions.cc', 'threading/watchdog.cc', diff --git a/base/threading/thread_local.h b/base/threading/thread_local.h index b13be1a..ec8ce49 100644 --- a/base/threading/thread_local.h +++ b/base/threading/thread_local.h @@ -50,55 +50,27 @@ #include "base/base_export.h" #include "base/basictypes.h" - -#if defined(OS_POSIX) -#include -#endif +#include "base/threading/thread_local_storage.h" namespace base { -namespace internal { - -// Helper functions that abstract the cross-platform APIs. Do not use directly. -struct BASE_EXPORT ThreadLocalPlatform { -#if defined(OS_WIN) - typedef unsigned long SlotType; -#elif defined(OS_POSIX) - typedef pthread_key_t SlotType; -#endif - - static void AllocateSlot(SlotType* slot); - static void FreeSlot(SlotType slot); - static void* GetValueFromSlot(SlotType slot); - static void SetValueInSlot(SlotType slot, void* value); -}; - -} // namespace internal template class ThreadLocalPointer { public: - ThreadLocalPointer() : slot_() { - internal::ThreadLocalPlatform::AllocateSlot(&slot_); - } + ThreadLocalPointer() {} - ~ThreadLocalPointer() { - internal::ThreadLocalPlatform::FreeSlot(slot_); - } + ~ThreadLocalPointer() {} Type* Get() { - return static_cast( - internal::ThreadLocalPlatform::GetValueFromSlot(slot_)); + return static_cast(slot_.Get()); } void Set(Type* ptr) { - internal::ThreadLocalPlatform::SetValueInSlot( - slot_, const_cast(static_cast(ptr))); + slot_.Set(const_cast(static_cast(ptr))); } private: - typedef internal::ThreadLocalPlatform::SlotType SlotType; - - SlotType slot_; + ThreadLocalStorage::Slot slot_; DISALLOW_COPY_AND_ASSIGN(ThreadLocalPointer); }; diff --git a/base/threading/thread_local_posix.cc b/base/threading/thread_local_posix.cc deleted file mode 100644 index 526a66d..0000000 --- a/base/threading/thread_local_posix.cc +++ /dev/null @@ -1,38 +0,0 @@ -// 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. - -#include "base/threading/thread_local.h" - -#include - -#include "base/logging.h" - -namespace base { -namespace internal { - -// static -void ThreadLocalPlatform::AllocateSlot(SlotType* slot) { - int error = pthread_key_create(slot, NULL); - CHECK_EQ(error, 0); -} - -// static -void ThreadLocalPlatform::FreeSlot(SlotType slot) { - int error = pthread_key_delete(slot); - DCHECK_EQ(0, error); -} - -// static -void* ThreadLocalPlatform::GetValueFromSlot(SlotType slot) { - return pthread_getspecific(slot); -} - -// static -void ThreadLocalPlatform::SetValueInSlot(SlotType slot, void* value) { - int error = pthread_setspecific(slot, value); - DCHECK_EQ(error, 0); -} - -} // namespace internal -} // namespace base diff --git a/base/threading/thread_local_storage.cc b/base/threading/thread_local_storage.cc index ad4ff68..e1749c4 100644 --- a/base/threading/thread_local_storage.cc +++ b/base/threading/thread_local_storage.cc @@ -31,7 +31,7 @@ base::subtle::AtomicWord g_native_tls_key = base::subtle::Atomic32 g_last_used_tls_key = 0; // The maximum number of 'slots' in our thread local storage stack. -const int kThreadLocalStorageSize = 64; +const int kThreadLocalStorageSize = 256; // The maximum number of times to try to clear slots by calling destructors. // Use pthread naming convention for clarity. diff --git a/base/threading/thread_local_win.cc b/base/threading/thread_local_win.cc deleted file mode 100644 index 1c74e42..0000000 --- a/base/threading/thread_local_win.cc +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) 2010 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. - -#include "base/threading/thread_local.h" - -#include - -#include "base/logging.h" - -namespace base { -namespace internal { - -// static -void ThreadLocalPlatform::AllocateSlot(SlotType* slot) { - *slot = TlsAlloc(); - CHECK_NE(*slot, TLS_OUT_OF_INDEXES); -} - -// static -void ThreadLocalPlatform::FreeSlot(SlotType slot) { - if (!TlsFree(slot)) { - NOTREACHED() << "Failed to deallocate tls slot with TlsFree()."; - } -} - -// static -void* ThreadLocalPlatform::GetValueFromSlot(SlotType slot) { - return TlsGetValue(slot); -} - -// static -void ThreadLocalPlatform::SetValueInSlot(SlotType slot, void* value) { - if (!TlsSetValue(slot, value)) { - LOG(FATAL) << "Failed to TlsSetValue()."; - } -} - -} // namespace internal -} // namespace base -- cgit v1.1