ipc/chromium/src/base/at_exit.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include "base/at_exit.h"
michael@0 6 #include "base/logging.h"
michael@0 7
michael@0 8 namespace base {
michael@0 9
michael@0 10 // Keep a stack of registered AtExitManagers. We always operate on the most
michael@0 11 // recent, and we should never have more than one outside of testing, when we
michael@0 12 // use the shadow version of the constructor. We don't protect this for
michael@0 13 // thread-safe access, since it will only be modified in testing.
michael@0 14 static AtExitManager* g_top_manager = NULL;
michael@0 15
michael@0 16 AtExitManager::AtExitManager() : next_manager_(NULL) {
michael@0 17 DCHECK(!g_top_manager);
michael@0 18 g_top_manager = this;
michael@0 19 }
michael@0 20
michael@0 21 AtExitManager::AtExitManager(bool shadow) : next_manager_(g_top_manager) {
michael@0 22 DCHECK(shadow || !g_top_manager);
michael@0 23 g_top_manager = this;
michael@0 24 }
michael@0 25
michael@0 26 AtExitManager::~AtExitManager() {
michael@0 27 if (!g_top_manager) {
michael@0 28 NOTREACHED() << "Tried to ~AtExitManager without an AtExitManager";
michael@0 29 return;
michael@0 30 }
michael@0 31 DCHECK(g_top_manager == this);
michael@0 32
michael@0 33 ProcessCallbacksNow();
michael@0 34 g_top_manager = next_manager_;
michael@0 35 }
michael@0 36
michael@0 37 // static
michael@0 38 void AtExitManager::RegisterCallback(AtExitCallbackType func, void* param) {
michael@0 39 if (!g_top_manager) {
michael@0 40 NOTREACHED() << "Tried to RegisterCallback without an AtExitManager";
michael@0 41 return;
michael@0 42 }
michael@0 43
michael@0 44 DCHECK(func);
michael@0 45
michael@0 46 AutoLock lock(g_top_manager->lock_);
michael@0 47 g_top_manager->stack_.push(CallbackAndParam(func, param));
michael@0 48 }
michael@0 49
michael@0 50 // static
michael@0 51 void AtExitManager::ProcessCallbacksNow() {
michael@0 52 if (!g_top_manager) {
michael@0 53 NOTREACHED() << "Tried to ProcessCallbacksNow without an AtExitManager";
michael@0 54 return;
michael@0 55 }
michael@0 56
michael@0 57 AutoLock lock(g_top_manager->lock_);
michael@0 58
michael@0 59 while (!g_top_manager->stack_.empty()) {
michael@0 60 CallbackAndParam callback_and_param = g_top_manager->stack_.top();
michael@0 61 g_top_manager->stack_.pop();
michael@0 62
michael@0 63 callback_and_param.func_(callback_and_param.param_);
michael@0 64 }
michael@0 65 }
michael@0 66
michael@0 67 // static
michael@0 68 bool AtExitManager::AlreadyRegistered() {
michael@0 69 return !!g_top_manager;
michael@0 70 }
michael@0 71
michael@0 72 } // namespace base

mercurial