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