Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [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 | |
Lei Zhang | 6e17434 | 2021-04-21 03:57:20 | [diff] [blame] | 5 | #ifndef BASE_LAZY_INSTANCE_HELPERS_H_ |
| 6 | #define BASE_LAZY_INSTANCE_HELPERS_H_ |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 7 | |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 8 | #include<atomic> |
| 9 | #include<cstdint> |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 10 | |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 11 | #include"base/base_export.h" |
Hans Wennborg | 7b53371 | 2020-06-22 20:52:27 | [diff] [blame] | 12 | #include"base/check.h" |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 13 | |
| 14 | // Helper methods used by LazyInstance and a few other base APIs for thread-safe |
| 15 | // lazy construction. |
| 16 | |
| 17 | namespacebase{ |
| 18 | namespaceinternal{ |
| 19 | |
| 20 | // Our AtomicWord doubles as a spinlock, where a value of |
| 21 | // kLazyInstanceStateCreating means the spinlock is being held for creation. |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 22 | constexpruintptr_t kLazyInstanceStateCreating=1; |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 23 | |
| 24 | // Helper for GetOrCreateLazyPointer(). Checks if instance needs to be created. |
| 25 | // If so returns true otherwise if another thread has beat us, waits for |
| 26 | // instance to be created and returns false. |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 27 | BASE_EXPORTboolNeedsLazyInstance(std::atomic<uintptr_t>& state); |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 28 | |
| 29 | // Helper for GetOrCreateLazyPointer(). After creating an instance, this is |
| 30 | // called to register the dtor to be called at program exit and to update the |
| 31 | // atomic state to hold the |new_instance| |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 32 | BASE_EXPORTvoidCompleteLazyInstance(std::atomic<uintptr_t>& state, |
| 33 | uintptr_t new_instance, |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 34 | void(*destructor)(void*), |
| 35 | void* destructor_arg); |
| 36 | |
Gabriel Charette | d55aad50 | 2018-01-26 18:07:51 | [diff] [blame] | 37 | }// namespace internal |
| 38 | |
| 39 | namespace subtle{ |
| 40 | |
| 41 | // If |state| is uninitialized (zero), constructs a value using |
| 42 | // |creator_func(creator_arg)|, stores it into |state| and registers |
| 43 | // |destructor(destructor_arg)| to be called when the current AtExitManager goes |
| 44 | // out of scope. Then, returns the value stored in |state|. It is safe to have |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 45 | // concurrent calls to this function with the same |state|. |creator_func| may |
| 46 | // return nullptr if it doesn't want to create an instance anymore (e.g. on |
| 47 | // shutdown), it is from then on required to return nullptr to all callers (ref. |
Gabriel Charette | d55aad50 | 2018-01-26 18:07:51 | [diff] [blame] | 48 | // StaticMemorySingletonTraits). In that case, callers need to synchronize |
| 49 | // before |creator_func| may return a non-null instance again (ref. |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 50 | // StaticMemorySingletonTraits::ResurectForTesting()). |
Gabriel Charette | d55aad50 | 2018-01-26 18:07:51 | [diff] [blame] | 51 | // Implementation note on |creator_func/creator_arg|. It makes for ugly adapters |
| 52 | // but it avoids redundant template instantiations (e.g. saves 27KB in |
| 53 | // chrome.dll) because linker is able to fold these for multiple Types but |
| 54 | // couldn't with the more advanced CreatorFunc template type which in turn |
| 55 | // improves code locality (and application startup) -- ref. |
| 56 | // https://chromium-review.googlesource.com/c/chromium/src/+/530984/5/base/lazy_instance.h#140, |
| 57 | // worsened by https://chromium-review.googlesource.com/c/chromium/src/+/868013 |
| 58 | // and caught then as https://crbug.com/804034. |
| 59 | template<typenameType> |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 60 | Type*GetOrCreateLazyPointer(std::atomic<uintptr_t>& state, |
Gabriel Charette | d55aad50 | 2018-01-26 18:07:51 | [diff] [blame] | 61 | Type*(*creator_func)(void*), |
| 62 | void* creator_arg, |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 63 | void(*destructor)(void*), |
| 64 | void* destructor_arg){ |
Gabriel Charette | d55aad50 | 2018-01-26 18:07:51 | [diff] [blame] | 65 | DCHECK(creator_func); |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 66 | |
| 67 | // If any bit in the created mask is true, the instance has already been |
| 68 | // fully constructed. |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 69 | constexpruintptr_t kLazyInstanceCreatedMask= |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 70 | ~internal::kLazyInstanceStateCreating; |
| 71 | |
| 72 | // We will hopefully have fast access when the instance is already created. |
| 73 | // Since a thread sees |state| == 0 or kLazyInstanceStateCreating at most |
| 74 | // once, the load is taken out of NeedsLazyInstance() as a fast-path. The load |
| 75 | // has acquire memory ordering as a thread which sees |state| > creating needs |
| 76 | // to acquire visibility over the associated data. Pairing Release_Store is in |
| 77 | // CompleteLazyInstance(). |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 78 | uintptr_t instance= state.load(std::memory_order_acquire); |
Gabriel Charette | d55aad50 | 2018-01-26 18:07:51 | [diff] [blame] | 79 | if(!(instance& kLazyInstanceCreatedMask)){ |
| 80 | if(internal::NeedsLazyInstance(state)){ |
| 81 | // This thread won the race and is now responsible for creating the |
| 82 | // instance and storing it back into |state|. |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 83 | instance=reinterpret_cast<uintptr_t>((*creator_func)(creator_arg)); |
Gabriel Charette | d55aad50 | 2018-01-26 18:07:51 | [diff] [blame] | 84 | internal::CompleteLazyInstance(state, instance, destructor, |
| 85 | destructor_arg); |
| 86 | }else{ |
| 87 | // This thread lost the race but now has visibility over the constructed |
| 88 | // instance (NeedsLazyInstance() doesn't return until the constructing |
| 89 | // thread releases the instance via CompleteLazyInstance()). |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 90 | instance= state.load(std::memory_order_acquire); |
Gabriel Charette | d55aad50 | 2018-01-26 18:07:51 | [diff] [blame] | 91 | DCHECK(instance& kLazyInstanceCreatedMask); |
| 92 | } |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 93 | } |
Gabriel Charette | d55aad50 | 2018-01-26 18:07:51 | [diff] [blame] | 94 | returnreinterpret_cast<Type*>(instance); |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 95 | } |
| 96 | |
Gabriel Charette | d55aad50 | 2018-01-26 18:07:51 | [diff] [blame] | 97 | }// namespace subtle |
| 98 | |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 99 | }// namespace base |
| 100 | |
Lei Zhang | 6e17434 | 2021-04-21 03:57:20 | [diff] [blame] | 101 | #endif// BASE_LAZY_INSTANCE_HELPERS_H_ |