diff options
author | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-02 06:10:30 +0000 |
---|---|---|
committer | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-02 06:10:30 +0000 |
commit | f671062f8c572d6729173c4ac7f058a1a89b5a1d (patch) | |
tree | 3eebe393f6f20310ba812662d092c3ca3c3c4d9e /base | |
parent | 6fad26338ed6119903826156f307e20fe6657c31 (diff) | |
download | chromium_src-f671062f8c572d6729173c4ac7f058a1a89b5a1d.zip chromium_src-f671062f8c572d6729173c4ac7f058a1a89b5a1d.tar.gz chromium_src-f671062f8c572d6729173c4ac7f058a1a89b5a1d.tar.bz2 |
Add the ability for objects which derive from RefCountedThreadSafe to specify a destructor trait. This allows browser objects to specify which thread they're terminated on. The benefit is we avoid the need to do manual ref counting when an object posts tasks to itself on different threads, if an object must be destructed on a specific thread.
This patch adds initial support and only shows one example with ResourceMessageFilter. I will do the rest in a follow-up patch to keep things small.
BUG=25354
TEST=added unit tests
Review URL: http://codereview.chromium.org/338065
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30688 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/ref_counted.h | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/base/ref_counted.h b/base/ref_counted.h index e04bb7c..ea532a4 100644 --- a/base/ref_counted.h +++ b/base/ref_counted.h @@ -93,6 +93,21 @@ class RefCounted : public subtle::RefCountedBase { DISALLOW_COPY_AND_ASSIGN(RefCounted<T>); }; +// Forward declaration. +template <class T, typename Traits> class RefCountedThreadSafe; + +// Default traits for RefCountedThreadSafe<T>. Deletes the object when its ref +// count reaches 0. Overload to delete it on a different thread etc. +template<typename T> +struct DefaultRefCountedThreadSafeTraits { + static void Destruct(T* x) { + // Delete through RefCountedThreadSafe to make child classes only need to be + // friend with RefCountedThreadSafe instead of this struct, which is an + // implementation detail. + RefCountedThreadSafe<T, DefaultRefCountedThreadSafeTraits>::DeleteInternal(x); + } +}; + // // A thread-safe variant of RefCounted<T> // @@ -100,7 +115,12 @@ class RefCounted : public subtle::RefCountedBase { // ... // }; // -template <class T> +// If you're using the default trait, then you may choose to add compile time +// asserts that no one else is deleting your object. i.e. +// private: +// friend struct base::RefCountedThreadSafe<MyFoo>; +// ~MyFoo(); +template <class T, typename Traits = DefaultRefCountedThreadSafeTraits<T> > class RefCountedThreadSafe : public subtle::RefCountedThreadSafeBase { public: RefCountedThreadSafe() { } @@ -112,12 +132,15 @@ class RefCountedThreadSafe : public subtle::RefCountedThreadSafeBase { void Release() { if (subtle::RefCountedThreadSafeBase::Release()) { - delete static_cast<T*>(this); + Traits::Destruct(static_cast<T*>(this)); } } private: - DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafe<T>); + friend struct DefaultRefCountedThreadSafeTraits<T>; + static void DeleteInternal(T* x) { delete x; } + + DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafe); }; // |