2013-07-24 11:41:39 +04: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-10-04 23:45:07 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2013-07-24 11:41:39 +04:00
|
|
|
* 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/. */
|
2012-10-04 23:45:07 +04:00
|
|
|
|
|
|
|
/* Weak pointer functionality, implemented as a mixin for use with any class. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SupportsWeakPtr lets you have a pointer to an object 'Foo' without affecting
|
|
|
|
* its lifetime. It works by creating a single shared reference counted object
|
|
|
|
* (WeakReference) that each WeakPtr will access 'Foo' through. This lets 'Foo'
|
|
|
|
* clear the pointer in the WeakReference without having to know about all of
|
|
|
|
* the WeakPtrs to it and allows the WeakReference to live beyond the lifetime
|
|
|
|
* of 'Foo'.
|
|
|
|
*
|
2014-03-28 08:12:30 +04:00
|
|
|
* PLEASE NOTE: This weak pointer implementation is not thread-safe.
|
|
|
|
*
|
2014-02-21 06:33:49 +04:00
|
|
|
* Note that when deriving from SupportsWeakPtr you should add
|
2015-04-24 19:43:01 +03:00
|
|
|
* MOZ_DECLARE_WEAKREFERENCE_TYPENAME(ClassName) to the public section of your
|
2014-02-21 06:33:49 +04:00
|
|
|
* class, where ClassName is the name of your class.
|
|
|
|
*
|
2012-10-04 23:45:07 +04:00
|
|
|
* The overhead of WeakPtr is that accesses to 'Foo' becomes an additional
|
|
|
|
* dereference, and an additional heap allocated pointer sized object shared
|
|
|
|
* between all of the WeakPtrs.
|
|
|
|
*
|
|
|
|
* Example of usage:
|
|
|
|
*
|
2014-07-11 06:10:17 +04:00
|
|
|
* // To have a class C support weak pointers, inherit from
|
|
|
|
* // SupportsWeakPtr<C>.
|
2012-10-04 23:45:07 +04:00
|
|
|
* class C : public SupportsWeakPtr<C>
|
|
|
|
* {
|
2014-07-11 06:10:17 +04:00
|
|
|
* public:
|
2015-04-24 19:43:01 +03:00
|
|
|
* MOZ_DECLARE_WEAKREFERENCE_TYPENAME(C)
|
2014-07-11 06:10:17 +04:00
|
|
|
* int mNum;
|
|
|
|
* void act();
|
2012-10-04 23:45:07 +04:00
|
|
|
* };
|
|
|
|
*
|
2014-07-11 06:10:17 +04:00
|
|
|
* C* ptr = new C();
|
2012-10-04 23:45:07 +04:00
|
|
|
*
|
2014-07-30 23:52:05 +04:00
|
|
|
* // Get weak pointers to ptr. The first time a weak pointer
|
|
|
|
* // is obtained, a reference counted WeakReference object is created that
|
2012-10-04 23:45:07 +04:00
|
|
|
* // can live beyond the lifetime of 'ptr'. The WeakReference
|
|
|
|
* // object will be notified of 'ptr's destruction.
|
2014-07-30 23:52:05 +04:00
|
|
|
* WeakPtr<C> weak = ptr;
|
|
|
|
* WeakPtr<C> other = ptr;
|
2012-10-04 23:45:07 +04:00
|
|
|
*
|
|
|
|
* // Test a weak pointer for validity before using it.
|
|
|
|
* if (weak) {
|
2014-07-11 06:10:17 +04:00
|
|
|
* weak->mNum = 17;
|
2012-10-04 23:45:07 +04:00
|
|
|
* weak->act();
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // Destroying the underlying object clears weak pointers to it.
|
|
|
|
* delete ptr;
|
|
|
|
*
|
|
|
|
* MOZ_ASSERT(!weak, "Deleting |ptr| clears weak pointers to it.");
|
|
|
|
* MOZ_ASSERT(!other, "Deleting |ptr| clears all weak pointers to it.");
|
|
|
|
*
|
|
|
|
* WeakPtr is typesafe and may be used with any class. It is not required that
|
|
|
|
* the class be reference-counted or allocated in any particular way.
|
|
|
|
*
|
|
|
|
* The API was loosely inspired by Chromium's weak_ptr.h:
|
|
|
|
* http://src.chromium.org/svn/trunk/src/base/memory/weak_ptr.h
|
|
|
|
*/
|
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#ifndef mozilla_WeakPtr_h
|
|
|
|
#define mozilla_WeakPtr_h
|
2012-10-04 23:45:07 +04:00
|
|
|
|
2014-03-09 22:36:36 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2012-10-04 23:45:07 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2014-12-24 01:26:28 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
2017-05-27 00:21:28 +03:00
|
|
|
#include "mozilla/Maybe.h"
|
2015-09-23 04:27:34 +03:00
|
|
|
#include "mozilla/RefCounted.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2012-10-04 23:45:07 +04:00
|
|
|
#include "mozilla/TypeTraits.h"
|
|
|
|
|
2014-03-09 22:36:36 +04:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-05-27 00:21:28 +03:00
|
|
|
#if defined(MOZILLA_INTERNAL_API)
|
|
|
|
// For thread safety checking.
|
|
|
|
# include "nsISupportsImpl.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MOZILLA_INTERNAL_API) && \
|
|
|
|
defined(MOZ_THREAD_SAFETY_OWNERSHIP_CHECKS_SUPPORTED)
|
|
|
|
|
2016-08-01 14:16:16 +03:00
|
|
|
// Weak referencing is not implemeted as thread safe. When a WeakPtr
|
|
|
|
// is created or dereferenced on thread A but the real object is just
|
|
|
|
// being Released() on thread B, there is a possibility of a race
|
|
|
|
// when the proxy object (detail::WeakReference) is notified about
|
|
|
|
// the real object destruction just between when thread A is storing
|
|
|
|
// the object pointer locally and is about to add a reference to it.
|
|
|
|
//
|
|
|
|
// Hence, a non-null weak proxy object is considered to have a single
|
|
|
|
// "owning thread". It means that each query for a weak reference,
|
|
|
|
// its dereference, and destruction of the real object must all happen
|
|
|
|
// on a single thread. The following macros implement assertions for
|
|
|
|
// checking these conditions.
|
2017-01-13 22:50:21 +03:00
|
|
|
//
|
2017-05-27 00:21:28 +03:00
|
|
|
// We re-use XPCOM's nsAutoOwningThread checks when they are available. This has
|
|
|
|
// the advantage that it works with cooperative thread pools.
|
2016-08-01 14:16:16 +03:00
|
|
|
|
|
|
|
# define MOZ_WEAKPTR_DECLARE_THREAD_SAFETY_CHECK \
|
2017-05-27 00:21:28 +03:00
|
|
|
/* Will be none if mPtr = nullptr. */ \
|
|
|
|
Maybe<nsAutoOwningThread> _owningThread;
|
2016-08-01 14:16:16 +03:00
|
|
|
# define MOZ_WEAKPTR_INIT_THREAD_SAFETY_CHECK() \
|
|
|
|
do { \
|
2017-05-27 00:21:28 +03:00
|
|
|
if (p) { \
|
|
|
|
_owningThread.emplace(); \
|
|
|
|
} \
|
2016-08-01 14:16:16 +03:00
|
|
|
} while (false)
|
|
|
|
# define MOZ_WEAKPTR_ASSERT_THREAD_SAFETY() \
|
2017-04-30 07:14:06 +03:00
|
|
|
do { \
|
2017-05-27 00:21:28 +03:00
|
|
|
if (_owningThread.isSome() && !_owningThread.ref().IsCurrentThread()) { \
|
2017-04-30 07:14:06 +03:00
|
|
|
WeakPtrTraits<T>::AssertSafeToAccessFromNonOwningThread(); \
|
|
|
|
} \
|
|
|
|
} while (false)
|
2016-08-01 14:16:16 +03:00
|
|
|
# define MOZ_WEAKPTR_ASSERT_THREAD_SAFETY_DELEGATED(that) \
|
|
|
|
(that)->AssertThreadSafety();
|
2019-09-13 20:23:16 +03:00
|
|
|
# define MOZ_WEAKPTR_ASSERT_THREAD_SAFETY_DELEGATED_IF(that) \
|
|
|
|
do { \
|
|
|
|
if (that) { \
|
|
|
|
(that)->AssertThreadSafety(); \
|
|
|
|
} \
|
|
|
|
} while (false)
|
2019-01-18 12:16:18 +03:00
|
|
|
|
2016-08-01 14:16:16 +03:00
|
|
|
# define MOZ_WEAKPTR_THREAD_SAFETY_CHECKING 1
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
# define MOZ_WEAKPTR_DECLARE_THREAD_SAFETY_CHECK
|
|
|
|
# define MOZ_WEAKPTR_INIT_THREAD_SAFETY_CHECK() \
|
|
|
|
do { \
|
|
|
|
} while (false)
|
|
|
|
# define MOZ_WEAKPTR_ASSERT_THREAD_SAFETY() \
|
|
|
|
do { \
|
|
|
|
} while (false)
|
|
|
|
# define MOZ_WEAKPTR_ASSERT_THREAD_SAFETY_DELEGATED(that) \
|
|
|
|
do { \
|
|
|
|
} while (false)
|
2019-09-13 20:23:16 +03:00
|
|
|
# define MOZ_WEAKPTR_ASSERT_THREAD_SAFETY_DELEGATED_IF(that) \
|
|
|
|
do { \
|
|
|
|
} while (false)
|
2016-08-01 14:16:16 +03:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-10-04 23:45:07 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2014-07-30 23:52:02 +04:00
|
|
|
template <typename T>
|
|
|
|
class WeakPtr;
|
|
|
|
template <typename T>
|
|
|
|
class SupportsWeakPtr;
|
2012-10-04 23:45:07 +04:00
|
|
|
|
2015-04-24 19:43:01 +03:00
|
|
|
#ifdef MOZ_REFCOUNTED_LEAK_CHECKING
|
|
|
|
# define MOZ_DECLARE_WEAKREFERENCE_TYPENAME(T) \
|
|
|
|
static const char* weakReferenceTypeName() { \
|
|
|
|
return "WeakReference<" #T ">"; \
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define MOZ_DECLARE_WEAKREFERENCE_TYPENAME(T)
|
|
|
|
#endif
|
|
|
|
|
2017-04-30 07:14:06 +03:00
|
|
|
template <class T>
|
|
|
|
struct WeakPtrTraits {
|
|
|
|
static void AssertSafeToAccessFromNonOwningThread() {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(false, "WeakPtr accessed from multiple threads");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-20 01:59:01 +04:00
|
|
|
namespace detail {
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
// This can live beyond the lifetime of the class derived from
|
2014-07-30 23:52:02 +04:00
|
|
|
// SupportsWeakPtr.
|
2013-10-16 21:48:34 +04:00
|
|
|
template <class T>
|
2019-09-11 22:44:17 +03:00
|
|
|
class WeakReference : public ::mozilla::RefCounted<WeakReference<T>> {
|
2014-07-11 06:10:17 +04:00
|
|
|
public:
|
2016-08-01 14:16:16 +03:00
|
|
|
explicit WeakReference(T* p) : mPtr(p) {
|
|
|
|
MOZ_WEAKPTR_INIT_THREAD_SAFETY_CHECK();
|
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
|
2016-08-01 14:16:16 +03:00
|
|
|
T* get() const {
|
|
|
|
MOZ_WEAKPTR_ASSERT_THREAD_SAFETY();
|
|
|
|
return mPtr;
|
|
|
|
}
|
2013-04-20 01:59:01 +04:00
|
|
|
|
2014-03-09 22:36:36 +04:00
|
|
|
#ifdef MOZ_REFCOUNTED_LEAK_CHECKING
|
2014-07-11 06:10:17 +04:00
|
|
|
const char* typeName() const {
|
2015-04-24 19:43:01 +03:00
|
|
|
// The first time this is called mPtr is null, so don't
|
|
|
|
// invoke any methods on mPtr.
|
|
|
|
return T::weakReferenceTypeName();
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
|
|
|
size_t typeSize() const { return sizeof(*this); }
|
2014-03-09 22:36:36 +04:00
|
|
|
#endif
|
|
|
|
|
2016-08-01 14:16:16 +03:00
|
|
|
#ifdef MOZ_WEAKPTR_THREAD_SAFETY_CHECKING
|
|
|
|
void AssertThreadSafety() { MOZ_WEAKPTR_ASSERT_THREAD_SAFETY(); }
|
|
|
|
#endif
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
private:
|
2014-07-30 23:52:04 +04:00
|
|
|
friend class mozilla::SupportsWeakPtr<T>;
|
2014-07-11 06:10:17 +04:00
|
|
|
|
2016-08-01 14:16:16 +03:00
|
|
|
void detach() {
|
|
|
|
MOZ_WEAKPTR_ASSERT_THREAD_SAFETY();
|
|
|
|
mPtr = nullptr;
|
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
|
2014-12-24 05:17:50 +03:00
|
|
|
T* MOZ_NON_OWNING_REF mPtr;
|
2016-08-01 14:16:16 +03:00
|
|
|
MOZ_WEAKPTR_DECLARE_THREAD_SAFETY_CHECK
|
2013-04-20 01:59:01 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
2014-07-30 23:52:01 +04:00
|
|
|
template <typename T>
|
2014-07-30 23:52:02 +04:00
|
|
|
class SupportsWeakPtr {
|
2014-07-11 06:10:17 +04:00
|
|
|
protected:
|
2014-07-30 23:52:02 +04:00
|
|
|
~SupportsWeakPtr() {
|
|
|
|
static_assert(IsBaseOf<SupportsWeakPtr<T>, T>::value,
|
|
|
|
"T must derive from SupportsWeakPtr<T>");
|
2014-07-30 23:52:04 +04:00
|
|
|
if (mSelfReferencingWeakPtr) {
|
|
|
|
mSelfReferencingWeakPtr.mRef->detach();
|
2012-10-04 23:45:07 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2012-10-04 23:45:07 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
private:
|
2014-07-30 23:52:04 +04:00
|
|
|
const WeakPtr<T>& SelfReferencingWeakPtr() {
|
|
|
|
if (!mSelfReferencingWeakPtr) {
|
|
|
|
mSelfReferencingWeakPtr.mRef =
|
|
|
|
new detail::WeakReference<T>(static_cast<T*>(this));
|
2016-08-01 14:16:16 +03:00
|
|
|
} else {
|
|
|
|
MOZ_WEAKPTR_ASSERT_THREAD_SAFETY_DELEGATED(mSelfReferencingWeakPtr.mRef);
|
2014-07-30 23:52:04 +04:00
|
|
|
}
|
|
|
|
return mSelfReferencingWeakPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const WeakPtr<const T>& SelfReferencingWeakPtr() const {
|
|
|
|
const WeakPtr<T>& p =
|
|
|
|
const_cast<SupportsWeakPtr*>(this)->SelfReferencingWeakPtr();
|
|
|
|
return reinterpret_cast<const WeakPtr<const T>&>(p);
|
|
|
|
}
|
|
|
|
|
2014-07-30 23:52:02 +04:00
|
|
|
friend class WeakPtr<T>;
|
2014-07-30 23:52:04 +04:00
|
|
|
friend class WeakPtr<const T>;
|
2012-10-04 23:45:07 +04:00
|
|
|
|
2014-07-30 23:52:04 +04:00
|
|
|
WeakPtr<T> mSelfReferencingWeakPtr;
|
2012-10-04 23:45:07 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2014-07-30 23:52:02 +04:00
|
|
|
class WeakPtr {
|
2014-07-30 23:52:04 +04:00
|
|
|
typedef detail::WeakReference<T> WeakReference;
|
2019-09-11 22:44:17 +03:00
|
|
|
typedef typename RemoveConst<T>::Type NonConstT;
|
2014-07-30 23:52:04 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
public:
|
2014-07-30 23:52:04 +04:00
|
|
|
WeakPtr& operator=(const WeakPtr& aOther) {
|
2019-09-13 20:23:16 +03:00
|
|
|
// We must make sure the reference we have now is safe to be dereferenced
|
|
|
|
// before we throw it away... (this can be called from a ctor)
|
|
|
|
MOZ_WEAKPTR_ASSERT_THREAD_SAFETY_DELEGATED_IF(mRef);
|
|
|
|
// ...and make sure the new reference is used on a single thread as well.
|
|
|
|
MOZ_WEAKPTR_ASSERT_THREAD_SAFETY_DELEGATED(aOther.mRef);
|
|
|
|
|
2014-07-30 23:52:04 +04:00
|
|
|
mRef = aOther.mRef;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-07-30 23:52:03 +04:00
|
|
|
WeakPtr(const WeakPtr& aOther) {
|
2016-08-01 14:16:16 +03:00
|
|
|
// The thread safety check is performed inside of the operator= method.
|
2014-07-30 23:52:04 +04:00
|
|
|
*this = aOther;
|
|
|
|
}
|
|
|
|
|
2019-09-11 22:44:17 +03:00
|
|
|
WeakPtr& operator=(decltype(nullptr)) {
|
2019-09-13 20:23:16 +03:00
|
|
|
// We must make sure the reference we have now is safe to be dereferenced
|
|
|
|
// before we throw it away.
|
|
|
|
MOZ_WEAKPTR_ASSERT_THREAD_SAFETY_DELEGATED_IF(mRef);
|
2019-09-11 22:44:17 +03:00
|
|
|
if (!mRef || mRef->get()) {
|
|
|
|
// Ensure that mRef is dereferenceable in the uninitialized state.
|
|
|
|
mRef = new WeakReference(nullptr);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
WeakPtr& operator=(SupportsWeakPtr<NonConstT> const* aOther) {
|
2019-09-13 20:23:16 +03:00
|
|
|
// We must make sure the reference we have now is safe to be dereferenced
|
|
|
|
// before we throw it away.
|
|
|
|
MOZ_WEAKPTR_ASSERT_THREAD_SAFETY_DELEGATED_IF(mRef);
|
2015-09-24 03:53:31 +03:00
|
|
|
if (aOther) {
|
|
|
|
*this = aOther->SelfReferencingWeakPtr();
|
|
|
|
} else if (!mRef || mRef->get()) {
|
|
|
|
// Ensure that mRef is dereferenceable in the uninitialized state.
|
|
|
|
mRef = new WeakReference(nullptr);
|
|
|
|
}
|
2016-08-01 14:16:16 +03:00
|
|
|
// The thread safety check happens inside SelfReferencingWeakPtr
|
|
|
|
// or is initialized in the WeakReference constructor.
|
2015-09-24 03:53:31 +03:00
|
|
|
return *this;
|
2014-07-30 23:52:04 +04:00
|
|
|
}
|
|
|
|
|
2019-09-11 22:44:17 +03:00
|
|
|
WeakPtr& operator=(SupportsWeakPtr<NonConstT>* aOther) {
|
2019-09-13 20:23:16 +03:00
|
|
|
// We must make sure the reference we have now is safe to be dereferenced
|
|
|
|
// before we throw it away.
|
|
|
|
MOZ_WEAKPTR_ASSERT_THREAD_SAFETY_DELEGATED_IF(mRef);
|
2019-09-11 22:44:17 +03:00
|
|
|
if (aOther) {
|
|
|
|
*this = aOther->SelfReferencingWeakPtr();
|
|
|
|
} else if (!mRef || mRef->get()) {
|
|
|
|
// Ensure that mRef is dereferenceable in the uninitialized state.
|
|
|
|
mRef = new WeakReference(nullptr);
|
|
|
|
}
|
|
|
|
// The thread safety check happens inside SelfReferencingWeakPtr
|
|
|
|
// or is initialized in the WeakReference constructor.
|
|
|
|
return *this;
|
2014-07-30 23:52:04 +04:00
|
|
|
}
|
2012-10-04 23:45:07 +04:00
|
|
|
|
2019-09-11 22:44:17 +03:00
|
|
|
MOZ_IMPLICIT WeakPtr(T* aOther) { *this = aOther; }
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
// Ensure that mRef is dereferenceable in the uninitialized state.
|
2014-07-30 23:52:04 +04:00
|
|
|
WeakPtr() : mRef(new WeakReference(nullptr)) {}
|
2012-10-04 23:45:07 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
operator T*() const { return mRef->get(); }
|
|
|
|
T& operator*() const { return *mRef->get(); }
|
2012-10-04 23:45:07 +04:00
|
|
|
|
2014-12-25 23:18:38 +03:00
|
|
|
T* operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN { return mRef->get(); }
|
2014-07-11 06:10:17 +04:00
|
|
|
|
|
|
|
T* get() const { return mRef->get(); }
|
2012-10-13 02:17:58 +04:00
|
|
|
|
2018-10-03 19:41:10 +03:00
|
|
|
~WeakPtr() { MOZ_WEAKPTR_ASSERT_THREAD_SAFETY_DELEGATED(mRef); }
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
private:
|
2014-07-30 23:52:02 +04:00
|
|
|
friend class SupportsWeakPtr<T>;
|
2012-10-04 23:45:07 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
explicit WeakPtr(const RefPtr<WeakReference>& aOther) : mRef(aOther) {}
|
2012-10-04 23:45:07 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WeakReference> mRef;
|
2013-04-20 01:59:01 +04:00
|
|
|
};
|
|
|
|
|
2012-10-04 23:45:07 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#endif /* mozilla_WeakPtr_h */
|