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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 // static initializer to register the empty destructor with atexit(). | 69 // static initializer to register the empty destructor with atexit(). |
70 | 70 |
71 // Make sure that instance is created, creating or waiting for it to be | 71 // Make sure that instance is created, creating or waiting for it to be |
72 // created if neccessary. Constructs with |ctor| in the space provided by | 72 // created if neccessary. Constructs with |ctor| in the space provided by |
73 // |instance| and registers dtor for destruction at program exit. | 73 // |instance| and registers dtor for destruction at program exit. |
74 void EnsureInstance(void* instance, void (*ctor)(void*), void (*dtor)(void*)); | 74 void EnsureInstance(void* instance, void (*ctor)(void*), void (*dtor)(void*)); |
75 | 75 |
76 base::subtle::Atomic32 state_; | 76 base::subtle::Atomic32 state_; |
77 | 77 |
78 private: | 78 private: |
| 79 // Resets state of |helper| to STATE_EMPTY so that it can be reused. |
| 80 // Not thread safe. |
| 81 static void ResetState(void* helper); |
| 82 |
79 DISALLOW_COPY_AND_ASSIGN(LazyInstanceHelper); | 83 DISALLOW_COPY_AND_ASSIGN(LazyInstanceHelper); |
80 }; | 84 }; |
81 | 85 |
82 template <typename Type, typename Traits = DefaultLazyInstanceTraits<Type> > | 86 template <typename Type, typename Traits = DefaultLazyInstanceTraits<Type> > |
83 class LazyInstance : public LazyInstanceHelper { | 87 class LazyInstance : public LazyInstanceHelper { |
84 public: | 88 public: |
85 explicit LazyInstance(LinkerInitialized x) : LazyInstanceHelper(x) { } | 89 explicit LazyInstance(LinkerInitialized x) : LazyInstanceHelper(x) { } |
86 // Declaring a destructor (even if it's empty) will cause MSVC to register a | 90 // Declaring a destructor (even if it's empty) will cause MSVC to register a |
87 // static initializer to register the empty destructor with atexit(). | 91 // static initializer to register the empty destructor with atexit(). |
88 | 92 |
(...skipping 21 matching lines...) Expand all Loading... |
110 | 114 |
111 private: | 115 private: |
112 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. | 116 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. |
113 | 117 |
114 DISALLOW_COPY_AND_ASSIGN(LazyInstance); | 118 DISALLOW_COPY_AND_ASSIGN(LazyInstance); |
115 }; | 119 }; |
116 | 120 |
117 } // namespace base | 121 } // namespace base |
118 | 122 |
119 #endif // BASE_LAZY_INSTANCE_H_ | 123 #endif // BASE_LAZY_INSTANCE_H_ |
OLD | NEW |