| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/at_exit.h" | 5 #include "base/at_exit.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <ostream> | 8 #include <ostream> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 | 16 |
| 17 // Keep a stack of registered AtExitManagers. We always operate on the most | 17 // Keep a stack of registered AtExitManagers. We always operate on the most |
| 18 // recent, and we should never have more than one outside of testing (for a | 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 | 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 | 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 | 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. | 22 // this for thread-safe access, since it will only be modified in testing. |
| 23 static AtExitManager* g_top_manager = NULL; | 23 static AtExitManager* g_top_manager = NULL; |
| 24 | 24 |
| 25 static bool g_disable_managers = false; |
| 26 |
| 25 AtExitManager::AtExitManager() | 27 AtExitManager::AtExitManager() |
| 26 : processing_callbacks_(false), next_manager_(g_top_manager) { | 28 : processing_callbacks_(false), next_manager_(g_top_manager) { |
| 27 // If multiple modules instantiate AtExitManagers they'll end up living in this | 29 // If multiple modules instantiate AtExitManagers they'll end up living in this |
| 28 // module... they have to coexist. | 30 // module... they have to coexist. |
| 29 #if !defined(COMPONENT_BUILD) | 31 #if !defined(COMPONENT_BUILD) |
| 30 DCHECK(!g_top_manager); | 32 DCHECK(!g_top_manager); |
| 31 #endif | 33 #endif |
| 32 g_top_manager = this; | 34 g_top_manager = this; |
| 33 } | 35 } |
| 34 | 36 |
| 35 AtExitManager::~AtExitManager() { | 37 AtExitManager::~AtExitManager() { |
| 36 if (!g_top_manager) { | 38 if (!g_top_manager) { |
| 37 NOTREACHED() << "Tried to ~AtExitManager without an AtExitManager"; | 39 NOTREACHED() << "Tried to ~AtExitManager without an AtExitManager"; |
| 38 return; | 40 return; |
| 39 } | 41 } |
| 40 DCHECK_EQ(this, g_top_manager); | 42 DCHECK_EQ(this, g_top_manager); |
| 41 | 43 |
| 42 ProcessCallbacksNow(); | 44 if (!g_disable_managers) |
| 45 ProcessCallbacksNow(); |
| 43 g_top_manager = next_manager_; | 46 g_top_manager = next_manager_; |
| 44 } | 47 } |
| 45 | 48 |
| 46 // static | 49 // static |
| 47 void AtExitManager::RegisterCallback(AtExitCallbackType func, void* param) { | 50 void AtExitManager::RegisterCallback(AtExitCallbackType func, void* param) { |
| 48 DCHECK(func); | 51 DCHECK(func); |
| 49 RegisterTask(base::Bind(func, param)); | 52 RegisterTask(base::Bind(func, param)); |
| 50 } | 53 } |
| 51 | 54 |
| 52 // static | 55 // static |
| (...skipping 28 matching lines...) Expand all Loading... |
| 81 while (!tasks.empty()) { | 84 while (!tasks.empty()) { |
| 82 base::Closure task = tasks.top(); | 85 base::Closure task = tasks.top(); |
| 83 task.Run(); | 86 task.Run(); |
| 84 tasks.pop(); | 87 tasks.pop(); |
| 85 } | 88 } |
| 86 | 89 |
| 87 // Expect that all callbacks have been run. | 90 // Expect that all callbacks have been run. |
| 88 DCHECK(g_top_manager->stack_.empty()); | 91 DCHECK(g_top_manager->stack_.empty()); |
| 89 } | 92 } |
| 90 | 93 |
| 94 void AtExitManager::DisableAllAtExitManagers() { |
| 95 AutoLock lock(g_top_manager->lock_); |
| 96 g_disable_managers = true; |
| 97 } |
| 98 |
| 91 AtExitManager::AtExitManager(bool shadow) | 99 AtExitManager::AtExitManager(bool shadow) |
| 92 : processing_callbacks_(false), next_manager_(g_top_manager) { | 100 : processing_callbacks_(false), next_manager_(g_top_manager) { |
| 93 DCHECK(shadow || !g_top_manager); | 101 DCHECK(shadow || !g_top_manager); |
| 94 g_top_manager = this; | 102 g_top_manager = this; |
| 95 } | 103 } |
| 96 | 104 |
| 97 } // namespace base | 105 } // namespace base |
| OLD | NEW |