Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
mmentovai@google.com | 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" |
jhawkins@chromium.org | 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> |
jhawkins@chromium.org | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 10 | |
Hans Wennborg | c3cffa6 | 2020-04-27 10:09:12 | [diff] [blame] | 11 | #include "base/check_op.h" |
Avi Drissman | 63e1f99 | 2023-01-13 18:54:43 | [diff] [blame] | 12 | #include "base/functional/bind.h" |
| 13 | #include "base/functional/callback.h" |
Hans Wennborg | c3cffa6 | 2020-04-27 10:09:12 | [diff] [blame] | 14 | #include "base/notreached.h" |
mmentovai@google.com | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 15 | |
| 16 | namespace base { |
| 17 | |
| 18 | // Keep a stack of registered AtExitManagers. We always operate on the most |
rvargas@google.com | 5ceb1b0 | 2011-04-22 22:09:35 | [diff] [blame] | 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. |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 24 | static AtExitManager* g_top_manager = nullptr; |
mmentovai@google.com | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 25 | |
haraken | bbfdd9f0 | 2017-01-12 07:14:04 | [diff] [blame] | 26 | static bool g_disable_managers = false; |
| 27 | |
Gabriel Charette | 24e8339 | 2018-10-30 23:09:03 | [diff] [blame] | 28 | AtExitManager::AtExitManager() : next_manager_(g_top_manager) { |
rvargas@google.com | 5ceb1b0 | 2011-04-22 22:09:35 | [diff] [blame] | 29 | // If multiple modules instantiate AtExitManagers they'll end up living in this |
| 30 | // module... they have to coexist. |
darin@chromium.org | 63e39a28 | 2011-07-13 20:41:28 | [diff] [blame] | 31 | #if !defined(COMPONENT_BUILD) |
mmentovai@google.com | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 32 | DCHECK(!g_top_manager); |
rvargas@google.com | 5ceb1b0 | 2011-04-22 22:09:35 | [diff] [blame] | 33 | #endif |
mmentovai@google.com | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 34 | g_top_manager = this; |
| 35 | } |
| 36 | |
mmentovai@google.com | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 37 | AtExitManager::~AtExitManager() { |
| 38 | if (!g_top_manager) { |
| 39 | NOTREACHED() << "Tried to ~AtExitManager without an AtExitManager"; |
| 40 | return; |
| 41 | } |
kushi.p@gmail.com | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 42 | DCHECK_EQ(this, g_top_manager); |
mmentovai@google.com | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 43 | |
haraken | bbfdd9f0 | 2017-01-12 07:14:04 | [diff] [blame] | 44 | if (!g_disable_managers) |
| 45 | ProcessCallbacksNow(); |
mmentovai@google.com | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 46 | g_top_manager = next_manager_; |
| 47 | } |
| 48 | |
| 49 | // static |
deanm@google.com | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 50 | void AtExitManager::RegisterCallback(AtExitCallbackType func, void* param) { |
apatrick@chromium.org | 762de91 | 2011-09-06 23:14:47 | [diff] [blame] | 51 | DCHECK(func); |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 52 | RegisterTask(base::BindOnce(func, param)); |
apatrick@chromium.org | 762de91 | 2011-09-06 23:14:47 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // static |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 56 | void AtExitManager::RegisterTask(base::OnceClosure task) { |
mmentovai@google.com | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 57 | if (!g_top_manager) { |
| 58 | NOTREACHED() << "Tried to RegisterCallback without an AtExitManager"; |
| 59 | return; |
| 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)); |
mmentovai@google.com | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // static |
| 70 | void AtExitManager::ProcessCallbacksNow() { |
| 71 | if (!g_top_manager) { |
| 72 | NOTREACHED() << "Tried to ProcessCallbacksNow without an AtExitManager"; |
| 73 | return; |
| 74 | } |
| 75 | |
amistry | f35088d | 2016-02-10 02:18:33 | [diff] [blame] | 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. |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 79 | base::stack<base::OnceClosure> tasks; |
amistry | f35088d | 2016-02-10 02:18:33 | [diff] [blame] | 80 | { |
| 81 | AutoLock lock(g_top_manager->lock_); |
| 82 | tasks.swap(g_top_manager->stack_); |
Gabriel Charette | 24e8339 | 2018-10-30 23:09:03 | [diff] [blame] | 83 | #if DCHECK_IS_ON() |
amistry | f35088d | 2016-02-10 02:18:33 | [diff] [blame] | 84 | g_top_manager->processing_callbacks_ = true; |
Gabriel Charette | 24e8339 | 2018-10-30 23:09:03 | [diff] [blame] | 85 | #endif |
mmentovai@google.com | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 86 | } |
amistry | f35088d | 2016-02-10 02:18:33 | [diff] [blame] | 87 | |
tzik | 3f4231f | 2017-03-31 21:31:24 | [diff] [blame] | 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; |
| 91 | |
amistry | f35088d | 2016-02-10 02:18:33 | [diff] [blame] | 92 | while (!tasks.empty()) { |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 93 | std::move(tasks.top()).Run(); |
amistry | f35088d | 2016-02-10 02:18:33 | [diff] [blame] | 94 | tasks.pop(); |
| 95 | } |
| 96 | |
Gabriel Charette | 24e8339 | 2018-10-30 23:09:03 | [diff] [blame] | 97 | #if DCHECK_IS_ON() |
| 98 | AutoLock lock(g_top_manager->lock_); |
amistry | f35088d | 2016-02-10 02:18:33 | [diff] [blame] | 99 | // Expect that all callbacks have been run. |
| 100 | DCHECK(g_top_manager->stack_.empty()); |
Gabriel Charette | 24e8339 | 2018-10-30 23:09:03 | [diff] [blame] | 101 | g_top_manager->processing_callbacks_ = false; |
| 102 | #endif |
mmentovai@google.com | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 103 | } |
| 104 | |
haraken | bbfdd9f0 | 2017-01-12 07:14:04 | [diff] [blame] | 105 | void AtExitManager::DisableAllAtExitManagers() { |
| 106 | AutoLock lock(g_top_manager->lock_); |
| 107 | g_disable_managers = true; |
| 108 | } |
| 109 | |
Gabriel Charette | 24e8339 | 2018-10-30 23:09:03 | [diff] [blame] | 110 | AtExitManager::AtExitManager(bool shadow) : next_manager_(g_top_manager) { |
erg@google.com | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 111 | DCHECK(shadow || !g_top_manager); |
| 112 | g_top_manager = this; |
| 113 | } |
| 114 | |
mmentovai@google.com | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 115 | } // namespace base |