michael@0: // Copyright (c) 2006-2008 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: #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, when we michael@0: // use the shadow version of the constructor. We don't protect this for michael@0: // 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_(NULL) { michael@0: DCHECK(!g_top_manager); michael@0: g_top_manager = this; 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: 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(g_top_manager == this); 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: if (!g_top_manager) { michael@0: NOTREACHED() << "Tried to RegisterCallback without an AtExitManager"; michael@0: return; michael@0: } michael@0: michael@0: DCHECK(func); michael@0: michael@0: AutoLock lock(g_top_manager->lock_); michael@0: g_top_manager->stack_.push(CallbackAndParam(func, param)); 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: CallbackAndParam callback_and_param = g_top_manager->stack_.top(); michael@0: g_top_manager->stack_.pop(); michael@0: michael@0: callback_and_param.func_(callback_and_param.param_); michael@0: } michael@0: } michael@0: michael@0: // static michael@0: bool AtExitManager::AlreadyRegistered() { michael@0: return !!g_top_manager; michael@0: } michael@0: michael@0: } // namespace base