Skip to content
  • tzik's avatar
    Pass Callback to TaskRunner by value and consume it on invocation · 070c8ffb
    tzik authored
    This is a preparation CL for http://crrev.com/2637843002, which replaces
    the Callback parameter of TaskRunner::PostTask with OnceCallback.
    This one replaces the passed-by-const-ref Callback parameter of
    TaskRunner::PostTask() with pass-by-value.
    
    With the pass-by-const-ref manner as the old code does, we can't avoid
    leaving a reference to the callback object on the original thread. That
    is, the callback object may be destroyed either on the target thread or
    the original thread. That's problematic when a non-thread-safe object is
    bound to the callback.
    
    Pass-by-value and move() in this CL mitigate the nondeterminism: if the
    caller of TaskRunner::PostTask() passes the callback object as rvalue,
    TaskRunner::PostTask() leaves no reference on the original thread.
    I.e. the reference is not left if the callback is passed directly from
    Bind(), or passed with std::move() as below.
    
      task_runner->PostTask(FROM_HERE, base::Bind(&Foo));
    
      base::Closure cb = base::Bind(&Foo);
      task_runner->PostTask(FROM_HERE, std::move(cb));
    
    Otherwise, if the caller passes the callback as lvalue, a reference to
    the callback is left on the original thread as we do in the previous code.
    I.e. a reference is left if the callback is passed from other non-temporary
    variable.
    
      base::Closure cb = base::Bind(&Foo);
      task_runner->PostTask(FROM_HERE, cb);
    
    This is less controversial part of http://crrev.com/2637843002. This CL
    is mainly to land it incrementally.
    
    TBR=shrike@chromium.org
    BUG=704027
    CQ_INCLUDE_TRYBOTS=master.tryserver.blink:linux_trusty_blink_rel
    
    Review-Url: https://codereview.chromium.org/2726523002
    Cr-Commit-Position: refs/heads/master@{#460288}
    070c8ffb