scottbyer@chromium.org | 189aa71 | 2012-07-26 05:36:33 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [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 | 8e73d06 | 2011-04-05 03:04:37 +0900 | [diff] [blame] | 6 | |
| 7 | #include <stddef.h> |
| 8 | #include <ostream> |
| 9 | |
apatrick@chromium.org | 035f4af | 2011-09-07 08:14:47 +0900 | [diff] [blame] | 10 | #include "base/bind.h" |
ajwong@chromium.org | e4f3dc3 | 2012-01-07 07:12:28 +0900 | [diff] [blame] | 11 | #include "base/callback.h" |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 12 | #include "base/logging.h" |
| 13 | |
| 14 | namespace base { |
| 15 | |
| 16 | // Keep a stack of registered AtExitManagers. We always operate on the most |
rvargas@google.com | 3e37cca | 2011-04-23 07:09:35 +0900 | [diff] [blame] | 17 | // recent, and we should never have more than one outside of testing (for a |
| 18 | // statically linked version of this library). Testing may use the shadow |
| 19 | // version of the constructor, and if we are building a dynamic library we may |
| 20 | // end up with multiple AtExitManagers on the same process. We don't protect |
| 21 | // this for thread-safe access, since it will only be modified in testing. |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 22 | static AtExitManager* g_top_manager = NULL; |
| 23 | |
rvargas@google.com | 3e37cca | 2011-04-23 07:09:35 +0900 | [diff] [blame] | 24 | AtExitManager::AtExitManager() : next_manager_(g_top_manager) { |
| 25 | // If multiple modules instantiate AtExitManagers they'll end up living in this |
| 26 | // module... they have to coexist. |
darin@chromium.org | 86d9f94 | 2011-07-14 05:41:28 +0900 | [diff] [blame] | 27 | #if !defined(COMPONENT_BUILD) |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 28 | DCHECK(!g_top_manager); |
rvargas@google.com | 3e37cca | 2011-04-23 07:09:35 +0900 | [diff] [blame] | 29 | #endif |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 30 | g_top_manager = this; |
| 31 | } |
| 32 | |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 33 | AtExitManager::~AtExitManager() { |
| 34 | if (!g_top_manager) { |
| 35 | NOTREACHED() << "Tried to ~AtExitManager without an AtExitManager"; |
| 36 | return; |
| 37 | } |
kushi.p@gmail.com | 90594e3 | 2011-04-29 03:20:09 +0900 | [diff] [blame] | 38 | DCHECK_EQ(this, g_top_manager); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 39 | |
| 40 | ProcessCallbacksNow(); |
| 41 | g_top_manager = next_manager_; |
| 42 | } |
| 43 | |
| 44 | // static |
deanm@google.com | f6299e7 | 2008-09-08 18:06:51 +0900 | [diff] [blame] | 45 | void AtExitManager::RegisterCallback(AtExitCallbackType func, void* param) { |
apatrick@chromium.org | 035f4af | 2011-09-07 08:14:47 +0900 | [diff] [blame] | 46 | DCHECK(func); |
| 47 | RegisterTask(base::Bind(func, param)); |
| 48 | } |
| 49 | |
| 50 | // static |
| 51 | void AtExitManager::RegisterTask(base::Closure task) { |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 52 | if (!g_top_manager) { |
| 53 | NOTREACHED() << "Tried to RegisterCallback without an AtExitManager"; |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | AutoLock lock(g_top_manager->lock_); |
apatrick@chromium.org | 035f4af | 2011-09-07 08:14:47 +0900 | [diff] [blame] | 58 | g_top_manager->stack_.push(task); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // static |
| 62 | void AtExitManager::ProcessCallbacksNow() { |
| 63 | if (!g_top_manager) { |
| 64 | NOTREACHED() << "Tried to ProcessCallbacksNow without an AtExitManager"; |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | AutoLock lock(g_top_manager->lock_); |
| 69 | |
| 70 | while (!g_top_manager->stack_.empty()) { |
apatrick@chromium.org | 035f4af | 2011-09-07 08:14:47 +0900 | [diff] [blame] | 71 | base::Closure task = g_top_manager->stack_.top(); |
| 72 | task.Run(); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 73 | g_top_manager->stack_.pop(); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
erg@google.com | 37c078e | 2011-01-11 09:50:59 +0900 | [diff] [blame] | 77 | AtExitManager::AtExitManager(bool shadow) : next_manager_(g_top_manager) { |
| 78 | DCHECK(shadow || !g_top_manager); |
| 79 | g_top_manager = this; |
| 80 | } |
| 81 | |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 82 | } // namespace base |