diff options
author | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-05 18:47:46 +0000 |
---|---|---|
committer | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-05 18:47:46 +0000 |
commit | 8807426cdffa24c65e54987c65232c2a8c54fa96 (patch) | |
tree | a07692dc1c4f0c8771cc951bff00699ef89c51f9 /base/threading | |
parent | 21c6c43a17dff88f2ad0c9cc9a71a1bbf7fad2b6 (diff) | |
download | chromium_src-8807426cdffa24c65e54987c65232c2a8c54fa96.zip chromium_src-8807426cdffa24c65e54987c65232c2a8c54fa96.tar.gz chromium_src-8807426cdffa24c65e54987c65232c2a8c54fa96.tar.bz2 |
Revert 254986 "Switch thread_local to use chromium's TLS impleme..."
> 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
TBR=michaelbai@chromium.org
Review URL: https://codereview.chromium.org/184273006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@255102 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, 113 insertions, 7 deletions
diff --git a/base/threading/thread_local.h b/base/threading/thread_local.h index ec8ce49..b13be1a 100644 --- a/base/threading/thread_local.h +++ b/base/threading/thread_local.h @@ -50,27 +50,55 @@ #include "base/base_export.h" #include "base/basictypes.h" -#include "base/threading/thread_local_storage.h" + +#if defined(OS_POSIX) +#include <pthread.h> +#endif 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() {} + ThreadLocalPointer() : slot_() { + internal::ThreadLocalPlatform::AllocateSlot(&slot_); + } - ~ThreadLocalPointer() {} + ~ThreadLocalPointer() { + internal::ThreadLocalPlatform::FreeSlot(slot_); + } Type* Get() { - return static_cast<Type*>(slot_.Get()); + return static_cast<Type*>( + internal::ThreadLocalPlatform::GetValueFromSlot(slot_)); } void Set(Type* ptr) { - slot_.Set(const_cast<void*>(static_cast<const void*>(ptr))); + internal::ThreadLocalPlatform::SetValueInSlot( + slot_, const_cast<void*>(static_cast<const void*>(ptr))); } private: - ThreadLocalStorage::Slot slot_; + typedef internal::ThreadLocalPlatform::SlotType SlotType; + + SlotType slot_; DISALLOW_COPY_AND_ASSIGN(ThreadLocalPointer<Type>); }; diff --git a/base/threading/thread_local_posix.cc b/base/threading/thread_local_posix.cc new file mode 100644 index 0000000..526a66d --- /dev/null +++ b/base/threading/thread_local_posix.cc @@ -0,0 +1,38 @@ +// 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 e1749c4..ad4ff68 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 = 256; +const int kThreadLocalStorageSize = 64; // 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 new file mode 100644 index 0000000..1c74e42 --- /dev/null +++ b/base/threading/thread_local_win.cc @@ -0,0 +1,40 @@ +// 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 |