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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 65 |
66 explicit LazyInstanceHelper(LinkerInitialized x) { /* state_ is 0 */ } | 66 explicit LazyInstanceHelper(LinkerInitialized x) { /* state_ is 0 */ } |
67 ~LazyInstanceHelper() { } | 67 ~LazyInstanceHelper() { } |
68 | 68 |
69 // Make sure that instance is created, creating or waiting for it to be | 69 // Make sure that instance is created, creating or waiting for it to be |
70 // created if neccessary. Constructs with |ctor| in the space provided by | 70 // created if neccessary. Constructs with |ctor| in the space provided by |
71 // |instance| and registers dtor for destruction at program exit. | 71 // |instance| and registers dtor for destruction at program exit. |
72 void EnsureInstance(void* instance, void (*ctor)(void*), void (*dtor)(void*)); | 72 void EnsureInstance(void* instance, void (*ctor)(void*), void (*dtor)(void*)); |
73 | 73 |
74 base::subtle::Atomic32 state_; | 74 base::subtle::Atomic32 state_; |
| 75 |
| 76 private: |
| 77 DISALLOW_COPY_AND_ASSIGN(LazyInstanceHelper); |
75 }; | 78 }; |
76 | 79 |
77 template <typename Type, typename Traits = DefaultLazyInstanceTraits<Type> > | 80 template <typename Type, typename Traits = DefaultLazyInstanceTraits<Type> > |
78 class LazyInstance : public LazyInstanceHelper { | 81 class LazyInstance : public LazyInstanceHelper { |
79 public: | 82 public: |
80 explicit LazyInstance(LinkerInitialized x) : LazyInstanceHelper(x) { } | 83 explicit LazyInstance(LinkerInitialized x) : LazyInstanceHelper(x) { } |
81 ~LazyInstance() { } | 84 ~LazyInstance() { } |
82 | 85 |
83 Type& Get() { | 86 Type& Get() { |
84 return *Pointer(); | 87 return *Pointer(); |
(...skipping 11 matching lines...) Expand all Loading... |
96 | 99 |
97 private: | 100 private: |
98 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. | 101 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. |
99 | 102 |
100 DISALLOW_COPY_AND_ASSIGN(LazyInstance); | 103 DISALLOW_COPY_AND_ASSIGN(LazyInstance); |
101 }; | 104 }; |
102 | 105 |
103 } // namespace base | 106 } // namespace base |
104 | 107 |
105 #endif // BASE_LAZY_INSTANCE_H_ | 108 #endif // BASE_LAZY_INSTANCE_H_ |
OLD | NEW |