diff options
author | michaelbai@chromium.org <michaelbai@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-05 09:52:49 +0000 |
---|---|---|
committer | michaelbai@chromium.org <michaelbai@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-05 09:52:49 +0000 |
commit | 764435b11584cdb52218575095e44366d84290f3 (patch) | |
tree | f3bc1ae768d747f1fceb1c133749c18742ca4240 /base/threading | |
parent | 82825a002ed16c3c63dc99a2503fff8db28aaba4 (diff) | |
download | chromium_src-764435b11584cdb52218575095e44366d84290f3.zip chromium_src-764435b11584cdb52218575095e44366d84290f3.tar.gz chromium_src-764435b11584cdb52218575095e44366d84290f3.tar.bz2 |
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
Diffstat (limited to 'base/threading')
-rw-r--r-- | base/threading/thread_local.h | 40 | ||||
-rw-r--r-- | base/threading/thread_local_posix.cc | 38 | ||||
-rw-r--r-- | base/threading/thread_local_storage.cc | 2 | ||||
-rw-r--r-- | base/threading/thread_local_win.cc | 40 |
4 files changed, 7 insertions, 113 deletions
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 <pthread.h> -#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 <typename Type> class ThreadLocalPointer { public: - ThreadLocalPointer() : slot_() { - internal::ThreadLocalPlatform::AllocateSlot(&slot_); - } + ThreadLocalPointer() {} - ~ThreadLocalPointer() { - internal::ThreadLocalPlatform::FreeSlot(slot_); - } + ~ThreadLocalPointer() {} Type* Get() { - return static_cast<Type*>( - internal::ThreadLocalPlatform::GetValueFromSlot(slot_)); + return static_cast<Type*>(slot_.Get()); } void Set(Type* ptr) { - internal::ThreadLocalPlatform::SetValueInSlot( - slot_, const_cast<void*>(static_cast<const void*>(ptr))); + slot_.Set(const_cast<void*>(static_cast<const void*>(ptr))); } private: - typedef internal::ThreadLocalPlatform::SlotType SlotType; - - SlotType slot_; + ThreadLocalStorage::Slot slot_; DISALLOW_COPY_AND_ASSIGN(ThreadLocalPointer<Type>); }; 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 <pthread.h> - -#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 <windows.h> - -#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 |