[email protected] | fe992bf0 | 2012-07-25 20:36:33 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 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" | ||||
[email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 6 | |
7 | #include <stddef.h> | ||||
8 | #include <ostream> | ||||
dcheng | d02ce9c | 2016-07-06 03:59:26 | [diff] [blame] | 9 | #include <utility> |
[email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 10 | |
[email protected] | 762de91 | 2011-09-06 23:14:47 | [diff] [blame] | 11 | #include "base/bind.h" |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 12 | #include "base/callback.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 13 | #include "base/logging.h" |
14 | |||||
15 | namespace base { | ||||
16 | |||||
17 | // Keep a stack of registered AtExitManagers. We always operate on the most | ||||
[email protected] | 5ceb1b0 | 2011-04-22 22:09:35 | [diff] [blame] | 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. | ||||
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 23 | static AtExitManager* g_top_manager = nullptr; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 24 | |
haraken | bbfdd9f0 | 2017-01-12 07:14:04 | [diff] [blame] | 25 | static bool g_disable_managers = false; |
26 | |||||
Gabriel Charette | 24e8339 | 2018-10-30 23:09:03 | [diff] [blame] | 27 | AtExitManager::AtExitManager() : next_manager_(g_top_manager) { |
[email protected] | 5ceb1b0 | 2011-04-22 22:09:35 | [diff] [blame] | 28 | // If multiple modules instantiate AtExitManagers they'll end up living in this |
29 | // module... they have to coexist. | ||||
[email protected] | 63e39a28 | 2011-07-13 20:41:28 | [diff] [blame] | 30 | #if !defined(COMPONENT_BUILD) |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 31 | DCHECK(!g_top_manager); |
[email protected] | 5ceb1b0 | 2011-04-22 22:09:35 | [diff] [blame] | 32 | #endif |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 33 | g_top_manager = this; |
34 | } | ||||
35 | |||||
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 36 | AtExitManager::~AtExitManager() { |
37 | if (!g_top_manager) { | ||||
38 | NOTREACHED() << "Tried to ~AtExitManager without an AtExitManager"; | ||||
39 | return; | ||||
40 | } | ||||
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 41 | DCHECK_EQ(this, g_top_manager); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 42 | |
haraken | bbfdd9f0 | 2017-01-12 07:14:04 | [diff] [blame] | 43 | if (!g_disable_managers) |
44 | ProcessCallbacksNow(); | ||||
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 45 | g_top_manager = next_manager_; |
46 | } | ||||
47 | |||||
48 | // static | ||||
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 49 | void AtExitManager::RegisterCallback(AtExitCallbackType func, void* param) { |
[email protected] | 762de91 | 2011-09-06 23:14:47 | [diff] [blame] | 50 | DCHECK(func); |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame^] | 51 | RegisterTask(base::BindOnce(func, param)); |
[email protected] | 762de91 | 2011-09-06 23:14:47 | [diff] [blame] | 52 | } |
53 | |||||
54 | // static | ||||
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame^] | 55 | void AtExitManager::RegisterTask(base::OnceClosure task) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 56 | if (!g_top_manager) { |
57 | NOTREACHED() << "Tried to RegisterCallback without an AtExitManager"; | ||||
58 | return; | ||||
59 | } | ||||
60 | |||||
61 | AutoLock lock(g_top_manager->lock_); | ||||
Gabriel Charette | 24e8339 | 2018-10-30 23:09:03 | [diff] [blame] | 62 | #if DCHECK_IS_ON() |
amistry | f35088d | 2016-02-10 02:18:33 | [diff] [blame] | 63 | DCHECK(!g_top_manager->processing_callbacks_); |
Gabriel Charette | 24e8339 | 2018-10-30 23:09:03 | [diff] [blame] | 64 | #endif |
dcheng | d02ce9c | 2016-07-06 03:59:26 | [diff] [blame] | 65 | g_top_manager->stack_.push(std::move(task)); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 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 | |||||
amistry | f35088d | 2016-02-10 02:18:33 | [diff] [blame] | 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. | ||||
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame^] | 78 | base::stack<base::OnceClosure> tasks; |
amistry | f35088d | 2016-02-10 02:18:33 | [diff] [blame] | 79 | { |
80 | AutoLock lock(g_top_manager->lock_); | ||||
81 | tasks.swap(g_top_manager->stack_); | ||||
Gabriel Charette | 24e8339 | 2018-10-30 23:09:03 | [diff] [blame] | 82 | #if DCHECK_IS_ON() |
amistry | f35088d | 2016-02-10 02:18:33 | [diff] [blame] | 83 | g_top_manager->processing_callbacks_ = true; |
Gabriel Charette | 24e8339 | 2018-10-30 23:09:03 | [diff] [blame] | 84 | #endif |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 85 | } |
amistry | f35088d | 2016-02-10 02:18:33 | [diff] [blame] | 86 | |
tzik | 3f4231f | 2017-03-31 21:31:24 | [diff] [blame] | 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 | |||||
amistry | f35088d | 2016-02-10 02:18:33 | [diff] [blame] | 91 | while (!tasks.empty()) { |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame^] | 92 | std::move(tasks.top()).Run(); |
amistry | f35088d | 2016-02-10 02:18:33 | [diff] [blame] | 93 | tasks.pop(); |
94 | } | ||||
95 | |||||
Gabriel Charette | 24e8339 | 2018-10-30 23:09:03 | [diff] [blame] | 96 | #if DCHECK_IS_ON() |
97 | AutoLock lock(g_top_manager->lock_); | ||||
amistry | f35088d | 2016-02-10 02:18:33 | [diff] [blame] | 98 | // Expect that all callbacks have been run. |
99 | DCHECK(g_top_manager->stack_.empty()); | ||||
Gabriel Charette | 24e8339 | 2018-10-30 23:09:03 | [diff] [blame] | 100 | g_top_manager->processing_callbacks_ = false; |
101 | #endif | ||||
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 102 | } |
103 | |||||
haraken | bbfdd9f0 | 2017-01-12 07:14:04 | [diff] [blame] | 104 | void AtExitManager::DisableAllAtExitManagers() { |
105 | AutoLock lock(g_top_manager->lock_); | ||||
106 | g_disable_managers = true; | ||||
107 | } | ||||
108 | |||||
Gabriel Charette | 24e8339 | 2018-10-30 23:09:03 | [diff] [blame] | 109 | AtExitManager::AtExitManager(bool shadow) : next_manager_(g_top_manager) { |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 110 | DCHECK(shadow || !g_top_manager); |
111 | g_top_manager = this; | ||||
112 | } | ||||
113 | |||||
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 114 | } // namespace base |