2011-12-15 07:51:11 -08: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 04:12:37 -07: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 07:51:11 -08:00
|
|
|
|
|
|
|
#ifndef mozilla_ClearOnShutdown_h
|
2012-01-26 12:54:03 -08:00
|
|
|
#define mozilla_ClearOnShutdown_h
|
2011-12-15 07:51:11 -08:00
|
|
|
|
2012-01-26 12:54:03 -08:00
|
|
|
#include "mozilla/LinkedList.h"
|
2012-10-31 10:30:35 -07:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2013-09-19 06:54:42 -07:00
|
|
|
#include "MainThreadUtils.h"
|
2011-12-15 07:51:11 -08:00
|
|
|
|
|
|
|
/*
|
2012-01-26 12:54:03 -08:00
|
|
|
* This header exports one public method in the mozilla namespace:
|
2011-12-15 07:51:11 -08:00
|
|
|
*
|
|
|
|
* template<class SmartPtr>
|
|
|
|
* void ClearOnShutdown(SmartPtr *aPtr)
|
|
|
|
*
|
2012-07-26 12:33:45 -07:00
|
|
|
* This function takes a pointer to a smart pointer and nulls the smart pointer
|
|
|
|
* on shutdown.
|
2011-12-15 07:51:11 -08:00
|
|
|
*
|
|
|
|
* This is useful if you have a global smart pointer object which you don't
|
|
|
|
* want to "leak" on shutdown.
|
|
|
|
*
|
2012-07-26 12:33:45 -07: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 12:58:10 -07: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 07:51:11 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ClearOnShutdown_Internal {
|
|
|
|
|
2012-01-26 12:54:03 -08:00
|
|
|
class ShutdownObserver : public LinkedListElement<ShutdownObserver>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void Shutdown() = 0;
|
2012-06-19 15:23:35 -07:00
|
|
|
virtual ~ShutdownObserver() {}
|
2012-01-26 12:54:03 -08:00
|
|
|
};
|
|
|
|
|
2011-12-15 07:51:11 -08:00
|
|
|
template<class SmartPtr>
|
2012-01-26 12:54:03 -08:00
|
|
|
class PointerClearer : public ShutdownObserver
|
2011-12-15 07:51:11 -08:00
|
|
|
{
|
|
|
|
public:
|
2012-01-26 12:54:03 -08:00
|
|
|
PointerClearer(SmartPtr *aPtr)
|
2011-12-15 07:51:11 -08:00
|
|
|
: mPtr(aPtr)
|
|
|
|
{}
|
|
|
|
|
2012-01-26 12:54:03 -08:00
|
|
|
virtual void Shutdown()
|
2011-12-15 07:51:11 -08:00
|
|
|
{
|
|
|
|
if (mPtr) {
|
2013-10-10 13:41:00 -07:00
|
|
|
*mPtr = nullptr;
|
2011-12-15 07:51:11 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SmartPtr *mPtr;
|
|
|
|
};
|
|
|
|
|
2012-01-26 12:54:03 -08:00
|
|
|
extern bool sHasShutDown;
|
2012-10-31 10:30:35 -07:00
|
|
|
extern StaticAutoPtr<LinkedList<ShutdownObserver> > sShutdownObservers;
|
2011-12-19 12:49:04 -08:00
|
|
|
|
2012-01-26 12:54:03 -08:00
|
|
|
} // namespace ClearOnShutdown_Internal
|
2011-12-19 12:49:04 -08:00
|
|
|
|
2011-12-15 07:51:11 -08:00
|
|
|
template<class SmartPtr>
|
2012-01-26 12:54:03 -08:00
|
|
|
inline void ClearOnShutdown(SmartPtr *aPtr)
|
|
|
|
{
|
|
|
|
using namespace ClearOnShutdown_Internal;
|
2011-12-15 07:51:11 -08:00
|
|
|
|
2012-04-10 12:58:10 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-01-26 12:54:03 -08:00
|
|
|
MOZ_ASSERT(!sHasShutDown);
|
2012-10-31 10:30:35 -07:00
|
|
|
|
|
|
|
if (!sShutdownObservers) {
|
|
|
|
sShutdownObservers = new LinkedList<ShutdownObserver>();
|
|
|
|
}
|
|
|
|
sShutdownObservers->insertBack(new PointerClearer<SmartPtr>(aPtr));
|
2012-01-26 12:54:03 -08:00
|
|
|
}
|
2011-12-15 07:51:11 -08:00
|
|
|
|
2012-01-26 12:54:03 -08: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 07:51:11 -08:00
|
|
|
{
|
2012-01-26 12:54:03 -08:00
|
|
|
using namespace ClearOnShutdown_Internal;
|
2011-12-15 07:51:11 -08:00
|
|
|
|
2012-04-10 12:58:10 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-10-31 12:19:18 -07:00
|
|
|
if (sShutdownObservers) {
|
|
|
|
ShutdownObserver *observer;
|
|
|
|
while ((observer = sShutdownObservers->popFirst())) {
|
|
|
|
observer->Shutdown();
|
|
|
|
delete observer;
|
|
|
|
}
|
2011-12-15 07:51:11 -08:00
|
|
|
}
|
2012-01-26 12:54:03 -08:00
|
|
|
|
2012-10-31 10:30:35 -07:00
|
|
|
sShutdownObservers = nullptr;
|
2012-01-26 12:54:03 -08:00
|
|
|
sHasShutDown = true;
|
2011-12-15 07:51:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif
|