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 21 matching lines...) Expand all Loading... |
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 #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/base_api.h" |
42 #include "base/basictypes.h" | 43 #include "base/basictypes.h" |
43 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 44 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
44 #include "base/threading/thread_restrictions.h" | 45 #include "base/threading/thread_restrictions.h" |
45 | 46 |
46 namespace base { | 47 namespace base { |
47 | 48 |
48 template <typename Type> | 49 template <typename Type> |
49 struct DefaultLazyInstanceTraits { | 50 struct DefaultLazyInstanceTraits { |
50 static const bool kAllowedToAccessOnNonjoinableThread = false; | 51 static const bool kAllowedToAccessOnNonjoinableThread = false; |
51 | 52 |
(...skipping 21 matching lines...) Expand all Loading... |
73 // LeakyLazyInstanceTraits in contexts where you don't have an | 74 // LeakyLazyInstanceTraits in contexts where you don't have an |
74 // AtExitManager. | 75 // AtExitManager. |
75 static void (*Delete)(void* instance); | 76 static void (*Delete)(void* instance); |
76 }; | 77 }; |
77 | 78 |
78 template <typename Type> | 79 template <typename Type> |
79 void (*LeakyLazyInstanceTraits<Type>::Delete)(void* instance) = NULL; | 80 void (*LeakyLazyInstanceTraits<Type>::Delete)(void* instance) = NULL; |
80 | 81 |
81 // We pull out some of the functionality into a non-templated base, so that we | 82 // We pull out some of the functionality into a non-templated base, so that we |
82 // can implement the more complicated pieces out of line in the .cc file. | 83 // can implement the more complicated pieces out of line in the .cc file. |
83 class LazyInstanceHelper { | 84 class BASE_API LazyInstanceHelper { |
84 protected: | 85 protected: |
85 enum { | 86 enum { |
86 STATE_EMPTY = 0, | 87 STATE_EMPTY = 0, |
87 STATE_CREATING = 1, | 88 STATE_CREATING = 1, |
88 STATE_CREATED = 2 | 89 STATE_CREATED = 2 |
89 }; | 90 }; |
90 | 91 |
91 explicit LazyInstanceHelper(LinkerInitialized /*unused*/) {/* state_ is 0 */} | 92 explicit LazyInstanceHelper(LinkerInitialized /*unused*/) {/* state_ is 0 */} |
92 // Declaring a destructor (even if it's empty) will cause MSVC to register a | 93 // Declaring a destructor (even if it's empty) will cause MSVC to register a |
93 // static initializer to register the empty destructor with atexit(). | 94 // static initializer to register the empty destructor with atexit(). |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 | 169 |
169 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. | 170 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. |
170 Type *instance_; | 171 Type *instance_; |
171 | 172 |
172 DISALLOW_COPY_AND_ASSIGN(LazyInstance); | 173 DISALLOW_COPY_AND_ASSIGN(LazyInstance); |
173 }; | 174 }; |
174 | 175 |
175 } // namespace base | 176 } // namespace base |
176 | 177 |
177 #endif // BASE_LAZY_INSTANCE_H_ | 178 #endif // BASE_LAZY_INSTANCE_H_ |
OLD | NEW |