at_exit.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright (c) 2011 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/at_exit.h"
  5. #include <stddef.h>
  6. #include <ostream>
  7. #include <utility>
  8. #include "base/bind.h"
  9. #include "base/callback.h"
  10. #include "base/check_op.h"
  11. #include "base/notreached.h"
  12. namespace base {
  13. // Keep a stack of registered AtExitManagers. We always operate on the most
  14. // recent, and we should never have more than one outside of testing (for a
  15. // statically linked version of this library). Testing may use the shadow
  16. // version of the constructor, and if we are building a dynamic library we may
  17. // end up with multiple AtExitManagers on the same process. We don't protect
  18. // this for thread-safe access, since it will only be modified in testing.
  19. static AtExitManager* g_top_manager = nullptr;
  20. static bool g_disable_managers = false;
  21. AtExitManager::AtExitManager() : next_manager_(g_top_manager) {
  22. // If multiple modules instantiate AtExitManagers they'll end up living in this
  23. // module... they have to coexist.
  24. #if !defined(COMPONENT_BUILD)
  25. DCHECK(!g_top_manager);
  26. #endif
  27. g_top_manager = this;
  28. }
  29. AtExitManager::~AtExitManager() {
  30. if (!g_top_manager) {
  31. NOTREACHED() << "Tried to ~AtExitManager without an AtExitManager";
  32. return;
  33. }
  34. DCHECK_EQ(this, g_top_manager);
  35. if (!g_disable_managers)
  36. ProcessCallbacksNow();
  37. g_top_manager = next_manager_;
  38. }
  39. // static
  40. void AtExitManager::RegisterCallback(AtExitCallbackType func, void* param) {
  41. DCHECK(func);
  42. RegisterTask(base::BindOnce(func, param));
  43. }
  44. // static
  45. void AtExitManager::RegisterTask(base::OnceClosure task) {
  46. if (!g_top_manager) {
  47. NOTREACHED() << "Tried to RegisterCallback without an AtExitManager";
  48. return;
  49. }
  50. AutoLock lock(g_top_manager->lock_);
  51. #if DCHECK_IS_ON()
  52. DCHECK(!g_top_manager->processing_callbacks_);
  53. #endif
  54. g_top_manager->stack_.push(std::move(task));
  55. }
  56. // static
  57. void AtExitManager::ProcessCallbacksNow() {
  58. if (!g_top_manager) {
  59. NOTREACHED() << "Tried to ProcessCallbacksNow without an AtExitManager";
  60. return;
  61. }
  62. // Callbacks may try to add new callbacks, so run them without holding
  63. // |lock_|. This is an error and caught by the DCHECK in RegisterTask(), but
  64. // handle it gracefully in release builds so we don't deadlock.
  65. base::stack<base::OnceClosure> tasks;
  66. {
  67. AutoLock lock(g_top_manager->lock_);
  68. tasks.swap(g_top_manager->stack_);
  69. #if DCHECK_IS_ON()
  70. g_top_manager->processing_callbacks_ = true;
  71. #endif
  72. }
  73. // Relax the cross-thread access restriction to non-thread-safe RefCount.
  74. // It's safe since all other threads should be terminated at this point.
  75. ScopedAllowCrossThreadRefCountAccess allow_cross_thread_ref_count_access;
  76. while (!tasks.empty()) {
  77. std::move(tasks.top()).Run();
  78. tasks.pop();
  79. }
  80. #if DCHECK_IS_ON()
  81. AutoLock lock(g_top_manager->lock_);
  82. // Expect that all callbacks have been run.
  83. DCHECK(g_top_manager->stack_.empty());
  84. g_top_manager->processing_callbacks_ = false;
  85. #endif
  86. }
  87. void AtExitManager::DisableAllAtExitManagers() {
  88. AutoLock lock(g_top_manager->lock_);
  89. g_disable_managers = true;
  90. }
  91. AtExitManager::AtExitManager(bool shadow) : next_manager_(g_top_manager) {
  92. DCHECK(shadow || !g_top_manager);
  93. g_top_manager = this;
  94. }
  95. } // namespace base