michael@0: // Copyright (c) 2011 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/at_exit.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "base/bind.h" michael@0: #include "base/callback.h" michael@0: #include "base/logging.h" michael@0: michael@0: namespace base { michael@0: michael@0: // Keep a stack of registered AtExitManagers. We always operate on the most michael@0: // recent, and we should never have more than one outside of testing (for a michael@0: // statically linked version of this library). Testing may use the shadow michael@0: // version of the constructor, and if we are building a dynamic library we may michael@0: // end up with multiple AtExitManagers on the same process. We don't protect michael@0: // this for thread-safe access, since it will only be modified in testing. michael@0: static AtExitManager* g_top_manager = NULL; michael@0: michael@0: AtExitManager::AtExitManager() : next_manager_(g_top_manager) { michael@0: // If multiple modules instantiate AtExitManagers they'll end up living in this michael@0: // module... they have to coexist. michael@0: #if !defined(COMPONENT_BUILD) michael@0: DCHECK(!g_top_manager); michael@0: #endif michael@0: g_top_manager = this; michael@0: } michael@0: michael@0: AtExitManager::~AtExitManager() { michael@0: if (!g_top_manager) { michael@0: NOTREACHED() << "Tried to ~AtExitManager without an AtExitManager"; michael@0: return; michael@0: } michael@0: DCHECK_EQ(this, g_top_manager); michael@0: michael@0: ProcessCallbacksNow(); michael@0: g_top_manager = next_manager_; michael@0: } michael@0: michael@0: // static michael@0: void AtExitManager::RegisterCallback(AtExitCallbackType func, void* param) { michael@0: DCHECK(func); michael@0: RegisterTask(base::Bind(func, param)); michael@0: } michael@0: michael@0: // static michael@0: void AtExitManager::RegisterTask(base::Closure task) { michael@0: if (!g_top_manager) { michael@0: NOTREACHED() << "Tried to RegisterCallback without an AtExitManager"; michael@0: return; michael@0: } michael@0: michael@0: AutoLock lock(g_top_manager->lock_); michael@0: g_top_manager->stack_.push(task); michael@0: } michael@0: michael@0: // static michael@0: void AtExitManager::ProcessCallbacksNow() { michael@0: if (!g_top_manager) { michael@0: NOTREACHED() << "Tried to ProcessCallbacksNow without an AtExitManager"; michael@0: return; michael@0: } michael@0: michael@0: AutoLock lock(g_top_manager->lock_); michael@0: michael@0: while (!g_top_manager->stack_.empty()) { michael@0: base::Closure task = g_top_manager->stack_.top(); michael@0: task.Run(); michael@0: g_top_manager->stack_.pop(); michael@0: } michael@0: } michael@0: michael@0: AtExitManager::AtExitManager(bool shadow) : next_manager_(g_top_manager) { michael@0: DCHECK(shadow || !g_top_manager); michael@0: g_top_manager = this; michael@0: } michael@0: michael@0: } // namespace base