• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
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
7 // achieve.  Don't prematurely optimize, most likely you can just use a Lock.
8 //
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
11 // TLS slot on destruction.  No memory management (creation or destruction) is
12 // handled.  This means for uses of ThreadLocalPointer, you must correctly
13 // manage the memory yourself, these classes will not destroy the pointer for
14 // you.  There are no 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
19 // call 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,
26 // you must of course properly deal with safety and race conditions.  This
27 // means a function-level static initializer is generally inappropiate.
28 //
29 // In Android, the system TLS is limited, the implementation is backed with
30 // ThreadLocalStorage.
31 //
32 // Example usage:
33 //   // My class is logically attached to a single thread.  We cache a pointer
34 //   // on the thread it was created on, so we can implement current().
35 //   MyClass::MyClass() {
36 //     DCHECK(Singleton<ThreadLocalPointer<MyClass> >::get()->Get() == NULL);
37 //     Singleton<ThreadLocalPointer<MyClass> >::get()->Set(this);
38 //   }
39 //
40 //   MyClass::~MyClass() {
41 //     DCHECK(Singleton<ThreadLocalPointer<MyClass> >::get()->Get() != NULL);
42 //     Singleton<ThreadLocalPointer<MyClass> >::get()->Set(NULL);
43 //   }
44 //
45 //   // Return the current MyClass associated with the calling thread, can be
46 //   // NULL if there isn't a MyClass associated.
47 //   MyClass* MyClass::current() {
48 //     return Singleton<ThreadLocalPointer<MyClass> >::get()->Get();
49 //   }
50 
51 #ifndef BASE_THREADING_THREAD_LOCAL_H_
52 #define BASE_THREADING_THREAD_LOCAL_H_
53 
54 #include "base/base_export.h"
55 #include "base/basictypes.h"
56 #include "base/threading/thread_local_storage.h"
57 
58 #if defined(OS_POSIX)
59 #include <pthread.h>
60 #endif
61 
62 namespace base {
63 namespace internal {
64 
65 // Helper functions that abstract the cross-platform APIs.  Do not use directly.
66 struct BASE_EXPORT ThreadLocalPlatform {
67 #if defined(OS_WIN)
68   typedef unsigned long SlotType;
69 #elif defined(OS_ANDROID)
70   typedef ThreadLocalStorage::StaticSlot SlotType;
71 #elif defined(OS_POSIX)
72   typedef pthread_key_t SlotType;
73 #endif
74 
75   static void AllocateSlot(SlotType* slot);
76   static void FreeSlot(SlotType slot);
77   static void* GetValueFromSlot(SlotType slot);
78   static void SetValueInSlot(SlotType slot, void* value);
79 };
80 
81 }  // namespace internal
82 
83 template <typename Type>
84 class ThreadLocalPointer {
85  public:
ThreadLocalPointer()86   ThreadLocalPointer() : slot_() {
87     internal::ThreadLocalPlatform::AllocateSlot(&slot_);
88   }
89 
~ThreadLocalPointer()90   ~ThreadLocalPointer() {
91     internal::ThreadLocalPlatform::FreeSlot(slot_);
92   }
93 
Get()94   Type* Get() {
95     return static_cast<Type*>(
96         internal::ThreadLocalPlatform::GetValueFromSlot(slot_));
97   }
98 
Set(Type * ptr)99   void Set(Type* ptr) {
100     internal::ThreadLocalPlatform::SetValueInSlot(
101         slot_, const_cast<void*>(static_cast<const void*>(ptr)));
102   }
103 
104  private:
105   typedef internal::ThreadLocalPlatform::SlotType SlotType;
106 
107   SlotType slot_;
108 
109   DISALLOW_COPY_AND_ASSIGN(ThreadLocalPointer<Type>);
110 };
111 
112 class ThreadLocalBoolean {
113  public:
ThreadLocalBoolean()114   ThreadLocalBoolean() {}
~ThreadLocalBoolean()115   ~ThreadLocalBoolean() {}
116 
Get()117   bool Get() {
118     return tlp_.Get() != NULL;
119   }
120 
Set(bool val)121   void Set(bool val) {
122     tlp_.Set(val ? this : NULL);
123   }
124 
125  private:
126   ThreadLocalPointer<void> tlp_;
127 
128   DISALLOW_COPY_AND_ASSIGN(ThreadLocalBoolean);
129 };
130 
131 }  // namespace base
132 
133 #endif  // BASE_THREADING_THREAD_LOCAL_H_
134