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