Index: base/threading/sequenced_worker_pool.cc |
diff --git a/base/threading/sequenced_worker_pool.cc b/base/threading/sequenced_worker_pool.cc |
index 98306041f9ae6721b73961188dc413afbd174267..64f1bbd0bc563bbf3fd67de3d8fc03f76a8dba9d 100644 |
--- a/base/threading/sequenced_worker_pool.cc |
+++ b/base/threading/sequenced_worker_pool.cc |
@@ -93,12 +93,15 @@ struct SequencedTask : public TrackingInfo { |
~SequencedTask() {} |
+ SequencedTask(SequencedTask&&) = default; |
+ SequencedTask& operator=(SequencedTask&&) = default; |
+ |
int sequence_token_id; |
int trace_id; |
int64_t sequence_task_number; |
SequencedWorkerPool::WorkerShutdown shutdown_behavior; |
tracked_objects::Location posted_from; |
- Closure task; |
+ OnceClosure task; |
// Non-delayed tasks and delayed tasks are managed together by time-to-run |
// order. We calculate the time by adding the posted time and the given delay. |
@@ -140,7 +143,7 @@ class SequencedWorkerPoolTaskRunner : public TaskRunner { |
// TaskRunner implementation |
bool PostDelayedTask(const tracked_objects::Location& from_here, |
- Closure task, |
+ OnceClosure task, |
TimeDelta delay) override; |
bool RunsTasksOnCurrentThread() const override; |
@@ -164,7 +167,7 @@ SequencedWorkerPoolTaskRunner::~SequencedWorkerPoolTaskRunner() { |
bool SequencedWorkerPoolTaskRunner::PostDelayedTask( |
const tracked_objects::Location& from_here, |
- Closure task, |
+ OnceClosure task, |
TimeDelta delay) { |
if (delay.is_zero()) { |
return pool_->PostWorkerTaskWithShutdownBehavior(from_here, std::move(task), |
@@ -194,13 +197,13 @@ class SequencedWorkerPool::PoolSequencedTaskRunner |
// TaskRunner implementation |
bool PostDelayedTask(const tracked_objects::Location& from_here, |
- Closure task, |
+ OnceClosure task, |
TimeDelta delay) override; |
bool RunsTasksOnCurrentThread() const override; |
// SequencedTaskRunner implementation |
bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
- Closure task, |
+ OnceClosure task, |
TimeDelta delay) override; |
private: |
@@ -229,7 +232,7 @@ SequencedWorkerPool::PoolSequencedTaskRunner:: |
bool SequencedWorkerPool::PoolSequencedTaskRunner::PostDelayedTask( |
const tracked_objects::Location& from_here, |
- Closure task, |
+ OnceClosure task, |
TimeDelta delay) { |
if (delay.is_zero()) { |
return pool_->PostSequencedWorkerTaskWithShutdownBehavior( |
@@ -246,7 +249,7 @@ bool SequencedWorkerPool::PoolSequencedTaskRunner:: |
bool SequencedWorkerPool::PoolSequencedTaskRunner::PostNonNestableDelayedTask( |
const tracked_objects::Location& from_here, |
- Closure task, |
+ OnceClosure task, |
TimeDelta delay) { |
// There's no way to run nested tasks, so simply forward to |
// PostDelayedTask. |
@@ -349,7 +352,7 @@ class SequencedWorkerPool::Inner { |
SequenceToken sequence_token, |
WorkerShutdown shutdown_behavior, |
const tracked_objects::Location& from_here, |
- Closure task, |
+ OnceClosure task, |
TimeDelta delay); |
bool RunsTasksOnCurrentThread() const; |
@@ -394,8 +397,7 @@ class SequencedWorkerPool::Inner { |
// Returns true if the task may run at some point in the future and false if |
// it will definitely not run. |
// Coalesce upon resolution of http://crbug.com/622400. |
- bool PostTaskToTaskScheduler(const SequencedTask& sequenced, |
- const TimeDelta& delay); |
+ bool PostTaskToTaskScheduler(SequencedTask sequenced, const TimeDelta& delay); |
// Returns the TaskScheduler TaskRunner for the specified |sequence_token_id| |
// and |traits|. |
@@ -693,7 +695,7 @@ bool SequencedWorkerPool::Inner::PostTask( |
SequenceToken sequence_token, |
WorkerShutdown shutdown_behavior, |
const tracked_objects::Location& from_here, |
- Closure task, |
+ OnceClosure task, |
TimeDelta delay) { |
DCHECK(task); |
@@ -754,12 +756,14 @@ bool SequencedWorkerPool::Inner::PostTask( |
sequenced.sequence_token_id = LockedGetNamedTokenID(*optional_token_name); |
if (g_all_pools_state == AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER) { |
- if (!PostTaskToTaskScheduler(sequenced, delay)) |
+ if (!PostTaskToTaskScheduler(std::move(sequenced), delay)) |
return false; |
} else { |
- pending_tasks_.insert(sequenced); |
+ SequencedWorkerPool::WorkerShutdown shutdown_behavior = |
+ sequenced.shutdown_behavior; |
+ pending_tasks_.insert(std::move(sequenced)); |
- if (sequenced.shutdown_behavior == BLOCK_SHUTDOWN) |
+ if (shutdown_behavior == BLOCK_SHUTDOWN) |
blocking_shutdown_pending_task_count_++; |
create_thread_id = PrepareToStartAdditionalThreadIfHelpful(); |
@@ -796,7 +800,7 @@ bool SequencedWorkerPool::Inner::PostTask( |
} |
bool SequencedWorkerPool::Inner::PostTaskToTaskScheduler( |
- const SequencedTask& sequenced, |
+ SequencedTask sequenced, |
const TimeDelta& delay) { |
DCHECK_EQ(AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER, g_all_pools_state); |
@@ -826,7 +830,8 @@ bool SequencedWorkerPool::Inner::PostTaskToTaskScheduler( |
.WithPriority(task_priority_) |
.WithShutdownBehavior(task_shutdown_behavior); |
return GetTaskSchedulerTaskRunner(sequenced.sequence_token_id, traits) |
- ->PostDelayedTask(sequenced.posted_from, sequenced.task, delay); |
+ ->PostDelayedTask(sequenced.posted_from, std::move(sequenced.task), |
+ delay); |
} |
scoped_refptr<TaskRunner> |
@@ -1244,7 +1249,11 @@ SequencedWorkerPool::Inner::GetWorkStatus SequencedWorkerPool::Inner::GetWork( |
// refcounted, so we just need to keep a copy of them alive until the lock |
// is exited. The calling code can just clear() the vector they passed to |
// us once the lock is exited to make this happen. |
- delete_these_outside_lock->push_back(*i); |
+ // |
+ // The const_cast here is safe since the object is erased from |
+ // |pending_tasks_| soon after the move. |
+ delete_these_outside_lock->push_back( |
+ std::move(const_cast<SequencedTask&>(*i))); |
pending_tasks_.erase(i++); |
continue; |
} |
@@ -1255,14 +1264,18 @@ SequencedWorkerPool::Inner::GetWorkStatus SequencedWorkerPool::Inner::GetWork( |
status = GET_WORK_WAIT; |
if (cleanup_state_ == CLEANUP_RUNNING) { |
// Deferred tasks are deleted when cleaning up, see Inner::ThreadLoop. |
- delete_these_outside_lock->push_back(*i); |
+ // The const_cast here is safe since the object is erased from |
+ // |pending_tasks_| soon after the move. |
+ delete_these_outside_lock->push_back( |
+ std::move(const_cast<SequencedTask&>(*i))); |
pending_tasks_.erase(i); |
} |
break; |
} |
- // Found a runnable task. |
- *task = *i; |
+ // Found a runnable task. The const_cast is safe here since the object is |
+ // erased from |pending_tasks_| soon after the move. |
+ *task = std::move(const_cast<SequencedTask&>(*i)); |
pending_tasks_.erase(i); |
if (task->shutdown_behavior == BLOCK_SHUTDOWN) { |
blocking_shutdown_pending_task_count_--; |
@@ -1535,14 +1548,14 @@ SequencedWorkerPool::GetTaskRunnerWithShutdownBehavior( |
bool SequencedWorkerPool::PostWorkerTask( |
const tracked_objects::Location& from_here, |
- Closure task) { |
+ OnceClosure task) { |
return inner_->PostTask(NULL, SequenceToken(), BLOCK_SHUTDOWN, from_here, |
std::move(task), TimeDelta()); |
} |
bool SequencedWorkerPool::PostDelayedWorkerTask( |
const tracked_objects::Location& from_here, |
- Closure task, |
+ OnceClosure task, |
TimeDelta delay) { |
WorkerShutdown shutdown_behavior = |
delay.is_zero() ? BLOCK_SHUTDOWN : SKIP_ON_SHUTDOWN; |
@@ -1552,7 +1565,7 @@ bool SequencedWorkerPool::PostDelayedWorkerTask( |
bool SequencedWorkerPool::PostWorkerTaskWithShutdownBehavior( |
const tracked_objects::Location& from_here, |
- Closure task, |
+ OnceClosure task, |
WorkerShutdown shutdown_behavior) { |
return inner_->PostTask(NULL, SequenceToken(), shutdown_behavior, from_here, |
std::move(task), TimeDelta()); |
@@ -1561,7 +1574,7 @@ bool SequencedWorkerPool::PostWorkerTaskWithShutdownBehavior( |
bool SequencedWorkerPool::PostSequencedWorkerTask( |
SequenceToken sequence_token, |
const tracked_objects::Location& from_here, |
- Closure task) { |
+ OnceClosure task) { |
return inner_->PostTask(NULL, sequence_token, BLOCK_SHUTDOWN, from_here, |
std::move(task), TimeDelta()); |
} |
@@ -1569,7 +1582,7 @@ bool SequencedWorkerPool::PostSequencedWorkerTask( |
bool SequencedWorkerPool::PostDelayedSequencedWorkerTask( |
SequenceToken sequence_token, |
const tracked_objects::Location& from_here, |
- Closure task, |
+ OnceClosure task, |
TimeDelta delay) { |
WorkerShutdown shutdown_behavior = |
delay.is_zero() ? BLOCK_SHUTDOWN : SKIP_ON_SHUTDOWN; |
@@ -1580,7 +1593,7 @@ bool SequencedWorkerPool::PostDelayedSequencedWorkerTask( |
bool SequencedWorkerPool::PostNamedSequencedWorkerTask( |
const std::string& token_name, |
const tracked_objects::Location& from_here, |
- Closure task) { |
+ OnceClosure task) { |
DCHECK(!token_name.empty()); |
return inner_->PostTask(&token_name, SequenceToken(), BLOCK_SHUTDOWN, |
from_here, std::move(task), TimeDelta()); |
@@ -1589,7 +1602,7 @@ bool SequencedWorkerPool::PostNamedSequencedWorkerTask( |
bool SequencedWorkerPool::PostSequencedWorkerTaskWithShutdownBehavior( |
SequenceToken sequence_token, |
const tracked_objects::Location& from_here, |
- Closure task, |
+ OnceClosure task, |
WorkerShutdown shutdown_behavior) { |
return inner_->PostTask(NULL, sequence_token, shutdown_behavior, from_here, |
std::move(task), TimeDelta()); |
@@ -1597,7 +1610,7 @@ bool SequencedWorkerPool::PostSequencedWorkerTaskWithShutdownBehavior( |
bool SequencedWorkerPool::PostDelayedTask( |
const tracked_objects::Location& from_here, |
- Closure task, |
+ OnceClosure task, |
TimeDelta delay) { |
return PostDelayedWorkerTask(from_here, std::move(task), delay); |
} |