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/. */
7 #include "mozilla/ClearOnShutdown.h"
10 namespace ClearOnShutdown_Internal
{
12 Array
<StaticAutoPtr
<ShutdownList
>,
13 static_cast<size_t>(ShutdownPhase::ShutdownPhase_Length
)>
15 ShutdownPhase sCurrentClearOnShutdownPhase
= ShutdownPhase::NotInShutdown
;
17 void 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();
26 if (!(sShutdownObservers
[static_cast<size_t>(aPhase
)])) {
27 sShutdownObservers
[static_cast<size_t>(aPhase
)] = new ShutdownList();
29 sShutdownObservers
[static_cast<size_t>(aPhase
)]->insertBack(aObserver
);
32 } // namespace ClearOnShutdown_Internal
34 // Called by AdvanceShutdownPhase each time we switch a phase. Will null out
35 // pointers added by ClearOnShutdown for all phases up to and including aPhase.
36 void KillClearOnShutdown(ShutdownPhase aPhase
) {
37 using namespace ClearOnShutdown_Internal
;
39 MOZ_ASSERT(NS_IsMainThread());
40 // Shutdown only goes one direction...
41 MOZ_ASSERT(!PastShutdownPhase(aPhase
));
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
;
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()) {
58 sShutdownObservers
[static_cast<size_t>(phase
)] = nullptr;
63 } // namespace mozilla