lazy_instance_helpers.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #include "base/lazy_instance_helpers.h"
  5. #include <atomic>
  6. #include "base/at_exit.h"
  7. #include "base/threading/platform_thread.h"
  8. namespace base {
  9. namespace internal {
  10. bool NeedsLazyInstance(std::atomic<uintptr_t>& state) {
  11. // Try to create the instance, if we're the first, will go from 0 to
  12. // kLazyInstanceStateCreating, otherwise we've already been beaten here.
  13. // The memory access has no memory ordering as state 0 and
  14. // kLazyInstanceStateCreating have no associated data (memory barriers are
  15. // all about ordering of memory accesses to *associated* data).
  16. uintptr_t expected = 0;
  17. if (state.compare_exchange_strong(expected, kLazyInstanceStateCreating,
  18. std::memory_order_relaxed,
  19. std::memory_order_relaxed)) {
  20. // Caller must create instance
  21. return true;
  22. }
  23. // It's either in the process of being created, or already created. Spin.
  24. // The load has acquire memory ordering as a thread which sees
  25. // state_ == STATE_CREATED needs to acquire visibility over
  26. // the associated data (buf_). Pairing Release_Store is in
  27. // CompleteLazyInstance().
  28. if (state.load(std::memory_order_acquire) == kLazyInstanceStateCreating) {
  29. const base::TimeTicks start = base::TimeTicks::Now();
  30. do {
  31. const base::TimeDelta elapsed = base::TimeTicks::Now() - start;
  32. // Spin with YieldCurrentThread for at most one ms - this ensures
  33. // maximum responsiveness. After that spin with Sleep(1ms) so that we
  34. // don't burn excessive CPU time - this also avoids infinite loops due
  35. // to priority inversions (https://crbug.com/797129).
  36. if (elapsed < Milliseconds(1))
  37. PlatformThread::YieldCurrentThread();
  38. else
  39. PlatformThread::Sleep(Milliseconds(1));
  40. } while (state.load(std::memory_order_acquire) ==
  41. kLazyInstanceStateCreating);
  42. }
  43. // Someone else created the instance.
  44. return false;
  45. }
  46. void CompleteLazyInstance(std::atomic<uintptr_t>& state,
  47. uintptr_t new_instance,
  48. void (*destructor)(void*),
  49. void* destructor_arg) {
  50. // Instance is created, go from CREATING to CREATED (or reset it if
  51. // |new_instance| is null). Releases visibility over |private_buf_| to
  52. // readers. Pairing Acquire_Load is in NeedsLazyInstance().
  53. state.store(new_instance, std::memory_order_release);
  54. // Make sure that the lazily instantiated object will get destroyed at exit.
  55. if (new_instance && destructor)
  56. AtExitManager::RegisterCallback(destructor, destructor_arg);
  57. }
  58. } // namespace internal
  59. } // namespace base