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