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

Function NeedsLazyInstance

src/butil/lazy_instance.cc:18–39  ·  view source on GitHub ↗

TODO(joth): This function could be shared with Singleton, in place of its WaitForInstance() call.

Source from the content-addressed store, hash-verified

16// TODO(joth): This function could be shared with Singleton, in place of its
17// WaitForInstance() call.
18bool NeedsLazyInstance(subtle::AtomicWord* state) {
19 // Try to create the instance, if we're the first, will go from 0 to
20 // kLazyInstanceStateCreating, otherwise we've already been beaten here.
21 // The memory access has no memory ordering as state 0 and
22 // kLazyInstanceStateCreating have no associated data (memory barriers are
23 // all about ordering of memory accesses to *associated* data).
24 if (subtle::NoBarrier_CompareAndSwap(state, 0,
25 kLazyInstanceStateCreating) == 0)
26 // Caller must create instance
27 return true;
28
29 // It's either in the process of being created, or already created. Spin.
30 // The load has acquire memory ordering as a thread which sees
31 // state_ == STATE_CREATED needs to acquire visibility over
32 // the associated data (buf_). Pairing Release_Store is in
33 // CompleteLazyInstance().
34 while (subtle::Acquire_Load(state) == kLazyInstanceStateCreating) {
35 PlatformThread::YieldCurrentThread();
36 }
37 // Someone else created the instance.
38 return false;
39}
40
41void CompleteLazyInstance(subtle::AtomicWord* state,
42 subtle::AtomicWord new_instance,

Callers 1

PointerMethod · 0.85

Calls 2

NoBarrier_CompareAndSwapFunction · 0.70
Acquire_LoadFunction · 0.70

Tested by

no test coverage detected