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.

     1 // 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.
     5 #include "base/at_exit.h"
     6 #include "base/logging.h"
     8 namespace base {
    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.
    14 static AtExitManager* g_top_manager = NULL;
    16 AtExitManager::AtExitManager() : next_manager_(NULL) {
    17   DCHECK(!g_top_manager);
    18   g_top_manager = this;
    19 }
    21 AtExitManager::AtExitManager(bool shadow) : next_manager_(g_top_manager) {
    22   DCHECK(shadow || !g_top_manager);
    23   g_top_manager = this;
    24 }
    26 AtExitManager::~AtExitManager() {
    27   if (!g_top_manager) {
    28     NOTREACHED() << "Tried to ~AtExitManager without an AtExitManager";
    29     return;
    30   }
    31   DCHECK(g_top_manager == this);
    33   ProcessCallbacksNow();
    34   g_top_manager = next_manager_;
    35 }
    37 // static
    38 void AtExitManager::RegisterCallback(AtExitCallbackType func, void* param) {
    39   if (!g_top_manager) {
    40     NOTREACHED() << "Tried to RegisterCallback without an AtExitManager";
    41     return;
    42   }
    44   DCHECK(func);
    46   AutoLock lock(g_top_manager->lock_);
    47   g_top_manager->stack_.push(CallbackAndParam(func, param));
    48 }
    50 // static
    51 void AtExitManager::ProcessCallbacksNow() {
    52   if (!g_top_manager) {
    53     NOTREACHED() << "Tried to ProcessCallbacksNow without an AtExitManager";
    54     return;
    55   }
    57   AutoLock lock(g_top_manager->lock_);
    59   while (!g_top_manager->stack_.empty()) {
    60     CallbackAndParam callback_and_param = g_top_manager->stack_.top();
    61     g_top_manager->stack_.pop();
    63     callback_and_param.func_(callback_and_param.param_);
    64   }
    65 }
    67 // static
    68 bool AtExitManager::AlreadyRegistered() {
    69   return !!g_top_manager;
    70 }
    72 }  // namespace base

mercurial