TODO(joth): This function could be shared with Singleton, in place of its WaitForInstance() call.
| 16 | // TODO(joth): This function could be shared with Singleton, in place of its |
| 17 | // WaitForInstance() call. |
| 18 | bool 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 | |
| 41 | void CompleteLazyInstance(subtle::AtomicWord* state, |
| 42 | subtle::AtomicWord new_instance, |
no test coverage detected