Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 5 | #include "base/lazy_instance_helpers.h" |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 6 | |
| 7 | #include "base/at_exit.h" |
| 8 | #include "base/atomicops.h" |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 9 | #include "base/threading/platform_thread.h" |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 10 | |
| 11 | namespace base { |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 12 | namespace internal { |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 13 | |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 14 | bool NeedsLazyInstance(subtle::AtomicWord* state) { |
| 15 | // Try to create the instance, if we're the first, will go from 0 to |
| 16 | // kLazyInstanceStateCreating, otherwise we've already been beaten here. |
| 17 | // The memory access has no memory ordering as state 0 and |
| 18 | // kLazyInstanceStateCreating have no associated data (memory barriers are |
| 19 | // all about ordering of memory accesses to *associated* data). |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 20 | if (subtle::NoBarrier_CompareAndSwap(state, 0, kLazyInstanceStateCreating) == |
| 21 | 0) { |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 22 | // Caller must create instance |
| 23 | return true; |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 24 | } |
[email protected] | 1b651d5 | 2011-05-16 15:01:54 | [diff] [blame] | 25 | |
| 26 | // It's either in the process of being created, or already created. Spin. |
| 27 | // The load has acquire memory ordering as a thread which sees |
| 28 | // state_ == STATE_CREATED needs to acquire visibility over |
| 29 | // the associated data (buf_). Pairing Release_Store is in |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 30 | // CompleteLazyInstance(). |
Bruce Dawson | cdf5fae | 2018-01-05 22:12:49 | [diff] [blame] | 31 | if (subtle::Acquire_Load(state) == kLazyInstanceStateCreating) { |
Francois Doray | 94a0386f | 2018-01-23 14:18:46 | [diff] [blame] | 32 | const base::TimeTicks start = base::TimeTicks::Now(); |
Bruce Dawson | cdf5fae | 2018-01-05 22:12:49 | [diff] [blame] | 33 | do { |
Francois Doray | 94a0386f | 2018-01-23 14:18:46 | [diff] [blame] | 34 | const base::TimeDelta elapsed = base::TimeTicks::Now() - start; |
Bruce Dawson | cdf5fae | 2018-01-05 22:12:49 | [diff] [blame] | 35 | // Spin with YieldCurrentThread for at most one ms - this ensures maximum |
| 36 | // responsiveness. After that spin with Sleep(1ms) so that we don't burn |
| 37 | // excessive CPU time - this also avoids infinite loops due to priority |
| 38 | // inversions (https://crbug.com/797129). |
| 39 | if (elapsed < TimeDelta::FromMilliseconds(1)) |
| 40 | PlatformThread::YieldCurrentThread(); |
| 41 | else |
| 42 | PlatformThread::Sleep(TimeDelta::FromMilliseconds(1)); |
| 43 | } while (subtle::Acquire_Load(state) == kLazyInstanceStateCreating); |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 44 | } |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 45 | // Someone else created the instance. |
| 46 | return false; |
| 47 | } |
| 48 | |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 49 | void CompleteLazyInstance(subtle::AtomicWord* state, |
| 50 | subtle::AtomicWord new_instance, |
Francois Doray | a50035b | 2017-06-12 17:55:50 | [diff] [blame] | 51 | void (*destructor)(void*), |
| 52 | void* destructor_arg) { |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 53 | // Instance is created, go from CREATING to CREATED (or reset it if |
| 54 | // |new_instance| is null). Releases visibility over |private_buf_| to |
| 55 | // readers. Pairing Acquire_Load is in NeedsLazyInstance(). |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 56 | subtle::Release_Store(state, new_instance); |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 57 | |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 58 | // Make sure that the lazily instantiated object will get destroyed at exit. |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 59 | if (new_instance && destructor) |
Francois Doray | a50035b | 2017-06-12 17:55:50 | [diff] [blame] | 60 | AtExitManager::RegisterCallback(destructor, destructor_arg); |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 61 | } |
| 62 | |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 63 | } // namespace internal |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 64 | } // namespace base |