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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 // can implement the more complicated pieces out of line in the .cc file. | 57 // can implement the more complicated pieces out of line in the .cc file. |
58 class LazyInstanceHelper { | 58 class LazyInstanceHelper { |
59 protected: | 59 protected: |
60 enum { | 60 enum { |
61 STATE_EMPTY = 0, | 61 STATE_EMPTY = 0, |
62 STATE_CREATING = 1, | 62 STATE_CREATING = 1, |
63 STATE_CREATED = 2 | 63 STATE_CREATED = 2 |
64 }; | 64 }; |
65 | 65 |
66 explicit LazyInstanceHelper(LinkerInitialized x) { /* state_ is 0 */ } | 66 explicit LazyInstanceHelper(LinkerInitialized x) { /* state_ is 0 */ } |
67 ~LazyInstanceHelper() { } | 67 // Declaring a destructor (even if it's empty) will cause MSVC to register a |
| 68 // static initializer to register the empty destructor with at_exit(). |
68 | 69 |
69 // Make sure that instance is created, creating or waiting for it to be | 70 // 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 | 71 // created if neccessary. Constructs with |ctor| in the space provided by |
71 // |instance| and registers dtor for destruction at program exit. | 72 // |instance| and registers dtor for destruction at program exit. |
72 void EnsureInstance(void* instance, void (*ctor)(void*), void (*dtor)(void*)); | 73 void EnsureInstance(void* instance, void (*ctor)(void*), void (*dtor)(void*)); |
73 | 74 |
74 base::subtle::Atomic32 state_; | 75 base::subtle::Atomic32 state_; |
75 | 76 |
76 private: | 77 private: |
77 DISALLOW_COPY_AND_ASSIGN(LazyInstanceHelper); | 78 DISALLOW_COPY_AND_ASSIGN(LazyInstanceHelper); |
78 }; | 79 }; |
79 | 80 |
80 template <typename Type, typename Traits = DefaultLazyInstanceTraits<Type> > | 81 template <typename Type, typename Traits = DefaultLazyInstanceTraits<Type> > |
81 class LazyInstance : public LazyInstanceHelper { | 82 class LazyInstance : public LazyInstanceHelper { |
82 public: | 83 public: |
83 explicit LazyInstance(LinkerInitialized x) : LazyInstanceHelper(x) { } | 84 explicit LazyInstance(LinkerInitialized x) : LazyInstanceHelper(x) { } |
84 ~LazyInstance() { } | 85 // Declaring a destructor (even if it's empty) will cause MSVC to register a |
| 86 // static initializer to register the empty destructor with at_exit(). |
85 | 87 |
86 Type& Get() { | 88 Type& Get() { |
87 return *Pointer(); | 89 return *Pointer(); |
88 } | 90 } |
89 | 91 |
90 Type* Pointer() { | 92 Type* Pointer() { |
91 Type* instance = reinterpret_cast<Type*>(&buf_); | 93 Type* instance = reinterpret_cast<Type*>(&buf_); |
92 | 94 |
93 // We will hopefully have fast access when the instance is already created. | 95 // We will hopefully have fast access when the instance is already created. |
94 if (base::subtle::NoBarrier_Load(&state_) != STATE_CREATED) | 96 if (base::subtle::NoBarrier_Load(&state_) != STATE_CREATED) |
95 EnsureInstance(instance, Traits::New, Traits::Delete); | 97 EnsureInstance(instance, Traits::New, Traits::Delete); |
96 | 98 |
97 return instance; | 99 return instance; |
98 } | 100 } |
99 | 101 |
100 private: | 102 private: |
101 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. | 103 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. |
102 | 104 |
103 DISALLOW_COPY_AND_ASSIGN(LazyInstance); | 105 DISALLOW_COPY_AND_ASSIGN(LazyInstance); |
104 }; | 106 }; |
105 | 107 |
106 } // namespace base | 108 } // namespace base |
107 | 109 |
108 #endif // BASE_LAZY_INSTANCE_H_ | 110 #endif // BASE_LAZY_INSTANCE_H_ |
OLD | NEW |