| OLD | NEW |
| 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 // 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/base_export.h" |
| 43 #include "base/basictypes.h" | 43 #include "base/basictypes.h" |
| 44 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 44 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 45 #include "base/threading/thread_restrictions.h" | 45 #include "base/threading/thread_restrictions.h" |
| 46 | 46 |
| 47 namespace base { | 47 namespace base { |
| 48 | 48 |
| 49 template <typename Type> | 49 template <typename Type> |
| 50 struct DefaultLazyInstanceTraits { | 50 struct DefaultLazyInstanceTraits { |
| 51 static const bool kAllowedToAccessOnNonjoinableThread = false; | 51 static const bool kAllowedToAccessOnNonjoinableThread = false; |
| 52 | 52 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 74 // LeakyLazyInstanceTraits in contexts where you don't have an | 74 // LeakyLazyInstanceTraits in contexts where you don't have an |
| 75 // AtExitManager. | 75 // AtExitManager. |
| 76 static void (*Delete)(void* instance); | 76 static void (*Delete)(void* instance); |
| 77 }; | 77 }; |
| 78 | 78 |
| 79 template <typename Type> | 79 template <typename Type> |
| 80 void (*LeakyLazyInstanceTraits<Type>::Delete)(void* instance) = NULL; | 80 void (*LeakyLazyInstanceTraits<Type>::Delete)(void* instance) = NULL; |
| 81 | 81 |
| 82 // 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 |
| 83 // 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. |
| 84 class BASE_API LazyInstanceHelper { | 84 class BASE_EXPORT LazyInstanceHelper { |
| 85 protected: | 85 protected: |
| 86 enum { | 86 enum { |
| 87 STATE_EMPTY = 0, | 87 STATE_EMPTY = 0, |
| 88 STATE_CREATING = 1, | 88 STATE_CREATING = 1, |
| 89 STATE_CREATED = 2 | 89 STATE_CREATED = 2 |
| 90 }; | 90 }; |
| 91 | 91 |
| 92 explicit LazyInstanceHelper(LinkerInitialized /*unused*/) {/* state_ is 0 */} | 92 explicit LazyInstanceHelper(LinkerInitialized /*unused*/) {/* state_ is 0 */} |
| 93 | 93 |
| 94 // Declaring a destructor (even if it's empty) will cause MSVC to register a | 94 // Declaring a destructor (even if it's empty) will cause MSVC to register a |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 | 188 |
| 189 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. | 189 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. |
| 190 Type *instance_; | 190 Type *instance_; |
| 191 | 191 |
| 192 DISALLOW_COPY_AND_ASSIGN(LazyInstance); | 192 DISALLOW_COPY_AND_ASSIGN(LazyInstance); |
| 193 }; | 193 }; |
| 194 | 194 |
| 195 } // namespace base | 195 } // namespace base |
| 196 | 196 |
| 197 #endif // BASE_LAZY_INSTANCE_H_ | 197 #endif // BASE_LAZY_INSTANCE_H_ |
| OLD | NEW |