security/sandbox/chromium/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) 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"
     7 #include <stddef.h>
     8 #include <ostream>
    10 #include "base/bind.h"
    11 #include "base/callback.h"
    12 #include "base/logging.h"
    14 namespace base {
    16 // Keep a stack of registered AtExitManagers.  We always operate on the most
    17 // recent, and we should never have more than one outside of testing (for a
    18 // statically linked version of this library).  Testing may use the shadow
    19 // version of the constructor, and if we are building a dynamic library we may
    20 // end up with multiple AtExitManagers on the same process.  We don't protect
    21 // this for thread-safe access, since it will only be modified in testing.
    22 static AtExitManager* g_top_manager = NULL;
    24 AtExitManager::AtExitManager() : next_manager_(g_top_manager) {
    25 // If multiple modules instantiate AtExitManagers they'll end up living in this
    26 // module... they have to coexist.
    27 #if !defined(COMPONENT_BUILD)
    28   DCHECK(!g_top_manager);
    29 #endif
    30   g_top_manager = this;
    31 }
    33 AtExitManager::~AtExitManager() {
    34   if (!g_top_manager) {
    35     NOTREACHED() << "Tried to ~AtExitManager without an AtExitManager";
    36     return;
    37   }
    38   DCHECK_EQ(this, g_top_manager);
    40   ProcessCallbacksNow();
    41   g_top_manager = next_manager_;
    42 }
    44 // static
    45 void AtExitManager::RegisterCallback(AtExitCallbackType func, void* param) {
    46   DCHECK(func);
    47   RegisterTask(base::Bind(func, param));
    48 }
    50 // static
    51 void AtExitManager::RegisterTask(base::Closure task) {
    52   if (!g_top_manager) {
    53     NOTREACHED() << "Tried to RegisterCallback without an AtExitManager";
    54     return;
    55   }
    57   AutoLock lock(g_top_manager->lock_);
    58   g_top_manager->stack_.push(task);
    59 }
    61 // static
    62 void AtExitManager::ProcessCallbacksNow() {
    63   if (!g_top_manager) {
    64     NOTREACHED() << "Tried to ProcessCallbacksNow without an AtExitManager";
    65     return;
    66   }
    68   AutoLock lock(g_top_manager->lock_);
    70   while (!g_top_manager->stack_.empty()) {
    71     base::Closure task = g_top_manager->stack_.top();
    72     task.Run();
    73     g_top_manager->stack_.pop();
    74   }
    75 }
    77 AtExitManager::AtExitManager(bool shadow) : next_manager_(g_top_manager) {
    78   DCHECK(shadow || !g_top_manager);
    79   g_top_manager = this;
    80 }
    82 }  // namespace base

mercurial