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 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set sw=2 ts=8 et ft=cpp : */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef mozilla_ClearOnShutdown_h |
michael@0 | 8 | #define mozilla_ClearOnShutdown_h |
michael@0 | 9 | |
michael@0 | 10 | #include "mozilla/LinkedList.h" |
michael@0 | 11 | #include "mozilla/StaticPtr.h" |
michael@0 | 12 | #include "MainThreadUtils.h" |
michael@0 | 13 | |
michael@0 | 14 | /* |
michael@0 | 15 | * This header exports one public method in the mozilla namespace: |
michael@0 | 16 | * |
michael@0 | 17 | * template<class SmartPtr> |
michael@0 | 18 | * void ClearOnShutdown(SmartPtr *aPtr) |
michael@0 | 19 | * |
michael@0 | 20 | * This function takes a pointer to a smart pointer and nulls the smart pointer |
michael@0 | 21 | * on shutdown. |
michael@0 | 22 | * |
michael@0 | 23 | * This is useful if you have a global smart pointer object which you don't |
michael@0 | 24 | * want to "leak" on shutdown. |
michael@0 | 25 | * |
michael@0 | 26 | * Although ClearOnShutdown will work with any smart pointer (i.e., nsCOMPtr, |
michael@0 | 27 | * nsRefPtr, nsAutoPtr, StaticRefPtr, and StaticAutoPtr), you probably want to |
michael@0 | 28 | * use it only with StaticRefPtr and StaticAutoPtr. There is no way to undo a |
michael@0 | 29 | * call to ClearOnShutdown, so you can call it only on smart pointers which you |
michael@0 | 30 | * know will live until the program shuts down. In practice, these are likely |
michael@0 | 31 | * global variables, which should be Static{Ref,Auto}Ptr. |
michael@0 | 32 | * |
michael@0 | 33 | * ClearOnShutdown is currently main-thread only because we don't want to |
michael@0 | 34 | * accidentally free an object from a different thread than the one it was |
michael@0 | 35 | * created on. |
michael@0 | 36 | */ |
michael@0 | 37 | |
michael@0 | 38 | namespace mozilla { |
michael@0 | 39 | namespace ClearOnShutdown_Internal { |
michael@0 | 40 | |
michael@0 | 41 | class ShutdownObserver : public LinkedListElement<ShutdownObserver> |
michael@0 | 42 | { |
michael@0 | 43 | public: |
michael@0 | 44 | virtual void Shutdown() = 0; |
michael@0 | 45 | virtual ~ShutdownObserver() {} |
michael@0 | 46 | }; |
michael@0 | 47 | |
michael@0 | 48 | template<class SmartPtr> |
michael@0 | 49 | class PointerClearer : public ShutdownObserver |
michael@0 | 50 | { |
michael@0 | 51 | public: |
michael@0 | 52 | PointerClearer(SmartPtr *aPtr) |
michael@0 | 53 | : mPtr(aPtr) |
michael@0 | 54 | {} |
michael@0 | 55 | |
michael@0 | 56 | virtual void Shutdown() |
michael@0 | 57 | { |
michael@0 | 58 | if (mPtr) { |
michael@0 | 59 | *mPtr = nullptr; |
michael@0 | 60 | } |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | private: |
michael@0 | 64 | SmartPtr *mPtr; |
michael@0 | 65 | }; |
michael@0 | 66 | |
michael@0 | 67 | extern bool sHasShutDown; |
michael@0 | 68 | extern StaticAutoPtr<LinkedList<ShutdownObserver> > sShutdownObservers; |
michael@0 | 69 | |
michael@0 | 70 | } // namespace ClearOnShutdown_Internal |
michael@0 | 71 | |
michael@0 | 72 | template<class SmartPtr> |
michael@0 | 73 | inline void ClearOnShutdown(SmartPtr *aPtr) |
michael@0 | 74 | { |
michael@0 | 75 | using namespace ClearOnShutdown_Internal; |
michael@0 | 76 | |
michael@0 | 77 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 78 | MOZ_ASSERT(!sHasShutDown); |
michael@0 | 79 | |
michael@0 | 80 | if (!sShutdownObservers) { |
michael@0 | 81 | sShutdownObservers = new LinkedList<ShutdownObserver>(); |
michael@0 | 82 | } |
michael@0 | 83 | sShutdownObservers->insertBack(new PointerClearer<SmartPtr>(aPtr)); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | // Called when XPCOM is shutting down, after all shutdown notifications have |
michael@0 | 87 | // been sent and after all threads' event loops have been purged. |
michael@0 | 88 | inline void KillClearOnShutdown() |
michael@0 | 89 | { |
michael@0 | 90 | using namespace ClearOnShutdown_Internal; |
michael@0 | 91 | |
michael@0 | 92 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 93 | |
michael@0 | 94 | if (sShutdownObservers) { |
michael@0 | 95 | ShutdownObserver *observer; |
michael@0 | 96 | while ((observer = sShutdownObservers->popFirst())) { |
michael@0 | 97 | observer->Shutdown(); |
michael@0 | 98 | delete observer; |
michael@0 | 99 | } |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | sShutdownObservers = nullptr; |
michael@0 | 103 | sHasShutDown = true; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | } // namespace mozilla |
michael@0 | 107 | |
michael@0 | 108 | #endif |