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 /chrome/browser/chrome_thread.h | |
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 'chrome/browser/chrome_thread.h')
-rw-r--r-- | chrome/browser/chrome_thread.h | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/chrome/browser/chrome_thread.h b/chrome/browser/chrome_thread.h index 4240127..8f64dde 100644 --- a/chrome/browser/chrome_thread.h +++ b/chrome/browser/chrome_thread.h @@ -125,6 +125,35 @@ class ChromeThread : public base::Thread { // sets identifier to its ID. Otherwise returns false. static bool GetCurrentThreadIdentifier(ID* identifier); + // Use these templates in conjuction with RefCountedThreadSafe when you want + // to ensure that an object is deleted on a specific thread. This is needed + // when an object can hop between threads (i.e. IO -> FILE -> IO), and thread + // switching delays can mean that the final IO tasks executes before the FILE + // task's stack unwinds. This would lead to the object destructing on the + // FILE thread, which often is not what you want (i.e. to unregister from + // NotificationService, to notify other objects on the creating thread etc). + template<ID thread> + struct DeleteOnThread { + template<typename T> + static void Destruct(T* x) { + if (CurrentlyOn(thread)) { + delete x; + } else { + DeleteSoon(thread, FROM_HERE, x); + } + } + }; + + // Sample usage: + // class Foo + // : public base::RefCountedThreadSafe< + // Foo, ChromeThread::DeleteOnIOThread> { + struct DeleteOnUIThread : public DeleteOnThread<UI> { }; + struct DeleteOnIOThread : public DeleteOnThread<IO> { }; + struct DeleteOnFileThread : public DeleteOnThread<FILE> { }; + struct DeleteOnDBThread : public DeleteOnThread<DB> { }; + struct DeleteOnWebKitThread : public DeleteOnThread<WEBKIT> { }; + private: // Common initialization code for the constructors. void Initialize(); |