| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 #pragma once | 50 #pragma once |
| 51 | 51 |
| 52 #include "base/basictypes.h" | 52 #include "base/basictypes.h" |
| 53 | 53 |
| 54 #if defined(OS_POSIX) | 54 #if defined(OS_POSIX) |
| 55 #include <pthread.h> | 55 #include <pthread.h> |
| 56 #endif | 56 #endif |
| 57 | 57 |
| 58 namespace base { | 58 namespace base { |
| 59 | 59 |
| 60 namespace internal { |
| 61 |
| 60 // Helper functions that abstract the cross-platform APIs. Do not use directly. | 62 // Helper functions that abstract the cross-platform APIs. Do not use directly. |
| 61 struct ThreadLocalPlatform { | 63 struct ThreadLocalPlatform { |
| 62 #if defined(OS_WIN) | 64 #if defined(OS_WIN) |
| 63 typedef unsigned long SlotType; | 65 typedef unsigned long SlotType; |
| 64 #elif defined(OS_POSIX) | 66 #elif defined(OS_POSIX) |
| 65 typedef pthread_key_t SlotType; | 67 typedef pthread_key_t SlotType; |
| 66 #endif | 68 #endif |
| 67 | 69 |
| 68 static void AllocateSlot(SlotType& slot); | 70 static void AllocateSlot(SlotType& slot); |
| 69 static void FreeSlot(SlotType& slot); | 71 static void FreeSlot(SlotType& slot); |
| 70 static void* GetValueFromSlot(SlotType& slot); | 72 static void* GetValueFromSlot(SlotType& slot); |
| 71 static void SetValueInSlot(SlotType& slot, void* value); | 73 static void SetValueInSlot(SlotType& slot, void* value); |
| 72 }; | 74 }; |
| 73 | 75 |
| 76 } // namespace internal |
| 77 |
| 74 template <typename Type> | 78 template <typename Type> |
| 75 class ThreadLocalPointer { | 79 class ThreadLocalPointer { |
| 76 public: | 80 public: |
| 77 ThreadLocalPointer() : slot_() { | 81 ThreadLocalPointer() : slot_() { |
| 78 ThreadLocalPlatform::AllocateSlot(slot_); | 82 internal::ThreadLocalPlatform::AllocateSlot(slot_); |
| 79 } | 83 } |
| 80 | 84 |
| 81 ~ThreadLocalPointer() { | 85 ~ThreadLocalPointer() { |
| 82 ThreadLocalPlatform::FreeSlot(slot_); | 86 internal::ThreadLocalPlatform::FreeSlot(slot_); |
| 83 } | 87 } |
| 84 | 88 |
| 85 Type* Get() { | 89 Type* Get() { |
| 86 return static_cast<Type*>(ThreadLocalPlatform::GetValueFromSlot(slot_)); | 90 return static_cast<Type*>( |
| 91 internal::ThreadLocalPlatform::GetValueFromSlot(slot_)); |
| 87 } | 92 } |
| 88 | 93 |
| 89 void Set(Type* ptr) { | 94 void Set(Type* ptr) { |
| 90 ThreadLocalPlatform::SetValueInSlot(slot_, ptr); | 95 internal::ThreadLocalPlatform::SetValueInSlot(slot_, ptr); |
| 91 } | 96 } |
| 92 | 97 |
| 93 private: | 98 private: |
| 94 typedef ThreadLocalPlatform::SlotType SlotType; | 99 typedef internal::ThreadLocalPlatform::SlotType SlotType; |
| 95 | 100 |
| 96 SlotType slot_; | 101 SlotType slot_; |
| 97 | 102 |
| 98 DISALLOW_COPY_AND_ASSIGN(ThreadLocalPointer<Type>); | 103 DISALLOW_COPY_AND_ASSIGN(ThreadLocalPointer<Type>); |
| 99 }; | 104 }; |
| 100 | 105 |
| 101 class ThreadLocalBoolean { | 106 class ThreadLocalBoolean { |
| 102 public: | 107 public: |
| 103 ThreadLocalBoolean() { } | 108 ThreadLocalBoolean() { } |
| 104 ~ThreadLocalBoolean() { } | 109 ~ThreadLocalBoolean() { } |
| 105 | 110 |
| 106 bool Get() { | 111 bool Get() { |
| 107 return tlp_.Get() != NULL; | 112 return tlp_.Get() != NULL; |
| 108 } | 113 } |
| 109 | 114 |
| 110 void Set(bool val) { | 115 void Set(bool val) { |
| 111 tlp_.Set(reinterpret_cast<void*>(val ? 1 : 0)); | 116 tlp_.Set(reinterpret_cast<void*>(val ? 1 : 0)); |
| 112 } | 117 } |
| 113 | 118 |
| 114 private: | 119 private: |
| 115 ThreadLocalPointer<void> tlp_; | 120 ThreadLocalPointer<void> tlp_; |
| 116 | 121 |
| 117 DISALLOW_COPY_AND_ASSIGN(ThreadLocalBoolean); | 122 DISALLOW_COPY_AND_ASSIGN(ThreadLocalBoolean); |
| 118 }; | 123 }; |
| 119 | 124 |
| 120 } // namespace base | 125 } // namespace base |
| 121 | 126 |
| 122 #endif // BASE_THREAD_LOCAL_H_ | 127 #endif // BASE_THREAD_LOCAL_H_ |
| OLD | NEW |