If you use NewRunnableMethod or NewCallback, this mail concerns you!We're looking for people to help convert all the NewRunnableMethod() and NewCallback() calls over to base::Bind(). Because these constructs are so widely used. we need all the help we can get!Luckily, the syntax change should be fairly easy. Recipes for common conversions and a few caveats are listed at the end of this mail. The general procedure should be:1) Pick a file.2) Apply recipes below.3) Fix compile errors and memory suppressions as necessary.4) Rinse. Repeat with next file.I've created a spreadsheet listing all the files that have constructs needing changes. If you decide to help out, please put your name next to the file you are working on here (make sure to be signed in with your @chromium.org account):http://goo.gl/89ChrWhen you are finish with a file, add an "x" in the done column. I'll periodically go through and shift the finished files down to the bottom of the list.Note that I am also working on Clang plugin (http://codereview.chromium.org/7886056/) to aide in this, but haven't gotten the kinks worked out yet. When/if I get that working, I'll send an update.Let me know if there are any questions or concerns!-Albert== NewCallback ==1) Callback2<int, double>::Type* callback ==> base::Callback<void(int, double)> callback;2) callback = NewCallback(this, &Foo::func) ==>callback = base::Bind(&Foo::func, base::Unretained(this));
Be sure to add base::Unretained() around the object that give to Bind() when replacing a NewCallback call. Otherwise, base::Bind() will take a reference on the object which will change the memory handling behavior.
== NewRunnableMethod() ==1) Task* task ==> base::Closure task2) task = NewRunnableMethod(this, &Foo::func, a, b) ==>task = base::Bind(&Foo::func, this, a, b)3) ml->PostTask(FROM_HERE, NewRunnableMethod(this, &Foo::func, a, b) ==>ml->PostTask(FROM_HERE, base::Bind(&Foo::DoSomething, this, a, b)IMPORTANT: When using base::Bind() with PostTask, it is vital that all bound arguments are safe to delete from the originalting thread, and the thread that it is being posted onto. Unlike NewRunnableMethod(), the result of base::Bind() is refcounted, and may be deleted on either thread. This should not be an issue if the parameters (eg., a and b above) are primitive types, or things like scoped_refptrs. However, if they are custom, copyable objects, then be careful.== ScopedRunnableMethodFactory and ScopedCallbackFactory ==1) ScopedRunnableMethodFactory<Foo> callback_factory_ ==> WeakPtrFactory<Foo> weak_factory_2) callback_factory_.NewRunnableMethod(this, &Foo::func, a, b) ==>base::Bind(&Foo::func, weak_factory_.GetWeakPtr(), a, b)3) callback_factory.RevokeAll() ==> weak_factory_->InvalidateWeakPtrs()The ScopedCallbackFactory conversion is almost exactly the same.Note, you cannot cancel individual callbacks created from the factory yet. If you really need that feature, please let me know.== Custom Implementations of Task ==Most of the time people inherit from Task(), they do so in order to carry some context around. Consider trying to remove the derived class and instead use currying with base::Bind() on a normal function to get the desired behavior.
Feel free to e-mail back to this thread with questions if you can't figure out how to replicate a custom Task class using base::Bind.== More Info ==For detail documentation on base::Bind and base::Callback, please see file comments here:The bind_unittest also has lots of examples for how to use this API.
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
Can you update the threading wiki on http://www.chromium.org/developers/design-documents/threading with the new recommended way?I have been mostly ignoring this work, so I've been using the old way because that's what I'm used for (and I have some stuff I wasn't sure about). A few questions below. If you can also put that in the wiki, that'd be great :)
On Tue, Sep 27, 2011 at 9:37 AM, Albert J. Wong (王重傑) <ajw...@chromium.org> wrote:If you use NewRunnableMethod or NewCallback, this mail concerns you!We're looking for people to help convert all the NewRunnableMethod() and NewCallback() calls over to base::Bind(). Because these constructs are so widely used. we need all the help we can get!Luckily, the syntax change should be fairly easy. Recipes for common conversions and a few caveats are listed at the end of this mail. The general procedure should be:1) Pick a file.2) Apply recipes below.3) Fix compile errors and memory suppressions as necessary.4) Rinse. Repeat with next file.I've created a spreadsheet listing all the files that have constructs needing changes. If you decide to help out, please put your name next to the file you are working on here (make sure to be signed in with your @chromium.org account):http://goo.gl/89ChrWhen you are finish with a file, add an "x" in the done column. I'll periodically go through and shift the finished files down to the bottom of the list.Note that I am also working on Clang plugin (http://codereview.chromium.org/7886056/) to aide in this, but haven't gotten the kinks worked out yet. When/if I get that working, I'll send an update.Let me know if there are any questions or concerns!-Albert== NewCallback ==1) Callback2<int, double>::Type* callback ==> base::Callback<void(int, double)> callback;2) callback = NewCallback(this, &Foo::func) ==>callback = base::Bind(&Foo::func, base::Unretained(this));Be sure to add base::Unretained() around the object that give to Bind() when replacing a NewCallback call. Otherwise, base::Bind() will take a reference on the object which will change the memory handling behavior.So just to be clear, obviously this will only take a reference if the object is ref-counted. What if the object isn't, what happens in that case? i.e. what if someone tries to call the callback after the object is gone, will it crash or will it do nothing?
> Note that I am also working on Clang
> plugin (http://codereview.chromium.org/7886056/) to aide in this, but
> haven't gotten the kinks worked out yet. When/if I get that working, I'll
> send an update.
Should folks delay work on manually converting over to the new
callback system while you work on the automated converter?
I'm guessing no given this email, but it's hard to get motivated to do
work that robots might be able to do for us.
== NewCallback ==1) Callback2<int, double>::Type* callback ==> base::Callback<void(int, double)> callback;
Let me know if there are any questions or concerns!
I had been meaning to reply for a while now. I think you could leverage PostTaskAndReplyfor the history system by either sending a ref-counted bool with each task that the taskknows to check before doing its work, or you could send an integer, and each task checkthat integer against a global counter that is incremented each time we wish to dispose ofold work. It depends on what kinds of cancellation semantics you need.The point is that the proxying of a request onto a background thread and then back againto the originating thread is non-trivial, and we should not need to repeat that code.
-Albert
I've got some reading up todo on all this new infrastructure. PostTaskAndReply as a base function might be nice.
IMHO the old names were clearer from the perspective of a developer
using them. It's pretty clear ScopedRunnableMethodFactory is used to
generate something that is going to invoke a method. Similarly empty()
is consistent with stl (for better or for worse). RevokeAll is also
pretty clear.
Compare that with base::WeakPtrFactory, HasWeakPtrs and
InvalidateWeakPtrs. Without being in the know WeakPtrFactory is an
extremely generic name and not particularly obvious what it is used
for.
-Scott
I did find it confusing that the ability to have a cancelable callback
is buried in the sense of whether you passed in a weak ptr at P1. It
does map directly from the old code (there is no
ScopedRunnableFunctionFactory<>, after all), but it feels like a wart
in the new code. It seems like "canceled" should be an entirely
separate concept from "bound parameters".
-scott