OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // WARNING: Thread local storage is a bit tricky to get right. Please make | 5 // WARNING: Thread local storage is a bit tricky to get right. Please make |
6 // sure that this is really the proper solution for what you're trying to | 6 // sure that this is really the proper solution for what you're trying to |
7 // achieve. Don't prematurely optimize, most likely you can just use a Lock. | 7 // achieve. Don't prematurely optimize, most likely you can just use a Lock. |
8 // | 8 // |
9 // These classes implement a wrapper around the platform's TLS storage | 9 // These classes implement a wrapper around the platform's TLS storage |
10 // mechanism. On construction, they will allocate a TLS slot, and free the | 10 // mechanism. On construction, they will allocate a TLS slot, and free the |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 // // NULL if there isn't a MyClass associated. | 43 // // NULL if there isn't a MyClass associated. |
44 // MyClass* MyClass::current() { | 44 // MyClass* MyClass::current() { |
45 // return Singleton<ThreadLocalPointer<MyClass> >::get()->Get(); | 45 // return Singleton<ThreadLocalPointer<MyClass> >::get()->Get(); |
46 // } | 46 // } |
47 | 47 |
48 #ifndef BASE_THREADING_THREAD_LOCAL_H_ | 48 #ifndef BASE_THREADING_THREAD_LOCAL_H_ |
49 #define BASE_THREADING_THREAD_LOCAL_H_ | 49 #define BASE_THREADING_THREAD_LOCAL_H_ |
50 | 50 |
51 #include "base/base_export.h" | 51 #include "base/base_export.h" |
52 #include "base/basictypes.h" | 52 #include "base/basictypes.h" |
53 | 53 #include "base/threading/thread_local_storage.h" |
54 #if defined(OS_POSIX) | |
55 #include <pthread.h> | |
56 #endif | |
57 | 54 |
58 namespace base { | 55 namespace base { |
59 namespace internal { | |
60 | |
61 // Helper functions that abstract the cross-platform APIs. Do not use directly. | |
62 struct BASE_EXPORT ThreadLocalPlatform { | |
63 #if defined(OS_WIN) | |
64 typedef unsigned long SlotType; | |
65 #elif defined(OS_POSIX) | |
66 typedef pthread_key_t SlotType; | |
67 #endif | |
68 | |
69 static void AllocateSlot(SlotType* slot); | |
70 static void FreeSlot(SlotType slot); | |
71 static void* GetValueFromSlot(SlotType slot); | |
72 static void SetValueInSlot(SlotType slot, void* value); | |
73 }; | |
74 | |
75 } // namespace internal | |
76 | 56 |
77 template <typename Type> | 57 template <typename Type> |
78 class ThreadLocalPointer { | 58 class ThreadLocalPointer { |
79 public: | 59 public: |
80 ThreadLocalPointer() : slot_() { | 60 ThreadLocalPointer() {} |
81 internal::ThreadLocalPlatform::AllocateSlot(&slot_); | |
82 } | |
83 | 61 |
84 ~ThreadLocalPointer() { | 62 ~ThreadLocalPointer() {} |
85 internal::ThreadLocalPlatform::FreeSlot(slot_); | |
86 } | |
87 | 63 |
88 Type* Get() { | 64 Type* Get() { |
89 return static_cast<Type*>( | 65 return static_cast<Type*>(slot_.Get()); |
90 internal::ThreadLocalPlatform::GetValueFromSlot(slot_)); | |
91 } | 66 } |
92 | 67 |
93 void Set(Type* ptr) { | 68 void Set(Type* ptr) { |
94 internal::ThreadLocalPlatform::SetValueInSlot( | 69 slot_.Set(const_cast<void*>(static_cast<const void*>(ptr))); |
95 slot_, const_cast<void*>(static_cast<const void*>(ptr))); | |
96 } | 70 } |
97 | 71 |
98 private: | 72 private: |
99 typedef internal::ThreadLocalPlatform::SlotType SlotType; | 73 ThreadLocalStorage::Slot slot_; |
100 | |
101 SlotType slot_; | |
102 | 74 |
103 DISALLOW_COPY_AND_ASSIGN(ThreadLocalPointer<Type>); | 75 DISALLOW_COPY_AND_ASSIGN(ThreadLocalPointer<Type>); |
104 }; | 76 }; |
105 | 77 |
106 class ThreadLocalBoolean { | 78 class ThreadLocalBoolean { |
107 public: | 79 public: |
108 ThreadLocalBoolean() {} | 80 ThreadLocalBoolean() {} |
109 ~ThreadLocalBoolean() {} | 81 ~ThreadLocalBoolean() {} |
110 | 82 |
111 bool Get() { | 83 bool Get() { |
112 return tlp_.Get() != NULL; | 84 return tlp_.Get() != NULL; |
113 } | 85 } |
114 | 86 |
115 void Set(bool val) { | 87 void Set(bool val) { |
116 tlp_.Set(val ? this : NULL); | 88 tlp_.Set(val ? this : NULL); |
117 } | 89 } |
118 | 90 |
119 private: | 91 private: |
120 ThreadLocalPointer<void> tlp_; | 92 ThreadLocalPointer<void> tlp_; |
121 | 93 |
122 DISALLOW_COPY_AND_ASSIGN(ThreadLocalBoolean); | 94 DISALLOW_COPY_AND_ASSIGN(ThreadLocalBoolean); |
123 }; | 95 }; |
124 | 96 |
125 } // namespace base | 97 } // namespace base |
126 | 98 |
127 #endif // BASE_THREADING_THREAD_LOCAL_H_ | 99 #endif // BASE_THREADING_THREAD_LOCAL_H_ |
OLD | NEW |