Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1052)

Unified Diff: chrome/browser/chrome_thread.h

Issue 338065: Add the ability for objects which derive from RefCountedThreadSafe to specify... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/ref_counted.h ('k') | chrome/browser/chrome_thread_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/chrome_thread.h
===================================================================
--- chrome/browser/chrome_thread.h (revision 30647)
+++ chrome/browser/chrome_thread.h (working copy)
@@ -125,6 +125,35 @@
// 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();
« no previous file with comments | « base/ref_counted.h ('k') | chrome/browser/chrome_thread_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698