thread_local.h (4074B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style license that can be 5 // found in the LICENSE file. 6 7 // WARNING: Thread local storage is a bit tricky to get right. Please make 8 // sure that this is really the proper solution for what you're trying to 9 // achieve. Don't prematurely optimize, most likely you can just use a Lock. 10 // 11 // These classes implement a warpper around the platform's TLS storage 12 // mechanism. On construction, they will allocate a TLS slot, and free the 13 // TLS slot on destruction. No memory management (creation or destruction) is 14 // handled. This means for uses of ThreadLocalPointer, you must correctly 15 // manage the memory yourself, these classes will not destroy the pointer for 16 // you. There are no at-thread-exit actions taken by these classes. 17 // 18 // ThreadLocalPointer<Type> wraps a Type*. It performs no creation or 19 // destruction, so memory management must be handled elsewhere. The first call 20 // to Get() on a thread will return NULL. You can update the pointer with a 21 // call to Set(). 22 // 23 // ThreadLocalBoolean wraps a bool. It will default to false if it has never 24 // been set otherwise with Set(). 25 // 26 // Thread Safety: An instance of ThreadLocalStorage is completely thread safe 27 // once it has been created. If you want to dynamically create an instance, 28 // you must of course properly deal with safety and race conditions. This 29 // means a function-level static initializer is generally inappropiate. 30 // 31 // Example usage: 32 // // My class is logically attached to a single thread. We cache a pointer 33 // // on the thread it was created on, so we can implement current(). 34 // MyClass::MyClass() { 35 // DCHECK(Singleton<ThreadLocalPointer<MyClass> >::get()->Get() == NULL); 36 // Singleton<ThreadLocalPointer<MyClass> >::get()->Set(this); 37 // } 38 // 39 // MyClass::~MyClass() { 40 // DCHECK(Singleton<ThreadLocalPointer<MyClass> >::get()->Get() != NULL); 41 // Singleton<ThreadLocalPointer<MyClass> >::get()->Set(NULL); 42 // } 43 // 44 // // Return the current MyClass associated with the calling thread, can be 45 // // NULL if there isn't a MyClass associated. 46 // MyClass* MyClass::current() { 47 // return Singleton<ThreadLocalPointer<MyClass> >::get()->Get(); 48 // } 49 50 #ifndef BASE_THREAD_LOCAL_H_ 51 #define BASE_THREAD_LOCAL_H_ 52 53 #include "base/basictypes.h" 54 55 #if defined(XP_WIN) 56 # include <windows.h> 57 #else 58 # include <pthread.h> 59 #endif 60 61 namespace base { 62 63 // Helper functions that abstract the cross-platform APIs. Do not use directly. 64 struct ThreadLocalPlatform { 65 #if defined(XP_WIN) 66 typedef DWORD SlotType; 67 #else 68 typedef pthread_key_t SlotType; 69 #endif 70 71 static void AllocateSlot(SlotType& slot); 72 static void FreeSlot(SlotType& slot); 73 static void* GetValueFromSlot(SlotType& slot); 74 static void SetValueInSlot(SlotType& slot, void* value); 75 }; 76 77 template <typename Type> 78 class ThreadLocalPointer { 79 public: 80 ThreadLocalPointer() : slot_() { ThreadLocalPlatform::AllocateSlot(slot_); } 81 82 ThreadLocalPointer(const ThreadLocalPointer&) = delete; 83 ThreadLocalPointer& operator=(const ThreadLocalPointer&) = delete; 84 85 ~ThreadLocalPointer() { ThreadLocalPlatform::FreeSlot(slot_); } 86 87 Type* Get() { 88 return static_cast<Type*>(ThreadLocalPlatform::GetValueFromSlot(slot_)); 89 } 90 91 void Set(Type* ptr) { ThreadLocalPlatform::SetValueInSlot(slot_, ptr); } 92 93 private: 94 typedef ThreadLocalPlatform::SlotType SlotType; 95 96 SlotType slot_; 97 }; 98 99 class ThreadLocalBoolean { 100 public: 101 ThreadLocalBoolean() {} 102 103 ThreadLocalBoolean(const ThreadLocalBoolean&) = delete; 104 ThreadLocalBoolean& operator=(const ThreadLocalBoolean&) = delete; 105 106 ~ThreadLocalBoolean() {} 107 108 bool Get() { return tlp_.Get() != NULL; } 109 110 void Set(bool val) { 111 uintptr_t intVal = val ? 1 : 0; 112 tlp_.Set(reinterpret_cast<void*>(intVal)); 113 } 114 115 private: 116 ThreadLocalPointer<void> tlp_; 117 }; 118 119 } // namespace base 120 121 #endif // BASE_THREAD_LOCAL_H_