Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1403)

Side by Side Diff: base/threading/thread_local.h

Issue 189263003: Switch thread_local to use ThreadPlatformStorage in Android (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sync Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/base.gypi ('k') | base/threading/thread_local_android.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 // TLS slot on destruction. No memory management (creation or destruction) is 11 // TLS slot on destruction. No memory management (creation or destruction) is
12 // handled. This means for uses of ThreadLocalPointer, you must correctly 12 // handled. This means for uses of ThreadLocalPointer, you must correctly
13 // manage the memory yourself, these classes will not destroy the pointer for 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. 14 // you. There are no at-thread-exit actions taken by these classes.
15 // 15 //
16 // ThreadLocalPointer<Type> wraps a Type*. It performs no creation or 16 // ThreadLocalPointer<Type> wraps a Type*. It performs no creation or
17 // destruction, so memory management must be handled elsewhere. The first call 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 18 // to Get() on a thread will return NULL. You can update the pointer with a
19 // call to Set(). 19 // call to Set().
20 // 20 //
21 // ThreadLocalBoolean wraps a bool. It will default to false if it has never 21 // ThreadLocalBoolean wraps a bool. It will default to false if it has never
22 // been set otherwise with Set(). 22 // been set otherwise with Set().
23 // 23 //
24 // Thread Safety: An instance of ThreadLocalStorage is completely thread safe 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, 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 26 // you must of course properly deal with safety and race conditions. This
27 // means a function-level static initializer is generally inappropiate. 27 // means a function-level static initializer is generally inappropiate.
28 // 28 //
29 // In Android, the system TLS is limited, the implementation is backed with
30 // ThreadLocalStorage.
31 //
29 // Example usage: 32 // Example usage:
30 // // My class is logically attached to a single thread. We cache a pointer 33 // // My class is logically attached to a single thread. We cache a pointer
31 // // on the thread it was created on, so we can implement current(). 34 // // on the thread it was created on, so we can implement current().
32 // MyClass::MyClass() { 35 // MyClass::MyClass() {
33 // DCHECK(Singleton<ThreadLocalPointer<MyClass> >::get()->Get() == NULL); 36 // DCHECK(Singleton<ThreadLocalPointer<MyClass> >::get()->Get() == NULL);
34 // Singleton<ThreadLocalPointer<MyClass> >::get()->Set(this); 37 // Singleton<ThreadLocalPointer<MyClass> >::get()->Set(this);
35 // } 38 // }
36 // 39 //
37 // MyClass::~MyClass() { 40 // MyClass::~MyClass() {
38 // DCHECK(Singleton<ThreadLocalPointer<MyClass> >::get()->Get() != NULL); 41 // DCHECK(Singleton<ThreadLocalPointer<MyClass> >::get()->Get() != NULL);
39 // Singleton<ThreadLocalPointer<MyClass> >::get()->Set(NULL); 42 // Singleton<ThreadLocalPointer<MyClass> >::get()->Set(NULL);
40 // } 43 // }
41 // 44 //
42 // // Return the current MyClass associated with the calling thread, can be 45 // // Return the current MyClass associated with the calling thread, can be
43 // // NULL if there isn't a MyClass associated. 46 // // NULL if there isn't a MyClass associated.
44 // MyClass* MyClass::current() { 47 // MyClass* MyClass::current() {
45 // return Singleton<ThreadLocalPointer<MyClass> >::get()->Get(); 48 // return Singleton<ThreadLocalPointer<MyClass> >::get()->Get();
46 // } 49 // }
47 50
48 #ifndef BASE_THREADING_THREAD_LOCAL_H_ 51 #ifndef BASE_THREADING_THREAD_LOCAL_H_
49 #define BASE_THREADING_THREAD_LOCAL_H_ 52 #define BASE_THREADING_THREAD_LOCAL_H_
50 53
51 #include "base/base_export.h" 54 #include "base/base_export.h"
52 #include "base/basictypes.h" 55 #include "base/basictypes.h"
56 #include "base/threading/thread_local_storage.h"
53 57
54 #if defined(OS_POSIX) 58 #if defined(OS_POSIX)
55 #include <pthread.h> 59 #include <pthread.h>
56 #endif 60 #endif
57 61
58 namespace base { 62 namespace base {
59 namespace internal { 63 namespace internal {
60 64
61 // Helper functions that abstract the cross-platform APIs. Do not use directly. 65 // Helper functions that abstract the cross-platform APIs. Do not use directly.
62 struct BASE_EXPORT ThreadLocalPlatform { 66 struct BASE_EXPORT ThreadLocalPlatform {
63 #if defined(OS_WIN) 67 #if defined(OS_WIN)
64 typedef unsigned long SlotType; 68 typedef unsigned long SlotType;
69 #elif defined(OS_ANDROID)
70 typedef ThreadLocalStorage::StaticSlot SlotType;
65 #elif defined(OS_POSIX) 71 #elif defined(OS_POSIX)
66 typedef pthread_key_t SlotType; 72 typedef pthread_key_t SlotType;
67 #endif 73 #endif
68 74
69 static void AllocateSlot(SlotType* slot); 75 static void AllocateSlot(SlotType* slot);
70 static void FreeSlot(SlotType slot); 76 static void FreeSlot(SlotType slot);
71 static void* GetValueFromSlot(SlotType slot); 77 static void* GetValueFromSlot(SlotType slot);
72 static void SetValueInSlot(SlotType slot, void* value); 78 static void SetValueInSlot(SlotType slot, void* value);
73 }; 79 };
74 80
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 124
119 private: 125 private:
120 ThreadLocalPointer<void> tlp_; 126 ThreadLocalPointer<void> tlp_;
121 127
122 DISALLOW_COPY_AND_ASSIGN(ThreadLocalBoolean); 128 DISALLOW_COPY_AND_ASSIGN(ThreadLocalBoolean);
123 }; 129 };
124 130
125 } // namespace base 131 } // namespace base
126 132
127 #endif // BASE_THREADING_THREAD_LOCAL_H_ 133 #endif // BASE_THREADING_THREAD_LOCAL_H_
OLDNEW
« no previous file with comments | « base/base.gypi ('k') | base/threading/thread_local_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698