michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: // WARNING: Thread local storage is a bit tricky to get right. Please make michael@0: // sure that this is really the proper solution for what you're trying to michael@0: // achieve. Don't prematurely optimize, most likely you can just use a Lock. michael@0: // michael@0: // These classes implement a warpper around the platform's TLS storage michael@0: // mechanism. On construction, they will allocate a TLS slot, and free the michael@0: // TLS slot on destruction. No memory management (creation or destruction) is michael@0: // handled. This means for uses of ThreadLocalPointer, you must correctly michael@0: // manage the memory yourself, these classes will not destroy the pointer for michael@0: // you. There are no at-thread-exit actions taken by these classes. michael@0: // michael@0: // ThreadLocalPointer wraps a Type*. It performs no creation or michael@0: // destruction, so memory management must be handled elsewhere. The first call michael@0: // to Get() on a thread will return NULL. You can update the pointer with a michael@0: // call to Set(). michael@0: // michael@0: // ThreadLocalBoolean wraps a bool. It will default to false if it has never michael@0: // been set otherwise with Set(). michael@0: // michael@0: // Thread Safety: An instance of ThreadLocalStorage is completely thread safe michael@0: // once it has been created. If you want to dynamically create an instance, michael@0: // you must of course properly deal with safety and race conditions. This michael@0: // means a function-level static initializer is generally inappropiate. michael@0: // michael@0: // Example usage: michael@0: // // My class is logically attached to a single thread. We cache a pointer michael@0: // // on the thread it was created on, so we can implement current(). michael@0: // MyClass::MyClass() { michael@0: // DCHECK(Singleton >::get()->Get() == NULL); michael@0: // Singleton >::get()->Set(this); michael@0: // } michael@0: // michael@0: // MyClass::~MyClass() { michael@0: // DCHECK(Singleton >::get()->Get() != NULL); michael@0: // Singleton >::get()->Set(NULL); michael@0: // } michael@0: // michael@0: // // Return the current MyClass associated with the calling thread, can be michael@0: // // NULL if there isn't a MyClass associated. michael@0: // MyClass* MyClass::current() { michael@0: // return Singleton >::get()->Get(); michael@0: // } michael@0: michael@0: #ifndef BASE_THREAD_LOCAL_H_ michael@0: #define BASE_THREAD_LOCAL_H_ michael@0: michael@0: #include "base/basictypes.h" michael@0: michael@0: #if defined(OS_POSIX) michael@0: #include michael@0: #endif michael@0: michael@0: namespace base { michael@0: michael@0: // Helper functions that abstract the cross-platform APIs. Do not use directly. michael@0: struct ThreadLocalPlatform { michael@0: #if defined(OS_WIN) michael@0: typedef int SlotType; michael@0: #elif defined(OS_POSIX) michael@0: typedef pthread_key_t SlotType; michael@0: #endif michael@0: michael@0: static void AllocateSlot(SlotType& slot); michael@0: static void FreeSlot(SlotType& slot); michael@0: static void* GetValueFromSlot(SlotType& slot); michael@0: static void SetValueInSlot(SlotType& slot, void* value); michael@0: }; michael@0: michael@0: template michael@0: class ThreadLocalPointer { michael@0: public: michael@0: ThreadLocalPointer() : slot_() { michael@0: ThreadLocalPlatform::AllocateSlot(slot_); michael@0: } michael@0: michael@0: ~ThreadLocalPointer() { michael@0: ThreadLocalPlatform::FreeSlot(slot_); michael@0: } michael@0: michael@0: Type* Get() { michael@0: return static_cast(ThreadLocalPlatform::GetValueFromSlot(slot_)); michael@0: } michael@0: michael@0: void Set(Type* ptr) { michael@0: ThreadLocalPlatform::SetValueInSlot(slot_, ptr); michael@0: } michael@0: michael@0: private: michael@0: typedef ThreadLocalPlatform::SlotType SlotType; michael@0: michael@0: SlotType slot_; michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(ThreadLocalPointer); michael@0: }; michael@0: michael@0: class ThreadLocalBoolean { michael@0: public: michael@0: ThreadLocalBoolean() { } michael@0: ~ThreadLocalBoolean() { } michael@0: michael@0: bool Get() { michael@0: return tlp_.Get() != NULL; michael@0: } michael@0: michael@0: void Set(bool val) { michael@0: tlp_.Set(reinterpret_cast(val ? 1 : 0)); michael@0: } michael@0: michael@0: private: michael@0: ThreadLocalPointer tlp_; michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(ThreadLocalBoolean); michael@0: }; michael@0: michael@0: } // namespace base michael@0: michael@0: #endif // BASE_THREAD_LOCAL_H_