| 157 | } |
| 158 | |
| 159 | Type* Pointer() { |
| 160 | #ifndef NDEBUG |
| 161 | // Avoid making TLS lookup on release builds. |
| 162 | if (!Traits::kAllowedToAccessOnNonjoinableThread) |
| 163 | ThreadRestrictions::AssertSingletonAllowed(); |
| 164 | #endif |
| 165 | // If any bit in the created mask is true, the instance has already been |
| 166 | // fully constructed. |
| 167 | static const subtle::AtomicWord kLazyInstanceCreatedMask = |
| 168 | ~internal::kLazyInstanceStateCreating; |
| 169 | |
| 170 | // We will hopefully have fast access when the instance is already created. |
| 171 | // Since a thread sees private_instance_ == 0 or kLazyInstanceStateCreating |
| 172 | // at most once, the load is taken out of NeedsInstance() as a fast-path. |
| 173 | // The load has acquire memory ordering as a thread which sees |
| 174 | // private_instance_ > creating needs to acquire visibility over |
| 175 | // the associated data (private_buf_). Pairing Release_Store is in |
| 176 | // CompleteLazyInstance(). |
| 177 | subtle::AtomicWord value = subtle::Acquire_Load(&private_instance_); |
| 178 | if (!(value & kLazyInstanceCreatedMask) && |
| 179 | internal::NeedsLazyInstance(&private_instance_)) { |
| 180 | // Create the instance in the space provided by |private_buf_|. |
| 181 | value = reinterpret_cast<subtle::AtomicWord>( |
| 182 | Traits::New(private_buf_.void_data())); |
| 183 | internal::CompleteLazyInstance(&private_instance_, value, this, |
| 184 | Traits::kRegisterOnExit ? OnExit : NULL); |
| 185 | } |
| 186 | |
| 187 | // This annotation helps race detectors recognize correct lock-less |
| 188 | // synchronization between different threads calling Pointer(). |
| 189 | // We suggest dynamic race detection tool that "Traits::New" above |
| 190 | // and CompleteLazyInstance(...) happens before "return instance()" below. |
| 191 | // See the corresponding HAPPENS_BEFORE in CompleteLazyInstance(...). |
| 192 | ANNOTATE_HAPPENS_AFTER(&private_instance_); |
| 193 | return instance(); |
| 194 | } |
| 195 | |
| 196 | bool operator==(Type* p) { |
| 197 | switch (subtle::NoBarrier_Load(&private_instance_)) { |