2011-12-15 19:51:11 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set sw=2 ts=8 et ft=cpp : */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-12-15 19:51:11 +04:00
|
|
|
|
|
|
|
#ifndef mozilla_ClearOnShutdown_h
|
2012-01-27 00:54:03 +04:00
|
|
|
#define mozilla_ClearOnShutdown_h
|
2011-12-15 19:51:11 +04:00
|
|
|
|
2012-01-27 00:54:03 +04:00
|
|
|
#include "mozilla/LinkedList.h"
|
2012-10-31 21:30:35 +04:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2013-09-19 17:54:42 +04:00
|
|
|
#include "MainThreadUtils.h"
|
2011-12-15 19:51:11 +04:00
|
|
|
|
|
|
|
/*
|
2012-01-27 00:54:03 +04:00
|
|
|
* This header exports one public method in the mozilla namespace:
|
2011-12-15 19:51:11 +04:00
|
|
|
*
|
|
|
|
* template<class SmartPtr>
|
|
|
|
* void ClearOnShutdown(SmartPtr *aPtr)
|
|
|
|
*
|
2012-07-26 23:33:45 +04:00
|
|
|
* This function takes a pointer to a smart pointer and nulls the smart pointer
|
|
|
|
* on shutdown.
|
2011-12-15 19:51:11 +04:00
|
|
|
*
|
|
|
|
* This is useful if you have a global smart pointer object which you don't
|
|
|
|
* want to "leak" on shutdown.
|
|
|
|
*
|
2012-07-26 23:33:45 +04:00
|
|
|
* Although ClearOnShutdown will work with any smart pointer (i.e., nsCOMPtr,
|
|
|
|
* nsRefPtr, nsAutoPtr, StaticRefPtr, and StaticAutoPtr), you probably want to
|
|
|
|
* use it only with StaticRefPtr and StaticAutoPtr. There is no way to undo a
|
|
|
|
* call to ClearOnShutdown, so you can call it only on smart pointers which you
|
|
|
|
* know will live until the program shuts down. In practice, these are likely
|
|
|
|
* global variables, which should be Static{Ref,Auto}Ptr.
|
2012-04-10 23:58:10 +04:00
|
|
|
*
|
|
|
|
* ClearOnShutdown is currently main-thread only because we don't want to
|
|
|
|
* accidentally free an object from a different thread than the one it was
|
|
|
|
* created on.
|
2011-12-15 19:51:11 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ClearOnShutdown_Internal {
|
|
|
|
|
2012-01-27 00:54:03 +04:00
|
|
|
class ShutdownObserver : public LinkedListElement<ShutdownObserver>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void Shutdown() = 0;
|
2012-06-20 02:23:35 +04:00
|
|
|
virtual ~ShutdownObserver() {}
|
2012-01-27 00:54:03 +04:00
|
|
|
};
|
|
|
|
|
2011-12-15 19:51:11 +04:00
|
|
|
template<class SmartPtr>
|
2012-01-27 00:54:03 +04:00
|
|
|
class PointerClearer : public ShutdownObserver
|
2011-12-15 19:51:11 +04:00
|
|
|
{
|
|
|
|
public:
|
2012-01-27 00:54:03 +04:00
|
|
|
PointerClearer(SmartPtr *aPtr)
|
2011-12-15 19:51:11 +04:00
|
|
|
: mPtr(aPtr)
|
|
|
|
{}
|
|
|
|
|
2012-01-27 00:54:03 +04:00
|
|
|
virtual void Shutdown()
|
2011-12-15 19:51:11 +04:00
|
|
|
{
|
|
|
|
if (mPtr) {
|
2013-10-11 00:41:00 +04:00
|
|
|
*mPtr = nullptr;
|
2011-12-15 19:51:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SmartPtr *mPtr;
|
|
|
|
};
|
|
|
|
|
2012-01-27 00:54:03 +04:00
|
|
|
extern bool sHasShutDown;
|
2012-10-31 21:30:35 +04:00
|
|
|
extern StaticAutoPtr<LinkedList<ShutdownObserver> > sShutdownObservers;
|
2011-12-20 00:49:04 +04:00
|
|
|
|
2012-01-27 00:54:03 +04:00
|
|
|
} // namespace ClearOnShutdown_Internal
|
2011-12-20 00:49:04 +04:00
|
|
|
|
2011-12-15 19:51:11 +04:00
|
|
|
template<class SmartPtr>
|
2012-01-27 00:54:03 +04:00
|
|
|
inline void ClearOnShutdown(SmartPtr *aPtr)
|
|
|
|
{
|
|
|
|
using namespace ClearOnShutdown_Internal;
|
2011-12-15 19:51:11 +04:00
|
|
|
|
2012-04-10 23:58:10 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-01-27 00:54:03 +04:00
|
|
|
MOZ_ASSERT(!sHasShutDown);
|
2012-10-31 21:30:35 +04:00
|
|
|
|
|
|
|
if (!sShutdownObservers) {
|
|
|
|
sShutdownObservers = new LinkedList<ShutdownObserver>();
|
|
|
|
}
|
|
|
|
sShutdownObservers->insertBack(new PointerClearer<SmartPtr>(aPtr));
|
2012-01-27 00:54:03 +04:00
|
|
|
}
|
2011-12-15 19:51:11 +04:00
|
|
|
|
2012-01-27 00:54:03 +04:00
|
|
|
// Called when XPCOM is shutting down, after all shutdown notifications have
|
|
|
|
// been sent and after all threads' event loops have been purged.
|
|
|
|
inline void KillClearOnShutdown()
|
2011-12-15 19:51:11 +04:00
|
|
|
{
|
2012-01-27 00:54:03 +04:00
|
|
|
using namespace ClearOnShutdown_Internal;
|
2011-12-15 19:51:11 +04:00
|
|
|
|
2012-04-10 23:58:10 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-10-31 23:19:18 +04:00
|
|
|
if (sShutdownObservers) {
|
|
|
|
ShutdownObserver *observer;
|
|
|
|
while ((observer = sShutdownObservers->popFirst())) {
|
|
|
|
observer->Shutdown();
|
|
|
|
delete observer;
|
|
|
|
}
|
2011-12-15 19:51:11 +04:00
|
|
|
}
|
2012-01-27 00:54:03 +04:00
|
|
|
|
2012-10-31 21:30:35 +04:00
|
|
|
sShutdownObservers = nullptr;
|
2012-01-27 00:54:03 +04:00
|
|
|
sHasShutDown = true;
|
2011-12-15 19:51:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif
|