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 23 matching lines...) Expand all Loading... |
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 #pragma once | 37 #pragma once |
38 | 38 |
39 #include <new> // For placement new. | 39 #include <new> // For placement new. |
40 | 40 |
41 #include "base/atomicops.h" | 41 #include "base/atomicops.h" |
42 #include "base/basictypes.h" | 42 #include "base/basictypes.h" |
43 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 43 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
44 #include "base/thread_restrictions.h" | 44 #include "base/threading/thread_restrictions.h" |
45 | 45 |
46 namespace base { | 46 namespace base { |
47 | 47 |
48 template <typename Type> | 48 template <typename Type> |
49 struct DefaultLazyInstanceTraits { | 49 struct DefaultLazyInstanceTraits { |
50 static const bool kAllowedToAccessOnNonjoinableThread = false; | 50 static const bool kAllowedToAccessOnNonjoinableThread = false; |
51 | 51 |
52 static Type* New(void* instance) { | 52 static Type* New(void* instance) { |
53 // Use placement new to initialize our instance in our preallocated space. | 53 // Use placement new to initialize our instance in our preallocated space. |
54 // The parenthesis is very important here to force POD type initialization. | 54 // The parenthesis is very important here to force POD type initialization. |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 | 155 |
156 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. | 156 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. |
157 Type *instance_; | 157 Type *instance_; |
158 | 158 |
159 DISALLOW_COPY_AND_ASSIGN(LazyInstance); | 159 DISALLOW_COPY_AND_ASSIGN(LazyInstance); |
160 }; | 160 }; |
161 | 161 |
162 } // namespace base | 162 } // namespace base |
163 | 163 |
164 #endif // BASE_LAZY_INSTANCE_H_ | 164 #endif // BASE_LAZY_INSTANCE_H_ |
OLD | NEW |