 Chromium Code Reviews
 Chromium Code Reviews Issue 5682008:
  Make members of Singleton<T> private and only visible to the singleton type.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 5682008:
  Make members of Singleton<T> private and only visible to the singleton type.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| 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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 120 | 120 | 
| 121 Type* Pointer() { | 121 Type* Pointer() { | 
| 122 if (!Traits::kAllowedToAccessOnNonjoinableThread) | 122 if (!Traits::kAllowedToAccessOnNonjoinableThread) | 
| 123 base::ThreadRestrictions::AssertSingletonAllowed(); | 123 base::ThreadRestrictions::AssertSingletonAllowed(); | 
| 124 | 124 | 
| 125 // We will hopefully have fast access when the instance is already created. | 125 // We will hopefully have fast access when the instance is already created. | 
| 126 if ((base::subtle::NoBarrier_Load(&state_) != STATE_CREATED) && | 126 if ((base::subtle::NoBarrier_Load(&state_) != STATE_CREATED) && | 
| 127 NeedsInstance()) { | 127 NeedsInstance()) { | 
| 128 // Create the instance in the space provided by |buf_|. | 128 // Create the instance in the space provided by |buf_|. | 
| 129 instance_ = Traits::New(buf_); | 129 instance_ = Traits::New(buf_); | 
| 130 CompleteInstance(instance_, Traits::Delete); | 130 if (Traits::Delete != NULL) // Will be null for LeakyLazyInstannceTraits | 
| 
M-A Ruel
2010/12/13 17:30:51
LeakyLazyInstanceTraits
 | |
| 131 CompleteInstance(this, OnExit); | |
| 131 } | 132 } | 
| 132 | 133 | 
| 133 // This annotation helps race detectors recognize correct lock-less | 134 // This annotation helps race detectors recognize correct lock-less | 
| 134 // synchronization between different threads calling Pointer(). | 135 // synchronization between different threads calling Pointer(). | 
| 135 // We suggest dynamic race detection tool that "Traits::New" above | 136 // We suggest dynamic race detection tool that "Traits::New" above | 
| 136 // and CompleteInstance(...) happens before "return instance_" below. | 137 // and CompleteInstance(...) happens before "return instance_" below. | 
| 137 // See the corresponding HAPPENS_BEFORE in CompleteInstance(...). | 138 // See the corresponding HAPPENS_BEFORE in CompleteInstance(...). | 
| 138 ANNOTATE_HAPPENS_AFTER(&state_); | 139 ANNOTATE_HAPPENS_AFTER(&state_); | 
| 139 return instance_; | 140 return instance_; | 
| 140 } | 141 } | 
| 141 | 142 | 
| 142 private: | 143 private: | 
| 144 // Adapter function for use with AtExit. This should be called single | |
| 145 // threaded, so don't use atomic operations. | |
| 146 // Calling OnExit while the instance is in use by other threads is a mistake. | |
| 147 static void OnExit(void* lazy_instance) { | |
| 148 LazyInstance<Type, Traits>* me = | |
| 149 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); | |
| 150 Traits::Delete(me->instance_); | |
| 151 me->instance_ = NULL; | |
| 152 base::subtle::Release_Store(&me->state_, STATE_EMPTY); | |
| 153 } | |
| 154 | |
| 143 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. | 155 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. | 
| 144 Type *instance_; | 156 Type *instance_; | 
| 145 | 157 | 
| 146 DISALLOW_COPY_AND_ASSIGN(LazyInstance); | 158 DISALLOW_COPY_AND_ASSIGN(LazyInstance); | 
| 147 }; | 159 }; | 
| 148 | 160 | 
| 149 } // namespace base | 161 } // namespace base | 
| 150 | 162 | 
| 151 #endif // BASE_LAZY_INSTANCE_H_ | 163 #endif // BASE_LAZY_INSTANCE_H_ | 
| OLD | NEW |