| 6 |
| // that this is really the proper solution for what you're trying to achieve. |
| 7 | // Don't prematurely optimize, most likely you can just use a Lock. |
| 8 | // |
| 9 | // These classes implement a wrapper around ThreadLocalStorage::Slot. On |
| 10 | // construction, they will allocate a TLS slot, and free the TLS slot on |
| 11 | // destruction. No memory management (creation or destruction) is handled. This |
| 12 | // means for uses of ThreadLocalPointer, you must correctly manage the memory |
| 13 | // yourself, these classes will not destroy the pointer for you. There are no |
| 14 | // at-thread-exit actions taken by these classes. |
| 15 | // |
| 16 | // ThreadLocalPointer<Type> wraps a Type*. It performs no creation or |
| 17 | // destruction, so memory management must be handled elsewhere. The first call |
| 18 | // to Get() on a thread will return NULL. You can update the pointer with a call |
| 19 | // to Set(). |
| 20 | // |
| 21 | // ThreadLocalBoolean wraps a bool. It will default to false if it has never |
| 22 | // been set otherwise with Set(). |
| 23 | // |
| 24 | // Thread Safety: An instance of ThreadLocalStorage is completely thread safe |
| 25 | // once it has been created. If you want to dynamically create an instance, you |
| 26 | // must of course properly deal with safety and race conditions. |
| 27 | // |
| 28 | // In Android, the system TLS is limited. |
| 29 | // |
| 30 | // Example usage: |
| 31 | // // My class is logically attached to a single thread. We cache a pointer |
| 32 | // // on the thread it was created on, so we can implement current(). |
| 33 | // MyClass::MyClass() { |
| 34 | // DCHECK(Singleton<ThreadLocalPointer<MyClass> >::get()->Get() == NULL); |
| 35 | // Singleton<ThreadLocalPointer<MyClass> >::get()->Set(this); |
| 36 | // } |
| 37 | // |
| 38 | // MyClass::~MyClass() { |
| 39 | // DCHECK(Singleton<ThreadLocalPointer<MyClass> >::get()->Get() != NULL); |
| 40 | // Singleton<ThreadLocalPointer<MyClass> >::get()->Set(NULL); |
| 41 | // } |
| 42 | // |
| 43 | // // Return the current MyClass associated with the calling thread, can be |
| 44 | // // NULL if there isn't a MyClass associated. |
| 45 | // MyClass* MyClass::current() { |
| 46 | // return Singleton<ThreadLocalPointer<MyClass> >::get()->Get(); |
| 47 | // } |
| 48 |
| 49 | #ifndef BASE_THREADING_THREAD_LOCAL_H_ |
| 50 | #define BASE_THREADING_THREAD_LOCAL_H_ |
| 51 |
| 52 | #include <memory> |
| 53 |
| 54 | #include "base/logging.h" |
| 55 | #include "base/macros.h" |
| 56 | #include "base/threading/thread_local_internal.h" |
| 57 | #include "base/threading/thread_local_storage.h" |
| 58 |
| 59 | namespace base { |
| 60 |
| 61 | template <typename T> |
| 62 | class ThreadLocalPointer { |
| 63 | public: |
| 64 | ThreadLocalPointer() = default; |
| 65 | ~ThreadLocalPointer() = default; |
| 66 |
| 67 | T* Get() const { return static_cast<T*>(slot_.Get()); } |
| 68 |
| 69 | void Set(T* ptr) { |
| 70 | slot_.Set(const_cast<void*>(static_cast<const void*>(ptr))); |
| 71 | } |
| 72 |
| 73 | private: |
| 74 | ThreadLocalStorage::Slot slot_; |
| 75 |
| 76 | DISALLOW_COPY_AND_ASSIGN(ThreadLocalPointer<T>); |
| 77 | }; |
| 78 |
| 79 | // A ThreadLocalOwnedPointer<T> is like a ThreadLocalPointer<T> except that |
| 80 | // pointers handed to it are owned and automatically deleted during their |
| 81 | // associated thread's exit phase (or when replaced if Set() is invoked multiple |
| 82 | // times on the same thread). |
| 83 | // The ThreadLocalOwnedPointer instance itself can only be destroyed when no |
| 84 | // threads, other than the one it is destroyed on, have remaining state set in |
| 85 | // it. Typically this means that ThreadLocalOwnedPointer instances are held in |
| 86 | // static storage or at the very least only recycled in the single-threaded |
| 87 | // phase between tests in the same process. |
| 88 | #if DCHECK_IS_ON() |
| 89 | template <typename T> |
| 90 | using ThreadLocalOwnedPointer = internal::CheckedThreadLocalOwnedPointer<T>; |
| 91 | #else // DCHECK_IS_ON() |
| 92 | template <typename T> |
| 93 | class ThreadLocalOwnedPointer { |
| 94 | public: |
| 95 | ThreadLocalOwnedPointer() = default; |
| 96 |
| 97 | ~ThreadLocalOwnedPointer() { |
| 98 | // Assume that this thread is the only one with potential state left. This |
| 99 | // is verified in ~CheckedThreadLocalOwnedPointer(). |
| 100 | Set(nullptr); |
| 101 | } |
| 102 |
| 103 | T* Get() const { return static_cast<T*>(slot_.Get()); } |
| 104 |
| 105 | void Set(std::unique_ptr<T> ptr) { |
| 106 | delete Get(); |
| 107 | slot_.Set(const_cast<void*>(static_cast<const void*>(ptr.release()))); |
| 108 | } |
| 109 |
| 110 | private: |
| 111 | static void DeleteTlsPtr(void* ptr) { delete static_cast<T*>(ptr); } |
| 112 |
| 113 | ThreadLocalStorage::Slot slot_{&DeleteTlsPtr}; |
| 114 |
| 115 | DISALLOW_COPY_AND_ASSIGN(ThreadLocalOwnedPointer<T>); |
| 116 | }; |
| 117 | #endif // DCHECK_IS_ON() |
| 118 |
| 119 | class ThreadLocalBoolean { |
| 120 | public: |
| 121 | ThreadLocalBoolean() = default; |
| 122 | ~ThreadLocalBoolean() = default; |
| 123 |
| 124 | bool Get() const { return tlp_.Get() != nullptr; } |
| 125 |
| 126 | void Set(bool val) { tlp_.Set(val ? this : nullptr); } |
| 127 |
| 128 | private: |
| 129 | ThreadLocalPointer<void> tlp_; |
| 130 |
| 131 | DISALLOW_COPY_AND_ASSIGN(ThreadLocalBoolean); |
| 132 | }; |
| 133 |
| 134 | } // namespace base |
| 135 |
| 136 | #endif // BASE_THREADING_THREAD_LOCAL_H_ |
| 137 |