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.
5 #include "base/at_exit.h"
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/check_op.h"
14 #include "base/notreached.h"
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;
26 static bool g_disable_managers = false;
28 AtExitManager::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);
37 AtExitManager::~AtExitManager() {
39 NOTREACHED() << "Tried to ~AtExitManager without an AtExitManager";
42 DCHECK_EQ(this, g_top_manager);
44 if (!g_disable_managers)
45 ProcessCallbacksNow();
46 g_top_manager = next_manager_;
50 void AtExitManager::RegisterCallback(AtExitCallbackType func, void* param) {
52 RegisterTask(base::BindOnce(func, param));
56 void AtExitManager::RegisterTask(base::OnceClosure task) {
58 NOTREACHED() << "Tried to RegisterCallback without an AtExitManager";
62 AutoLock lock(g_top_manager->lock_);
64 DCHECK(!g_top_manager->processing_callbacks_);
66 g_top_manager->stack_.push(std::move(task));
70 void AtExitManager::ProcessCallbacksNow() {
72 NOTREACHED() << "Tried to ProcessCallbacksNow without an AtExitManager";
76 // Callbacks may try to add new callbacks, so run them without holding
77 // |lock_|. This is an error and caught by the DCHECK in RegisterTask(), but
78 // handle it gracefully in release builds so we don't deadlock.
79 base::stack<base::OnceClosure> tasks;
81 AutoLock lock(g_top_manager->lock_);
82 tasks.swap(g_top_manager->stack_);
84 g_top_manager->processing_callbacks_ = true;
88 // Relax the cross-thread access restriction to non-thread-safe RefCount.
89 // It's safe since all other threads should be terminated at this point.
90 ScopedAllowCrossThreadRefCountAccess allow_cross_thread_ref_count_access;
92 while (!tasks.empty()) {
93 std::move(tasks.top()).Run();
98 AutoLock lock(g_top_manager->lock_);
99 // Expect that all callbacks have been run.
100 DCHECK(g_top_manager->stack_.empty());
101 g_top_manager->processing_callbacks_ = false;
105 void AtExitManager::DisableAllAtExitManagers() {
106 AutoLock lock(g_top_manager->lock_);
107 g_disable_managers = true;
110 AtExitManager::AtExitManager(bool shadow) : next_manager_(g_top_manager) {
111 DCHECK(shadow || !g_top_manager);
112 g_top_manager = this;