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