Bug 1171716 - Part 1: Add NS_ReleaseOnMainThread. r=froydnj

This commit is contained in:
Eric Rahm 2015-06-10 08:33:27 -07:00
parent f0273ac437
commit 353c3fadf4

View File

@ -57,7 +57,7 @@ NS_ProxyRelease(nsIEventTarget* aTarget, nsRefPtr<T>& aDoomed,
* the doomed object; the object to be released on the target thread.
* @param aAlwaysProxy
* normally, if NS_ProxyRelease is called on the target thread, then the
* doomed object will released directly. however, if this parameter is
* doomed object will be released directly. However, if this parameter is
* true, then an event will always be posted to the target thread for
* asynchronous release.
*/
@ -65,6 +65,62 @@ nsresult
NS_ProxyRelease(nsIEventTarget* aTarget, nsISupports* aDoomed,
bool aAlwaysProxy = false);
/**
* Ensure that a nsCOMPtr is released on the main thread.
*
* @see NS_ReleaseOnMainThread( nsISupports*, bool)
*/
template<class T>
inline NS_HIDDEN_(nsresult)
NS_ReleaseOnMainThread(nsCOMPtr<T>& aDoomed,
bool aAlwaysProxy = false)
{
T* raw = nullptr;
aDoomed.swap(raw);
return NS_ReleaseOnMainThread(raw, aAlwaysProxy);
}
/**
* Ensure that a nsRefPtr is released on the main thread.
*
* @see NS_ReleaseOnMainThread(nsISupports*, bool)
*/
template<class T>
inline NS_HIDDEN_(nsresult)
NS_ReleaseOnMainThread(nsRefPtr<T>& aDoomed,
bool aAlwaysProxy = false)
{
T* raw = nullptr;
aDoomed.swap(raw);
return NS_ReleaseOnMainThread(raw, aAlwaysProxy);
}
/**
* Ensures that the delete of a nsISupports object occurs on the main thread.
*
* @param aDoomed
* the doomed object; the object to be released on the main thread.
* @param aAlwaysProxy
* normally, if NS_ReleaseOnMainThread is called on the main thread,
* then the doomed object will be released directly. However, if this
* parameter is true, then an event will always be posted to the main
* thread for asynchronous release.
*/
inline nsresult
NS_ReleaseOnMainThread(nsISupports* aDoomed,
bool aAlwaysProxy = false)
{
// NS_ProxyRelease treats a null event target as "the current thread". So a
// handle on the main thread is only necessary when we're not already on the
// main thread or the release must happen asynchronously.
nsCOMPtr<nsIThread> mainThread;
if (!NS_IsMainThread() || aAlwaysProxy) {
NS_GetMainThread(getter_AddRefs(mainThread));
}
return NS_ProxyRelease(mainThread, aDoomed, aAlwaysProxy);
}
/**
* Class to safely handle main-thread-only pointers off the main thread.
*