| Index: base/memory/ref_counted.h
|
| diff --git a/base/memory/ref_counted.h b/base/memory/ref_counted.h
|
| index 784a1788a81a07808be26e82c830eeea59acc1b8..5277fcba69d55ff4c82903e1742d83096100f325 100644
|
| --- a/base/memory/ref_counted.h
|
| +++ b/base/memory/ref_counted.h
|
| @@ -16,11 +16,11 @@
|
| #include "base/compiler_specific.h"
|
| #include "base/logging.h"
|
| #include "base/macros.h"
|
| +#include "base/sequence_checker.h"
|
| #include "base/threading/thread_collision_warner.h"
|
| #include "build/build_config.h"
|
|
|
| namespace base {
|
| -
|
| namespace subtle {
|
|
|
| class BASE_EXPORT RefCountedBase {
|
| @@ -28,21 +28,17 @@ class BASE_EXPORT RefCountedBase {
|
| bool HasOneRef() const { return ref_count_ == 1; }
|
|
|
| protected:
|
| - RefCountedBase()
|
| - : ref_count_(0)
|
| + RefCountedBase() {
|
| #if DCHECK_IS_ON()
|
| - , in_dtor_(false)
|
| + sequence_checker_.DetachFromSequence();
|
| #endif
|
| - {
|
| }
|
| -
|
| ~RefCountedBase() {
|
| #if DCHECK_IS_ON()
|
| DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()";
|
| #endif
|
| }
|
|
|
| -
|
| void AddRef() const {
|
| // TODO(maruel): Add back once it doesn't assert 500 times/sec.
|
| // Current thread books the critical section "AddRelease"
|
| @@ -50,32 +46,46 @@ class BASE_EXPORT RefCountedBase {
|
| // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_);
|
| #if DCHECK_IS_ON()
|
| DCHECK(!in_dtor_);
|
| + if (ref_count_ >= 1)
|
| + DCHECK(CalledOnValidSequence());
|
| #endif
|
| +
|
| ++ref_count_;
|
| }
|
|
|
| // Returns true if the object should self-delete.
|
| bool Release() const {
|
| + --ref_count_;
|
| +
|
| // TODO(maruel): Add back once it doesn't assert 500 times/sec.
|
| // Current thread books the critical section "AddRelease"
|
| // without release it.
|
| // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_);
|
| +
|
| #if DCHECK_IS_ON()
|
| DCHECK(!in_dtor_);
|
| -#endif
|
| - if (--ref_count_ == 0) {
|
| -#if DCHECK_IS_ON()
|
| + if (ref_count_ == 0)
|
| in_dtor_ = true;
|
| +
|
| + if (ref_count_ >= 1)
|
| + DCHECK(CalledOnValidSequence());
|
| + if (ref_count_ == 1)
|
| + sequence_checker_.DetachFromSequence();
|
| #endif
|
| - return true;
|
| - }
|
| - return false;
|
| +
|
| + return ref_count_ == 0;
|
| }
|
|
|
| private:
|
| - mutable size_t ref_count_;
|
| #if DCHECK_IS_ON()
|
| - mutable bool in_dtor_;
|
| + bool CalledOnValidSequence() const;
|
| +#endif
|
| +
|
| + mutable size_t ref_count_ = 0;
|
| +
|
| +#if DCHECK_IS_ON()
|
| + mutable bool in_dtor_ = false;
|
| + mutable SequenceChecker sequence_checker_;
|
| #endif
|
|
|
| DFAKE_MUTEX(add_release_);
|
| @@ -107,6 +117,18 @@ class BASE_EXPORT RefCountedThreadSafeBase {
|
|
|
| } // namespace subtle
|
|
|
| +// See RefCounted class for documentation.
|
| +class BASE_EXPORT ScopedAllowCrossThreadRefCountAccess final {
|
| + public:
|
| +#if DCHECK_IS_ON()
|
| + ScopedAllowCrossThreadRefCountAccess();
|
| + ~ScopedAllowCrossThreadRefCountAccess();
|
| +#else
|
| + ScopedAllowCrossThreadRefCountAccess() {}
|
| + ~ScopedAllowCrossThreadRefCountAccess() {}
|
| +#endif
|
| +};
|
| +
|
| //
|
| // A base class for reference counted classes. Otherwise, known as a cheap
|
| // knock-off of WebKit's RefCounted<T> class. To use this, just extend your
|
| @@ -121,6 +143,16 @@ class BASE_EXPORT RefCountedThreadSafeBase {
|
| //
|
| // You should always make your destructor non-public, to avoid any code deleting
|
| // the object accidently while there are references to it.
|
| +//
|
| +// The ref count manipulation to RefCounted is NOT thread safe and has DCHECKs
|
| +// to trap unsafe cross thread usage. A subclass instance of RefCounted can be
|
| +// passed to another execution sequence only when its ref count is 1. If the ref
|
| +// count is more than 1, the RefCounted class verifies the ref updates are made
|
| +// on the same execution sequence as the previous ones.
|
| +// In rare cases where thread-safety is guaranteed through other means (e.g.
|
| +// locks or explicit sequencing of calls across execution sequences):
|
| +// ScopedAllowCrossThreadRefCountAccess can be used to explicitly indicate this
|
| +// and disable the aforementioned check.
|
| template <class T>
|
| class RefCounted : public subtle::RefCountedBase {
|
| public:
|
|
|