2014-06-30 15:39:45 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00: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/. */
|
2002-05-15 18:55:21 +00:00
|
|
|
|
2009-03-12 03:22:48 +00:00
|
|
|
#ifndef nsProxyRelease_h__
|
2002-05-15 18:55:21 +00:00
|
|
|
#define nsProxyRelease_h__
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
#include "nsIEventTarget.h"
|
2013-09-24 20:45:13 +00:00
|
|
|
#include "nsIThread.h"
|
2006-05-22 14:19:13 +00:00
|
|
|
#include "nsCOMPtr.h"
|
2010-11-16 00:08:57 +00:00
|
|
|
#include "nsAutoPtr.h"
|
2013-09-19 13:54:42 +00:00
|
|
|
#include "MainThreadUtils.h"
|
2012-10-26 13:32:10 +00:00
|
|
|
#include "mozilla/Likely.h"
|
2006-05-22 14:19:13 +00:00
|
|
|
|
|
|
|
#ifdef XPCOM_GLUE_AVOID_NSPR
|
|
|
|
#error NS_ProxyRelease implementation depends on NSPR.
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that a nsCOMPtr is released on the target thread.
|
|
|
|
*
|
2011-09-29 06:19:26 +00:00
|
|
|
* @see NS_ProxyRelease(nsIEventTarget*, nsISupports*, bool)
|
2006-05-22 14:19:13 +00:00
|
|
|
*/
|
2014-06-27 01:35:39 +00:00
|
|
|
template<class T>
|
2006-05-22 14:19:13 +00:00
|
|
|
inline NS_HIDDEN_(nsresult)
|
2014-06-27 01:35:39 +00:00
|
|
|
NS_ProxyRelease(nsIEventTarget* aTarget, nsCOMPtr<T>& aDoomed,
|
|
|
|
bool aAlwaysProxy = false)
|
2006-05-22 14:19:13 +00:00
|
|
|
{
|
2014-06-27 01:35:39 +00:00
|
|
|
T* raw = nullptr;
|
|
|
|
aDoomed.swap(raw);
|
|
|
|
return NS_ProxyRelease(aTarget, raw, aAlwaysProxy);
|
2006-05-22 14:19:13 +00:00
|
|
|
}
|
2002-05-15 18:55:21 +00:00
|
|
|
|
2010-11-16 00:08:57 +00:00
|
|
|
/**
|
|
|
|
* Ensure that a nsRefPtr is released on the target thread.
|
|
|
|
*
|
2011-09-29 06:19:26 +00:00
|
|
|
* @see NS_ProxyRelease(nsIEventTarget*, nsISupports*, bool)
|
2010-11-16 00:08:57 +00:00
|
|
|
*/
|
2014-06-27 01:35:39 +00:00
|
|
|
template<class T>
|
2010-11-16 00:08:57 +00:00
|
|
|
inline NS_HIDDEN_(nsresult)
|
2014-06-27 01:35:39 +00:00
|
|
|
NS_ProxyRelease(nsIEventTarget* aTarget, nsRefPtr<T>& aDoomed,
|
|
|
|
bool aAlwaysProxy = false)
|
2010-11-16 00:08:57 +00:00
|
|
|
{
|
2014-06-27 01:35:39 +00:00
|
|
|
T* raw = nullptr;
|
|
|
|
aDoomed.swap(raw);
|
|
|
|
return NS_ProxyRelease(aTarget, raw, aAlwaysProxy);
|
2010-11-16 00:08:57 +00:00
|
|
|
}
|
|
|
|
|
2003-10-06 01:46:31 +00:00
|
|
|
/**
|
|
|
|
* Ensures that the delete of a nsISupports object occurs on the target thread.
|
|
|
|
*
|
2014-06-27 01:35:39 +00:00
|
|
|
* @param aTarget
|
2003-10-06 01:46:31 +00:00
|
|
|
* the target thread where the doomed object should be released.
|
2014-06-27 01:35:39 +00:00
|
|
|
* @param aDoomed
|
2003-10-06 01:46:31 +00:00
|
|
|
* the doomed object; the object to be released on the target thread.
|
2014-06-27 01:35:39 +00:00
|
|
|
* @param aAlwaysProxy
|
2003-10-06 01:46:31 +00:00
|
|
|
* normally, if NS_ProxyRelease is called on the target thread, then the
|
|
|
|
* doomed object will released directly. however, if this parameter is
|
2006-05-22 14:19:13 +00:00
|
|
|
* true, then an event will always be posted to the target thread for
|
|
|
|
* asynchronous release.
|
2003-10-06 01:46:31 +00:00
|
|
|
*/
|
2014-08-27 22:47:27 +00:00
|
|
|
nsresult
|
2014-06-27 01:35:39 +00:00
|
|
|
NS_ProxyRelease(nsIEventTarget* aTarget, nsISupports* aDoomed,
|
|
|
|
bool aAlwaysProxy = false);
|
2002-05-31 20:40:11 +00:00
|
|
|
|
2012-07-13 14:47:14 +00:00
|
|
|
/**
|
|
|
|
* Class to safely handle main-thread-only pointers off the main thread.
|
|
|
|
*
|
|
|
|
* Classes like XPCWrappedJS are main-thread-only, which means that it is
|
|
|
|
* forbidden to call methods on instances of these classes off the main thread.
|
|
|
|
* For various reasons (see bug 771074), this restriction recently began to
|
|
|
|
* apply to AddRef/Release as well.
|
|
|
|
*
|
|
|
|
* This presents a problem for consumers that wish to hold a callback alive
|
|
|
|
* on non-main-thread code. A common example of this is the proxy callback
|
|
|
|
* pattern, where non-main-thread code holds a strong-reference to the callback
|
|
|
|
* object, and dispatches new Runnables (also with a strong reference) to the
|
|
|
|
* main thread in order to execute the callback. This involves several AddRef
|
|
|
|
* and Release calls on the other thread, which is (now) verboten.
|
|
|
|
*
|
|
|
|
* The basic idea of this class is to introduce a layer of indirection.
|
|
|
|
* nsMainThreadPtrHolder is a threadsafe reference-counted class that internally
|
|
|
|
* maintains one strong reference to the main-thread-only object. It must be
|
|
|
|
* instantiated on the main thread (so that the AddRef of the underlying object
|
|
|
|
* happens on the main thread), but consumers may subsequently pass references
|
|
|
|
* to the holder anywhere they please. These references are meant to be opaque
|
|
|
|
* when accessed off-main-thread (assertions enforce this).
|
|
|
|
*
|
|
|
|
* The semantics of nsRefPtr<nsMainThreadPtrHolder<T> > would be cumbersome, so
|
|
|
|
* we also introduce nsMainThreadPtrHandle<T>, which is conceptually identical
|
|
|
|
* to the above (though it includes various convenience methods). The basic
|
|
|
|
* pattern is as follows.
|
|
|
|
*
|
|
|
|
* // On the main thread:
|
|
|
|
* nsCOMPtr<nsIFooCallback> callback = ...;
|
|
|
|
* nsMainThreadPtrHandle<nsIFooCallback> callbackHandle =
|
|
|
|
* new nsMainThreadPtrHolder<nsIFooCallback>(callback);
|
|
|
|
* // Pass callbackHandle to structs/classes that might be accessed on other
|
|
|
|
* // threads.
|
|
|
|
*
|
|
|
|
* All structs and classes that might be accessed on other threads should store
|
|
|
|
* an nsMainThreadPtrHandle<T> rather than an nsCOMPtr<T>.
|
|
|
|
*/
|
|
|
|
template<class T>
|
2015-03-21 16:28:04 +00:00
|
|
|
class nsMainThreadPtrHolder final
|
2012-07-13 14:47:14 +00:00
|
|
|
{
|
|
|
|
public:
|
2013-03-15 17:41:48 +00:00
|
|
|
// We can only acquire a pointer on the main thread. We to fail fast for
|
|
|
|
// threading bugs, so by default we assert if our pointer is used or acquired
|
|
|
|
// off-main-thread. But some consumers need to use the same pointer for
|
|
|
|
// multiple classes, some of which are main-thread-only and some of which
|
|
|
|
// aren't. So we allow them to explicitly disable this strict checking.
|
2014-07-28 17:19:06 +00:00
|
|
|
explicit nsMainThreadPtrHolder(T* aPtr, bool aStrict = true)
|
2014-06-27 01:35:39 +00:00
|
|
|
: mRawPtr(nullptr)
|
|
|
|
, mStrict(aStrict)
|
|
|
|
{
|
2012-07-13 14:47:14 +00:00
|
|
|
// We can only AddRef our pointer on the main thread, which means that the
|
|
|
|
// holder must be constructed on the main thread.
|
2013-03-15 17:41:48 +00:00
|
|
|
MOZ_ASSERT(!mStrict || NS_IsMainThread());
|
2014-06-27 01:35:39 +00:00
|
|
|
NS_IF_ADDREF(mRawPtr = aPtr);
|
2012-07-13 14:47:14 +00:00
|
|
|
}
|
|
|
|
|
2014-06-19 02:29:00 +00:00
|
|
|
private:
|
2012-07-13 14:47:14 +00:00
|
|
|
// We can be released on any thread.
|
2014-06-27 01:35:39 +00:00
|
|
|
~nsMainThreadPtrHolder()
|
|
|
|
{
|
2012-07-13 14:47:14 +00:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
NS_IF_RELEASE(mRawPtr);
|
2013-01-08 18:40:18 +00:00
|
|
|
} else if (mRawPtr) {
|
2013-09-19 13:54:42 +00:00
|
|
|
nsCOMPtr<nsIThread> mainThread;
|
|
|
|
NS_GetMainThread(getter_AddRefs(mainThread));
|
2012-07-13 14:47:14 +00:00
|
|
|
if (!mainThread) {
|
|
|
|
NS_WARNING("Couldn't get main thread! Leaking pointer.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NS_ProxyRelease(mainThread, mRawPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-19 02:29:00 +00:00
|
|
|
public:
|
2014-06-27 01:35:39 +00:00
|
|
|
T* get()
|
|
|
|
{
|
2012-07-13 14:47:14 +00:00
|
|
|
// Nobody should be touching the raw pointer off-main-thread.
|
2013-03-15 17:41:48 +00:00
|
|
|
if (mStrict && MOZ_UNLIKELY(!NS_IsMainThread())) {
|
2013-01-09 23:58:29 +00:00
|
|
|
NS_ERROR("Can't dereference nsMainThreadPtrHolder off main thread");
|
2012-07-13 14:47:14 +00:00
|
|
|
MOZ_CRASH();
|
2013-01-09 23:58:29 +00:00
|
|
|
}
|
2012-07-13 14:47:14 +00:00
|
|
|
return mRawPtr;
|
|
|
|
}
|
|
|
|
|
2014-06-27 01:35:39 +00:00
|
|
|
bool operator==(const nsMainThreadPtrHolder<T>& aOther) const
|
|
|
|
{
|
|
|
|
return mRawPtr == aOther.mRawPtr;
|
|
|
|
}
|
2014-09-11 10:11:44 +00:00
|
|
|
bool operator!() const
|
|
|
|
{
|
|
|
|
return !mRawPtr;
|
|
|
|
}
|
2013-03-15 17:41:48 +00:00
|
|
|
|
2013-07-19 02:31:26 +00:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(nsMainThreadPtrHolder<T>)
|
2012-07-13 14:47:14 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Our wrapped pointer.
|
|
|
|
T* mRawPtr;
|
|
|
|
|
2013-03-15 17:41:48 +00:00
|
|
|
// Whether to strictly enforce thread invariants in this class.
|
|
|
|
bool mStrict;
|
|
|
|
|
2012-07-13 14:47:14 +00:00
|
|
|
// Copy constructor and operator= not implemented. Once constructed, the
|
|
|
|
// holder is immutable.
|
2014-06-27 01:35:39 +00:00
|
|
|
T& operator=(nsMainThreadPtrHolder& aOther);
|
|
|
|
nsMainThreadPtrHolder(const nsMainThreadPtrHolder& aOther);
|
2012-07-13 14:47:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class nsMainThreadPtrHandle
|
|
|
|
{
|
2014-06-27 01:35:39 +00:00
|
|
|
nsRefPtr<nsMainThreadPtrHolder<T>> mPtr;
|
2012-07-13 14:47:14 +00:00
|
|
|
|
2014-06-27 01:35:39 +00:00
|
|
|
public:
|
2013-10-10 20:41:39 +00:00
|
|
|
nsMainThreadPtrHandle() : mPtr(nullptr) {}
|
2014-08-25 19:17:24 +00:00
|
|
|
explicit nsMainThreadPtrHandle(nsMainThreadPtrHolder<T>* aHolder)
|
|
|
|
: mPtr(aHolder)
|
|
|
|
{
|
|
|
|
}
|
2014-06-27 01:35:39 +00:00
|
|
|
nsMainThreadPtrHandle(const nsMainThreadPtrHandle& aOther)
|
|
|
|
: mPtr(aOther.mPtr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
nsMainThreadPtrHandle& operator=(const nsMainThreadPtrHandle& aOther)
|
|
|
|
{
|
2012-07-13 14:47:14 +00:00
|
|
|
mPtr = aOther.mPtr;
|
2013-01-08 04:09:19 +00:00
|
|
|
return *this;
|
2012-07-13 14:47:14 +00:00
|
|
|
}
|
2014-07-30 00:43:56 +00:00
|
|
|
nsMainThreadPtrHandle& operator=(nsMainThreadPtrHolder<T>* aHolder)
|
|
|
|
{
|
|
|
|
mPtr = aHolder;
|
|
|
|
return *this;
|
|
|
|
}
|
2012-07-13 14:47:14 +00:00
|
|
|
|
|
|
|
// These all call through to nsMainThreadPtrHolder, and thus implicitly
|
|
|
|
// assert that we're on the main thread. Off-main-thread consumers must treat
|
|
|
|
// these handles as opaque.
|
2013-01-09 23:58:29 +00:00
|
|
|
T* get()
|
|
|
|
{
|
|
|
|
if (mPtr) {
|
|
|
|
return mPtr.get()->get();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-03-15 17:41:48 +00:00
|
|
|
const T* get() const
|
|
|
|
{
|
|
|
|
if (mPtr) {
|
|
|
|
return mPtr.get()->get();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-07-13 14:47:14 +00:00
|
|
|
operator T*() { return get(); }
|
2014-12-25 20:18:38 +00:00
|
|
|
T* operator->() MOZ_NO_ADDREF_RELEASE_ON_RETURN { return get(); }
|
2013-03-15 17:41:48 +00:00
|
|
|
|
|
|
|
// These are safe to call on other threads with appropriate external locking.
|
2014-06-27 01:35:39 +00:00
|
|
|
bool operator==(const nsMainThreadPtrHandle<T>& aOther) const
|
|
|
|
{
|
|
|
|
if (!mPtr || !aOther.mPtr) {
|
2013-03-15 17:41:48 +00:00
|
|
|
return mPtr == aOther.mPtr;
|
2014-06-27 01:35:39 +00:00
|
|
|
}
|
2013-03-15 17:41:48 +00:00
|
|
|
return *mPtr == *aOther.mPtr;
|
|
|
|
}
|
2014-09-11 10:11:44 +00:00
|
|
|
bool operator!() const {
|
|
|
|
return !mPtr || !*mPtr;
|
|
|
|
}
|
2012-07-13 14:47:14 +00:00
|
|
|
};
|
|
|
|
|
2002-05-15 18:55:21 +00:00
|
|
|
#endif
|