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
3 // found in the LICENSE file.
5 #include "base/at_exit.h"
10 #include "base/logging.h"
14 // Keep a stack of registered AtExitManagers. We always operate on the most
15 // recent, and we should never have more than one outside of testing, when we
16 // use the shadow version of the constructor. We don't protect this for
17 // thread-safe access, since it will only be modified in testing.
18 static AtExitManager
* g_top_manager
= NULL
;
20 AtExitManager::AtExitManager() : next_manager_(NULL
) {
21 DCHECK(!g_top_manager
);
25 AtExitManager::~AtExitManager() {
27 NOTREACHED() << "Tried to ~AtExitManager without an AtExitManager";
30 DCHECK(g_top_manager
== this);
32 ProcessCallbacksNow();
33 g_top_manager
= next_manager_
;
37 void AtExitManager::RegisterCallback(AtExitCallbackType func
, void* param
) {
39 NOTREACHED() << "Tried to RegisterCallback without an AtExitManager";
45 AutoLock
lock(g_top_manager
->lock_
);
46 g_top_manager
->stack_
.push(CallbackAndParam(func
, param
));
50 void AtExitManager::ProcessCallbacksNow() {
52 NOTREACHED() << "Tried to ProcessCallbacksNow without an AtExitManager";
56 AutoLock
lock(g_top_manager
->lock_
);
58 while (!g_top_manager
->stack_
.empty()) {
59 CallbackAndParam callback_and_param
= g_top_manager
->stack_
.top();
60 g_top_manager
->stack_
.pop();
62 callback_and_param
.func_(callback_and_param
.param_
);
66 AtExitManager::AtExitManager(bool shadow
) : next_manager_(g_top_manager
) {
67 DCHECK(shadow
|| !g_top_manager
);