diff options
author | tzik <tzik@chromium.org> | 2017-03-29 14:28:12 +0900 |
---|---|---|
committer | Qijiang Fan <fqj@google.com> | 2020-06-05 05:41:02 +0900 |
commit | 622d59eaf5fa62675651d4e9b31c8fc738d171d7 (patch) | |
tree | 0990352a11cd013e6740170b1550b3b8b838b556 /base/message_loop/incoming_task_queue.cc | |
parent | 42048491e1c2d52cb467a2e377350fff2a5eccdf (diff) | |
download | libchrome-622d59eaf5fa62675651d4e9b31c8fc738d171d7.tar.gz |
Pass Callback to TaskRunner by value and consume it on invocation
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}
CrOS-Libchrome-Original-Commit: 070c8ffb44d04075bd7400691a2325cb6233558a
Diffstat (limited to 'base/message_loop/incoming_task_queue.cc')
-rw-r--r-- | base/message_loop/incoming_task_queue.cc | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/base/message_loop/incoming_task_queue.cc b/base/message_loop/incoming_task_queue.cc index c7faa9a5b0..f0df650adc 100644 --- a/base/message_loop/incoming_task_queue.cc +++ b/base/message_loop/incoming_task_queue.cc @@ -5,6 +5,7 @@ #include "base/message_loop/incoming_task_queue.h" #include <limits> +#include <utility> #include "base/location.h" #include "base/message_loop/message_loop.h" @@ -59,7 +60,7 @@ IncomingTaskQueue::IncomingTaskQueue(MessageLoop* message_loop) bool IncomingTaskQueue::AddToIncomingQueue( const tracked_objects::Location& from_here, - const Closure& task, + Closure task, TimeDelta delay, bool nestable) { DLOG_IF(WARNING, @@ -67,8 +68,8 @@ bool IncomingTaskQueue::AddToIncomingQueue( << "Requesting super-long task delay period of " << delay.InSeconds() << " seconds from here: " << from_here.ToString(); - PendingTask pending_task(from_here, task, CalculateDelayedRuntime(delay), - nestable); + PendingTask pending_task(from_here, std::move(task), + CalculateDelayedRuntime(delay), nestable); #if defined(OS_WIN) // We consider the task needs a high resolution timer if the delay is // more than 0 and less than 32ms. This caps the relative error to |