Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
[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> | ||||
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 8 | |
[email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 9 | #include <ostream> |
dcheng | d02ce9c | 2016-07-06 03:59:26 | [diff] [blame] | 10 | #include <utility> |
[email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 11 | |
Hans Wennborg | c3cffa6 | 2020-04-27 10:09:12 | [diff] [blame] | 12 | #include "base/check_op.h" |
Avi Drissman | 63e1f99 | 2023-01-13 18:54:43 | [diff] [blame] | 13 | #include "base/functional/bind.h" |
14 | #include "base/functional/callback.h" | ||||
Hans Wennborg | c3cffa6 | 2020-04-27 10:09:12 | [diff] [blame] | 15 | #include "base/notreached.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 16 | |
17 | namespace base { | ||||
18 | |||||
19 | // Keep a stack of registered AtExitManagers. We always operate on the most | ||||
[email protected] | 5ceb1b0 | 2011-04-22 22:09:35 | [diff] [blame] | 20 | // recent, and we should never have more than one outside of testing (for a |
21 | // statically linked version of this library). Testing may use the shadow | ||||
22 | // version of the constructor, and if we are building a dynamic library we may | ||||
23 | // end up with multiple AtExitManagers on the same process. We don't protect | ||||
24 | // this for thread-safe access, since it will only be modified in testing. | ||||
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 25 | static AtExitManager* g_top_manager = nullptr; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 26 | |
haraken | bbfdd9f0 | 2017-01-12 07:14:04 | [diff] [blame] | 27 | static bool g_disable_managers = false; |
28 | |||||
Gabriel Charette | 24e8339 | 2018-10-30 23:09:03 | [diff] [blame] | 29 | AtExitManager::AtExitManager() : next_manager_(g_top_manager) { |
[email protected] | 5ceb1b0 | 2011-04-22 22:09:35 | [diff] [blame] | 30 | // If multiple modules instantiate AtExitManagers they'll end up living in this |
31 | // module... they have to coexist. | ||||
[email protected] | 63e39a28 | 2011-07-13 20:41:28 | [diff] [blame] | 32 | #if !defined(COMPONENT_BUILD) |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 33 | DCHECK(!g_top_manager); |
[email protected] | 5ceb1b0 | 2011-04-22 22:09:35 | [diff] [blame] | 34 | #endif |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 35 | g_top_manager = this; |
36 | } | ||||
37 | |||||
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 38 | AtExitManager::~AtExitManager() { |
39 | if (!g_top_manager) { | ||||
Peter Boström | de57333 | 2024-08-26 20:42:45 | [diff] [blame] | 40 | NOTREACHED() << "Tried to ~AtExitManager without an AtExitManager"; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 41 | } |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 42 | DCHECK_EQ(this, g_top_manager); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 43 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 44 | if (!g_disable_managers) { |
haraken | bbfdd9f0 | 2017-01-12 07:14:04 | [diff] [blame] | 45 | ProcessCallbacksNow(); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 46 | } |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 47 | g_top_manager = next_manager_; |
48 | } | ||||
49 | |||||
50 | // static | ||||
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 51 | void AtExitManager::RegisterCallback(AtExitCallbackType func, void* param) { |
[email protected] | 762de91 | 2011-09-06 23:14:47 | [diff] [blame] | 52 | DCHECK(func); |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 53 | RegisterTask(base::BindOnce(func, param)); |
[email protected] | 762de91 | 2011-09-06 23:14:47 | [diff] [blame] | 54 | } |
55 | |||||
56 | // static | ||||
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 57 | void AtExitManager::RegisterTask(base::OnceClosure task) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 58 | if (!g_top_manager) { |
Peter Boström | de57333 | 2024-08-26 20:42:45 | [diff] [blame] | 59 | NOTREACHED() << "Tried to RegisterCallback without an AtExitManager"; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 60 | } |
61 | |||||
62 | AutoLock lock(g_top_manager->lock_); | ||||
Gabriel Charette | 24e8339 | 2018-10-30 23:09:03 | [diff] [blame] | 63 | #if DCHECK_IS_ON() |
amistry | f35088d | 2016-02-10 02:18:33 | [diff] [blame] | 64 | DCHECK(!g_top_manager->processing_callbacks_); |
Gabriel Charette | 24e8339 | 2018-10-30 23:09:03 | [diff] [blame] | 65 | #endif |
dcheng | d02ce9c | 2016-07-06 03:59:26 | [diff] [blame] | 66 | g_top_manager->stack_.push(std::move(task)); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 67 | } |
68 | |||||
69 | // static | ||||
70 | void AtExitManager::ProcessCallbacksNow() { | ||||
71 | if (!g_top_manager) { | ||||
Peter Boström | de57333 | 2024-08-26 20:42:45 | [diff] [blame] | 72 | NOTREACHED() << "Tried to ProcessCallbacksNow without an AtExitManager"; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 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 |