blob: 9fdfd77d9e8998582004d8aa009026b972cbaed6 [file] [log] [blame]
mmentovai@google.comaa13be62008-09-03 03:20:34 +09001// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
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"
6#include "base/logging.h"
7
8namespace base {
9
10// Keep a stack of registered AtExitManagers. We always operate on the most
11// recent, and we should never have more than one outside of testing, when we
12// use the shadow version of the constructor. We don't protect this for
13// thread-safe access, since it will only be modified in testing.
14static AtExitManager* g_top_manager = NULL;
15
16AtExitManager::AtExitManager() : next_manager_(NULL) {
17 DCHECK(!g_top_manager);
18 g_top_manager = this;
19}
20
21AtExitManager::AtExitManager(bool shadow) : next_manager_(g_top_manager) {
22 DCHECK(shadow || !g_top_manager);
23 g_top_manager = this;
24}
25
26AtExitManager::~AtExitManager() {
27 if (!g_top_manager) {
28 NOTREACHED() << "Tried to ~AtExitManager without an AtExitManager";
29 return;
30 }
31 DCHECK(g_top_manager == this);
32
33 ProcessCallbacksNow();
34 g_top_manager = next_manager_;
35}
36
37// static
deanm@google.comf6299e72008-09-08 18:06:51 +090038void AtExitManager::RegisterCallback(AtExitCallbackType func, void* param) {
mmentovai@google.comaa13be62008-09-03 03:20:34 +090039 if (!g_top_manager) {
40 NOTREACHED() << "Tried to RegisterCallback without an AtExitManager";
41 return;
42 }
43
deanm@google.comf6299e72008-09-08 18:06:51 +090044 DCHECK(func);
45
mmentovai@google.comaa13be62008-09-03 03:20:34 +090046 AutoLock lock(g_top_manager->lock_);
deanm@google.comf6299e72008-09-08 18:06:51 +090047 g_top_manager->stack_.push(CallbackAndParam(func, param));
mmentovai@google.comaa13be62008-09-03 03:20:34 +090048}
49
50// static
51void AtExitManager::ProcessCallbacksNow() {
52 if (!g_top_manager) {
53 NOTREACHED() << "Tried to ProcessCallbacksNow without an AtExitManager";
54 return;
55 }
56
57 AutoLock lock(g_top_manager->lock_);
58
59 while (!g_top_manager->stack_.empty()) {
deanm@google.comf6299e72008-09-08 18:06:51 +090060 CallbackAndParam callback_and_param = g_top_manager->stack_.top();
mmentovai@google.comaa13be62008-09-03 03:20:34 +090061 g_top_manager->stack_.pop();
deanm@google.comf6299e72008-09-08 18:06:51 +090062
63 callback_and_param.func_(callback_and_param.param_);
mmentovai@google.comaa13be62008-09-03 03:20:34 +090064 }
65}
66
67} // namespace base