lazy_instance_helpers.h 4.6 KB

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