Index: base/threading/sequenced_worker_pool.cc |
diff --git a/base/threading/sequenced_worker_pool.cc b/base/threading/sequenced_worker_pool.cc |
index fb71efeaea89c2f901a2e87ecebca02e25f45c39..63afbe803bc00a62768e6f3efb1c36deaa9b8742 100644 |
--- a/base/threading/sequenced_worker_pool.cc |
+++ b/base/threading/sequenced_worker_pool.cc |
@@ -89,6 +89,9 @@ struct SequencedTask : public TrackingInfo { |
sequence_task_number(0), |
shutdown_behavior(SequencedWorkerPool::BLOCK_SHUTDOWN) {} |
+ SequencedTask(SequencedTask&&) = default; |
+ SequencedTask& operator=(SequencedTask&&) = default; |
+ |
~SequencedTask() {} |
int sequence_token_id; |
@@ -96,7 +99,7 @@ struct SequencedTask : public TrackingInfo { |
int64_t sequence_task_number; |
SequencedWorkerPool::WorkerShutdown shutdown_behavior; |
tracked_objects::Location posted_from; |
- Closure task; |
+ mutable 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. |
@@ -130,7 +133,7 @@ class SequencedWorkerPoolTaskRunner : public TaskRunner { |
// TaskRunner implementation |
bool PostDelayedTask(const tracked_objects::Location& from_here, |
- const Closure& task, |
+ OnceClosure task, |
TimeDelta delay) override; |
bool RunsTasksOnCurrentThread() const override; |
@@ -154,13 +157,13 @@ SequencedWorkerPoolTaskRunner::~SequencedWorkerPoolTaskRunner() { |
bool SequencedWorkerPoolTaskRunner::PostDelayedTask( |
const tracked_objects::Location& from_here, |
- const Closure& task, |
+ OnceClosure task, |
TimeDelta delay) { |
if (delay.is_zero()) { |
- return pool_->PostWorkerTaskWithShutdownBehavior( |
- from_here, task, shutdown_behavior_); |
+ return pool_->PostWorkerTaskWithShutdownBehavior(from_here, std::move(task), |
+ shutdown_behavior_); |
} |
- return pool_->PostDelayedWorkerTask(from_here, task, delay); |
+ return pool_->PostDelayedWorkerTask(from_here, std::move(task), delay); |
} |
bool SequencedWorkerPoolTaskRunner::RunsTasksOnCurrentThread() const { |
@@ -181,13 +184,13 @@ class SequencedWorkerPoolSequencedTaskRunner : public SequencedTaskRunner { |
// TaskRunner implementation |
bool PostDelayedTask(const tracked_objects::Location& from_here, |
- const Closure& task, |
+ OnceClosure task, |
TimeDelta delay) override; |
bool RunsTasksOnCurrentThread() const override; |
// SequencedTaskRunner implementation |
bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
- const Closure& task, |
+ OnceClosure task, |
TimeDelta delay) override; |
private: |
@@ -216,13 +219,14 @@ SequencedWorkerPoolSequencedTaskRunner:: |
bool SequencedWorkerPoolSequencedTaskRunner::PostDelayedTask( |
const tracked_objects::Location& from_here, |
- const Closure& task, |
+ OnceClosure task, |
TimeDelta delay) { |
if (delay.is_zero()) { |
return pool_->PostSequencedWorkerTaskWithShutdownBehavior( |
- token_, from_here, task, shutdown_behavior_); |
+ token_, from_here, std::move(task), shutdown_behavior_); |
} |
- return pool_->PostDelayedSequencedWorkerTask(token_, from_here, task, delay); |
+ return pool_->PostDelayedSequencedWorkerTask(token_, from_here, |
+ std::move(task), delay); |
} |
bool SequencedWorkerPoolSequencedTaskRunner::RunsTasksOnCurrentThread() const { |
@@ -231,11 +235,11 @@ bool SequencedWorkerPoolSequencedTaskRunner::RunsTasksOnCurrentThread() const { |
bool SequencedWorkerPoolSequencedTaskRunner::PostNonNestableDelayedTask( |
const tracked_objects::Location& from_here, |
- const Closure& task, |
+ OnceClosure task, |
TimeDelta delay) { |
// There's no way to run nested tasks, so simply forward to |
// PostDelayedTask. |
- return PostDelayedTask(from_here, task, delay); |
+ return PostDelayedTask(from_here, std::move(task), delay); |
} |
// Create a process-wide unique ID to represent this task in trace events. This |
@@ -344,7 +348,7 @@ class SequencedWorkerPool::Inner { |
SequenceToken sequence_token, |
WorkerShutdown shutdown_behavior, |
const tracked_objects::Location& from_here, |
- const Closure& task, |
+ OnceClosure task, |
TimeDelta delay); |
bool RunsTasksOnCurrentThread() const; |
@@ -381,7 +385,7 @@ class SequencedWorkerPool::Inner { |
// Helper used by PostTask() to complete the work when redirection is on. |
// Coalesce upon resolution of http://crbug.com/622400. |
- void PostTaskToTaskScheduler(const SequencedTask& sequenced); |
+ void PostTaskToTaskScheduler(SequencedTask sequenced); |
// Called from within the lock, this converts the given token name into a |
// token ID, creating a new one if necessary. |
@@ -407,7 +411,7 @@ class SequencedWorkerPool::Inner { |
// See the implementation for a more detailed description. |
GetWorkStatus GetWork(SequencedTask* task, |
TimeDelta* wait_time, |
- std::vector<Closure>* delete_these_outside_lock); |
+ std::vector<OnceClosure>* delete_these_outside_lock); |
void HandleCleanup(); |
@@ -666,16 +670,16 @@ bool SequencedWorkerPool::Inner::PostTask( |
SequenceToken sequence_token, |
WorkerShutdown shutdown_behavior, |
const tracked_objects::Location& from_here, |
- const Closure& task, |
+ OnceClosure task, |
TimeDelta delay) { |
DCHECK(delay.is_zero() || shutdown_behavior == SKIP_ON_SHUTDOWN); |
SequencedTask sequenced(from_here); |
sequenced.sequence_token_id = sequence_token.id_; |
sequenced.shutdown_behavior = shutdown_behavior; |
sequenced.posted_from = from_here; |
- sequenced.task = |
- shutdown_behavior == BLOCK_SHUTDOWN ? |
- base::MakeCriticalClosure(task) : task; |
+ sequenced.task = shutdown_behavior == BLOCK_SHUTDOWN |
+ ? base::MakeCriticalClosure(std::move(task)) |
+ : std::move(task); |
sequenced.time_to_run = TimeTicks::Now() + delay; |
int create_thread_id = 0; |
@@ -719,11 +723,13 @@ bool SequencedWorkerPool::Inner::PostTask( |
if (subtle::NoBarrier_Load(&g_all_pools_state) == |
AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER) { |
- PostTaskToTaskScheduler(sequenced); |
+ PostTaskToTaskScheduler(std::move(sequenced)); |
} 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(); |
@@ -759,7 +765,7 @@ bool SequencedWorkerPool::Inner::PostTask( |
} |
void SequencedWorkerPool::Inner::PostTaskToTaskScheduler( |
- const SequencedTask& sequenced) { |
+ SequencedTask sequenced) { |
DCHECK_EQ(AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER, |
subtle::NoBarrier_Load(&g_all_pools_state)); |
@@ -806,7 +812,7 @@ void SequencedWorkerPool::Inner::PostTaskToTaskScheduler( |
if (sequenced_task_runner) { |
(*sequenced_task_runner) |
- ->PostTask(sequenced.posted_from, sequenced.task); |
+ ->PostTask(sequenced.posted_from, std::move(sequenced.task)); |
} else { |
// PostTaskWithTraits() posts a task with PARALLEL semantics. There are |
// however a few pools that use only one thread and therefore can currently |
@@ -817,7 +823,7 @@ void SequencedWorkerPool::Inner::PostTaskToTaskScheduler( |
// directly. |
DCHECK_GT(max_threads_, 1U); |
base::PostTaskWithTraits(sequenced.posted_from, pool_traits, |
- sequenced.task); |
+ std::move(sequenced.task)); |
} |
} |
@@ -954,7 +960,7 @@ void SequencedWorkerPool::Inner::ThreadLoop(Worker* this_worker) { |
// See GetWork for what delete_these_outside_lock is doing. |
SequencedTask task; |
TimeDelta wait_time; |
- std::vector<Closure> delete_these_outside_lock; |
+ std::vector<OnceClosure> delete_these_outside_lock; |
GetWorkStatus status = |
GetWork(&task, &wait_time, &delete_these_outside_lock); |
if (status == GET_WORK_FOUND) { |
@@ -985,7 +991,7 @@ void SequencedWorkerPool::Inner::ThreadLoop(Worker* this_worker) { |
tracked_objects::TaskStopwatch stopwatch; |
stopwatch.Start(); |
- task.task.Run(); |
+ std::move(task.task).Run(); |
stopwatch.Stop(); |
tracked_objects::ThreadData::TallyRunOnNamedThreadIfTracking( |
@@ -1137,7 +1143,7 @@ int64_t SequencedWorkerPool::Inner::LockedGetNextSequenceTaskNumber() { |
SequencedWorkerPool::Inner::GetWorkStatus SequencedWorkerPool::Inner::GetWork( |
SequencedTask* task, |
TimeDelta* wait_time, |
- std::vector<Closure>* delete_these_outside_lock) { |
+ std::vector<OnceClosure>* delete_these_outside_lock) { |
DCHECK_EQ(AllPoolsState::WORKER_CREATED, |
subtle::NoBarrier_Load(&g_all_pools_state)); |
@@ -1195,7 +1201,8 @@ SequencedWorkerPool::Inner::GetWorkStatus SequencedWorkerPool::Inner::GetWork( |
// 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->task); |
+ OnceClosure cb = std::move(i->task); |
+ delete_these_outside_lock->push_back(std::move(cb)); |
pending_tasks_.erase(i++); |
continue; |
} |
@@ -1206,14 +1213,14 @@ 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->task); |
+ delete_these_outside_lock->push_back(std::move(i->task)); |
pending_tasks_.erase(i); |
} |
break; |
} |
- // Found a runnable task. |
- *task = *i; |
+ // Found a runnable task.o |
+ *task = std::move(const_cast<SequencedTask&>(*i)); |
pending_tasks_.erase(i); |
if (task->shutdown_behavior == BLOCK_SHUTDOWN) { |
blocking_shutdown_pending_task_count_--; |
@@ -1483,71 +1490,71 @@ SequencedWorkerPool::GetTaskRunnerWithShutdownBehavior( |
bool SequencedWorkerPool::PostWorkerTask( |
const tracked_objects::Location& from_here, |
- const Closure& task) { |
- return inner_->PostTask(NULL, SequenceToken(), BLOCK_SHUTDOWN, |
- from_here, task, TimeDelta()); |
+ OnceClosure task) { |
+ return inner_->PostTask(NULL, SequenceToken(), BLOCK_SHUTDOWN, from_here, |
+ std::move(task), TimeDelta()); |
} |
bool SequencedWorkerPool::PostDelayedWorkerTask( |
const tracked_objects::Location& from_here, |
- const Closure& task, |
+ OnceClosure task, |
TimeDelta delay) { |
WorkerShutdown shutdown_behavior = |
delay.is_zero() ? BLOCK_SHUTDOWN : SKIP_ON_SHUTDOWN; |
- return inner_->PostTask(NULL, SequenceToken(), shutdown_behavior, |
- from_here, task, delay); |
+ return inner_->PostTask(NULL, SequenceToken(), shutdown_behavior, from_here, |
+ std::move(task), delay); |
} |
bool SequencedWorkerPool::PostWorkerTaskWithShutdownBehavior( |
const tracked_objects::Location& from_here, |
- const Closure& task, |
+ OnceClosure task, |
WorkerShutdown shutdown_behavior) { |
- return inner_->PostTask(NULL, SequenceToken(), shutdown_behavior, |
- from_here, task, TimeDelta()); |
+ return inner_->PostTask(NULL, SequenceToken(), shutdown_behavior, from_here, |
+ std::move(task), TimeDelta()); |
} |
bool SequencedWorkerPool::PostSequencedWorkerTask( |
SequenceToken sequence_token, |
const tracked_objects::Location& from_here, |
- const Closure& task) { |
- return inner_->PostTask(NULL, sequence_token, BLOCK_SHUTDOWN, |
- from_here, task, TimeDelta()); |
+ OnceClosure task) { |
+ return inner_->PostTask(NULL, sequence_token, BLOCK_SHUTDOWN, from_here, |
+ std::move(task), TimeDelta()); |
} |
bool SequencedWorkerPool::PostDelayedSequencedWorkerTask( |
SequenceToken sequence_token, |
const tracked_objects::Location& from_here, |
- const Closure& task, |
+ OnceClosure task, |
TimeDelta delay) { |
WorkerShutdown shutdown_behavior = |
delay.is_zero() ? BLOCK_SHUTDOWN : SKIP_ON_SHUTDOWN; |
- return inner_->PostTask(NULL, sequence_token, shutdown_behavior, |
- from_here, task, delay); |
+ return inner_->PostTask(NULL, sequence_token, shutdown_behavior, from_here, |
+ std::move(task), delay); |
} |
bool SequencedWorkerPool::PostNamedSequencedWorkerTask( |
const std::string& token_name, |
const tracked_objects::Location& from_here, |
- const Closure& task) { |
+ OnceClosure task) { |
DCHECK(!token_name.empty()); |
return inner_->PostTask(&token_name, SequenceToken(), BLOCK_SHUTDOWN, |
- from_here, task, TimeDelta()); |
+ from_here, std::move(task), TimeDelta()); |
} |
bool SequencedWorkerPool::PostSequencedWorkerTaskWithShutdownBehavior( |
SequenceToken sequence_token, |
const tracked_objects::Location& from_here, |
- const Closure& task, |
+ OnceClosure task, |
WorkerShutdown shutdown_behavior) { |
- return inner_->PostTask(NULL, sequence_token, shutdown_behavior, |
- from_here, task, TimeDelta()); |
+ return inner_->PostTask(NULL, sequence_token, shutdown_behavior, from_here, |
+ std::move(task), TimeDelta()); |
} |
bool SequencedWorkerPool::PostDelayedTask( |
const tracked_objects::Location& from_here, |
- const Closure& task, |
+ OnceClosure task, |
TimeDelta delay) { |
- return PostDelayedWorkerTask(from_here, task, delay); |
+ return PostDelayedWorkerTask(from_here, std::move(task), delay); |
} |
bool SequencedWorkerPool::RunsTasksOnCurrentThread() const { |