1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/ClearOnShutdown.h" 8 9 namespace mozilla { 10 namespace ClearOnShutdown_Internal { 11 12 Array<StaticAutoPtr<ShutdownList>, 13 static_cast<size_t>(ShutdownPhase::ShutdownPhase_Length)> 14 sShutdownObservers; 15 ShutdownPhase sCurrentClearOnShutdownPhase = ShutdownPhase::NotInShutdown; 16 InsertIntoShutdownList(ShutdownObserver * aObserver,ShutdownPhase aPhase)17void InsertIntoShutdownList(ShutdownObserver* aObserver, ShutdownPhase aPhase) { 18 // Adding a ClearOnShutdown for a "past" phase is an error. 19 if (PastShutdownPhase(aPhase)) { 20 MOZ_ASSERT(false, "ClearOnShutdown for phase that already was cleared"); 21 aObserver->Shutdown(); 22 delete aObserver; 23 return; 24 } 25 26 if (!(sShutdownObservers[static_cast<size_t>(aPhase)])) { 27 sShutdownObservers[static_cast<size_t>(aPhase)] = new ShutdownList(); 28 } 29 sShutdownObservers[static_cast<size_t>(aPhase)]->insertBack(aObserver); 30 } 31 32 } // namespace ClearOnShutdown_Internal 33 34 // Called when XPCOM is shutting down, after all shutdown notifications have 35 // been sent and after all threads' event loops have been purged. KillClearOnShutdown(ShutdownPhase aPhase)36void KillClearOnShutdown(ShutdownPhase aPhase) { 37 using namespace ClearOnShutdown_Internal; 38 39 MOZ_ASSERT(NS_IsMainThread()); 40 // Shutdown only goes one direction... 41 MOZ_ASSERT(!PastShutdownPhase(aPhase)); 42 43 // Set the phase before notifying observers to make sure that they can't run 44 // any code which isn't allowed to run after the start of this phase. 45 sCurrentClearOnShutdownPhase = aPhase; 46 47 // It's impossible to add an entry for a "past" phase; this is blocked in 48 // ClearOnShutdown, but clear them out anyways in case there are phases 49 // that weren't passed to KillClearOnShutdown. 50 for (size_t phase = static_cast<size_t>(ShutdownPhase::First); 51 phase <= static_cast<size_t>(aPhase); phase++) { 52 if (sShutdownObservers[static_cast<size_t>(phase)]) { 53 while (ShutdownObserver* observer = 54 sShutdownObservers[static_cast<size_t>(phase)]->popLast()) { 55 observer->Shutdown(); 56 delete observer; 57 } 58 sShutdownObservers[static_cast<size_t>(phase)] = nullptr; 59 } 60 } 61 } 62 63 } // namespace mozilla 64