MCPcopy Create free account
hub / github.com/apache/brpc / Pointer

Method Pointer

src/butil/lazy_instance.h:159–194  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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_)) {

Callers 6

SlowDelegateClass · 0.80
TESTFunction · 0.80
RandBytesFunction · 0.80
GetUrandomFDFunction · 0.80
ThreadMainMethod · 0.80
ResetStaticDataMethod · 0.80

Calls 4

AssertSingletonAllowedFunction · 0.85
NeedsLazyInstanceFunction · 0.85
CompleteLazyInstanceFunction · 0.85
Acquire_LoadFunction · 0.70

Tested by 1

TESTFunction · 0.64