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