diff options
author | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-18 23:13:01 +0000 |
---|---|---|
committer | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-18 23:13:01 +0000 |
commit | c31af70db21e370eee35a677f6fcc62a2963784a (patch) | |
tree | b3f1908471efc10ac35d61cbffa2c7d160d6247f /base/message_loop_proxy.h | |
parent | d55c2380d728c2cc29c1a84970bb1d6f51c397d7 (diff) | |
download | chromium_src-c31af70db21e370eee35a677f6fcc62a2963784a.zip chromium_src-c31af70db21e370eee35a677f6fcc62a2963784a.tar.gz chromium_src-c31af70db21e370eee35a677f6fcc62a2963784a.tar.bz2 |
Implementation of PostTaskAndReply() in MessageLoopProxy and BrowserThread.
This ensures that the request/reply closures are always deleted on the origin
thread, or leaked if the task cannot be completed (due to message loop
shutdown).
BUG=86301
TEST=new unittests
Review URL: http://codereview.chromium.org/7210053
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@97387 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/message_loop_proxy.h')
-rw-r--r-- | base/message_loop_proxy.h | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/base/message_loop_proxy.h b/base/message_loop_proxy.h index 9a856bf..decaa38 100644 --- a/base/message_loop_proxy.h +++ b/base/message_loop_proxy.h @@ -70,6 +70,51 @@ class BASE_EXPORT MessageLoopProxy // this proxy represents. virtual bool BelongsToCurrentThread() = 0; + // Executes |task| on the given MessageLoopProxy. On completion, |reply| + // is passed back to the MessageLoopProxy for the thread that called + // PostTaskAndReply(). Both |task| and |reply| are guaranteed to be deleted + // on the thread from which PostTaskAndReply() is invoked. This allows + // objects that must be deleted on the originating thread to be bound into the + // |task| and |reply| Closures. In particular, it can be useful to use + // WeakPtr<> in the |reply| Closure so that the reply operation can be + // canceled. See the following pseudo-code: + // + // class DataBuffer : public RefCountedThreadSafe<DataBuffer> { + // public: + // // Called to add data into a buffer. + // void AddData(void* buf, size_t length); + // ... + // }; + // + // + // class DataLoader : public SupportsWeakPtr<ReadToBuffer> { + // public: + // void GetData() { + // scoped_refptr<DataBuffer> buffer = new DataBuffer(); + // target_thread_.message_loop_proxy()->PostTaskAndReply( + // FROM_HERE, + // base::Bind(&DataBuffer::AddData, buffer), + // base::Bind(&DataLoader::OnDataReceived, AsWeakPtr(), buffer)); + // } + // + // private: + // void OnDataReceived(scoped_refptr<DataBuffer> buffer) { + // // Do something with buffer. + // } + // }; + // + // + // Things to notice: + // * Results of |task| are shared with |reply| by binding a shared argument + // (a DataBuffer instance). + // * The DataLoader object has no special thread safety. + // * The DataLoader object can be deleted while |task| is still running, + // and the reply will cancel itself safely because it is bound to a + // WeakPtr<>. + bool PostTaskAndReply(const tracked_objects::Location& from_here, + const Closure& task, + const Closure& reply); + template <class T> bool DeleteSoon(const tracked_objects::Location& from_here, T* object) { |