| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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 // The LazyInstance<Type, Traits> class manages a single instance of Type, | 5 // The LazyInstance<Type, Traits> class manages a single instance of Type, |
| 6 // which will be lazily created on the first time it's accessed. This class is | 6 // which will be lazily created on the first time it's accessed. This class is |
| 7 // useful for places you would normally use a function-level static, but you | 7 // useful for places you would normally use a function-level static, but you |
| 8 // need to have guaranteed thread-safety. The Type constructor will only ever | 8 // need to have guaranteed thread-safety. The Type constructor will only ever |
| 9 // be called once, even if two threads are racing to create the object. Get() | 9 // be called once, even if two threads are racing to create the object. Get() |
| 10 // and Pointer() will always return the same, completely initialized instance. | 10 // and Pointer() will always return the same, completely initialized instance. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 // | 30 // |
| 31 // MyClass* ptr = my_instance.Pointer(); | 31 // MyClass* ptr = my_instance.Pointer(); |
| 32 // ptr->DoDoDo(); // MyClass::DoDoDo | 32 // ptr->DoDoDo(); // MyClass::DoDoDo |
| 33 // } | 33 // } |
| 34 | 34 |
| 35 #ifndef BASE_LAZY_INSTANCE_H_ | 35 #ifndef BASE_LAZY_INSTANCE_H_ |
| 36 #define BASE_LAZY_INSTANCE_H_ | 36 #define BASE_LAZY_INSTANCE_H_ |
| 37 | 37 |
| 38 #include "base/atomicops.h" | 38 #include "base/atomicops.h" |
| 39 #include "base/basictypes.h" | 39 #include "base/basictypes.h" |
| 40 #include "base/dynamic_annotations.h" |
| 40 | 41 |
| 41 namespace base { | 42 namespace base { |
| 42 | 43 |
| 43 template <typename Type> | 44 template <typename Type> |
| 44 struct DefaultLazyInstanceTraits { | 45 struct DefaultLazyInstanceTraits { |
| 45 static void New(void* instance) { | 46 static void New(void* instance) { |
| 46 // Use placement new to initialize our instance in our preallocated space. | 47 // Use placement new to initialize our instance in our preallocated space. |
| 47 // The parenthesis is very important here to force POD type initialization. | 48 // The parenthesis is very important here to force POD type initialization. |
| 48 new (instance) Type(); | 49 new (instance) Type(); |
| 49 } | 50 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 return *Pointer(); | 90 return *Pointer(); |
| 90 } | 91 } |
| 91 | 92 |
| 92 Type* Pointer() { | 93 Type* Pointer() { |
| 93 Type* instance = reinterpret_cast<Type*>(&buf_); | 94 Type* instance = reinterpret_cast<Type*>(&buf_); |
| 94 | 95 |
| 95 // We will hopefully have fast access when the instance is already created. | 96 // We will hopefully have fast access when the instance is already created. |
| 96 if (base::subtle::NoBarrier_Load(&state_) != STATE_CREATED) | 97 if (base::subtle::NoBarrier_Load(&state_) != STATE_CREATED) |
| 97 EnsureInstance(instance, Traits::New, Traits::Delete); | 98 EnsureInstance(instance, Traits::New, Traits::Delete); |
| 98 | 99 |
| 100 // This annotation helps race detectors recognize correct lock-less |
| 101 // synchronization between different threads calling Pointer(). |
| 102 // We suggest dynamic race detection tool that |
| 103 // "ctor(instance)" in EnsureInstance(...) happens before |
| 104 // "return instance" in Pointer(). |
| 105 // See the corresponding HAPPENS_BEFORE in EnsureInstance(...). |
| 106 ANNOTATE_HAPPENS_AFTER(&state_); |
| 107 |
| 99 return instance; | 108 return instance; |
| 100 } | 109 } |
| 101 | 110 |
| 102 private: | 111 private: |
| 103 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. | 112 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. |
| 104 | 113 |
| 105 DISALLOW_COPY_AND_ASSIGN(LazyInstance); | 114 DISALLOW_COPY_AND_ASSIGN(LazyInstance); |
| 106 }; | 115 }; |
| 107 | 116 |
| 108 } // namespace base | 117 } // namespace base |
| 109 | 118 |
| 110 #endif // BASE_LAZY_INSTANCE_H_ | 119 #endif // BASE_LAZY_INSTANCE_H_ |
| OLD | NEW |