Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: base/threading/sequenced_worker_pool.cc

Issue 2823103003: Introduce TaskRunner::RunsTasksInCurrentSequence() (Closed)
Patch Set: remove RunsTasksOnCurrentThread() overrided. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/threading/sequenced_worker_pool.h" 5 #include "base/threading/sequenced_worker_pool.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <list> 9 #include <list>
10 #include <map> 10 #include <map>
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 class SequencedWorkerPoolTaskRunner : public TaskRunner { 138 class SequencedWorkerPoolTaskRunner : public TaskRunner {
139 public: 139 public:
140 SequencedWorkerPoolTaskRunner( 140 SequencedWorkerPoolTaskRunner(
141 scoped_refptr<SequencedWorkerPool> pool, 141 scoped_refptr<SequencedWorkerPool> pool,
142 SequencedWorkerPool::WorkerShutdown shutdown_behavior); 142 SequencedWorkerPool::WorkerShutdown shutdown_behavior);
143 143
144 // TaskRunner implementation 144 // TaskRunner implementation
145 bool PostDelayedTask(const tracked_objects::Location& from_here, 145 bool PostDelayedTask(const tracked_objects::Location& from_here,
146 OnceClosure task, 146 OnceClosure task,
147 TimeDelta delay) override; 147 TimeDelta delay) override;
148 bool RunsTasksOnCurrentThread() const override; 148 bool RunsTasksInCurrentSequence() const override;
149 149
150 private: 150 private:
151 ~SequencedWorkerPoolTaskRunner() override; 151 ~SequencedWorkerPoolTaskRunner() override;
152 152
153 const scoped_refptr<SequencedWorkerPool> pool_; 153 const scoped_refptr<SequencedWorkerPool> pool_;
154 154
155 const SequencedWorkerPool::WorkerShutdown shutdown_behavior_; 155 const SequencedWorkerPool::WorkerShutdown shutdown_behavior_;
156 156
157 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPoolTaskRunner); 157 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPoolTaskRunner);
158 }; 158 };
(...skipping 10 matching lines...) Expand all
169 const tracked_objects::Location& from_here, 169 const tracked_objects::Location& from_here,
170 OnceClosure task, 170 OnceClosure task,
171 TimeDelta delay) { 171 TimeDelta delay) {
172 if (delay.is_zero()) { 172 if (delay.is_zero()) {
173 return pool_->PostWorkerTaskWithShutdownBehavior(from_here, std::move(task), 173 return pool_->PostWorkerTaskWithShutdownBehavior(from_here, std::move(task),
174 shutdown_behavior_); 174 shutdown_behavior_);
175 } 175 }
176 return pool_->PostDelayedWorkerTask(from_here, std::move(task), delay); 176 return pool_->PostDelayedWorkerTask(from_here, std::move(task), delay);
177 } 177 }
178 178
179 bool SequencedWorkerPoolTaskRunner::RunsTasksOnCurrentThread() const { 179 bool SequencedWorkerPoolTaskRunner::RunsTasksInCurrentSequence() const {
180 return pool_->RunsTasksOnCurrentThread(); 180 return pool_->RunsTasksInCurrentSequence();
181 } 181 }
182 182
183 } // namespace 183 } // namespace
184 184
185 // SequencedWorkerPool::PoolSequencedTaskRunner ------------------------------ 185 // SequencedWorkerPool::PoolSequencedTaskRunner ------------------------------
186 // A SequencedTaskRunner which posts tasks to a SequencedWorkerPool with a 186 // A SequencedTaskRunner which posts tasks to a SequencedWorkerPool with a
187 // fixed sequence token. 187 // fixed sequence token.
188 // 188 //
189 // Note that this class is RefCountedThreadSafe (inherited from TaskRunner). 189 // Note that this class is RefCountedThreadSafe (inherited from TaskRunner).
190 class SequencedWorkerPool::PoolSequencedTaskRunner 190 class SequencedWorkerPool::PoolSequencedTaskRunner
191 : public SequencedTaskRunner { 191 : public SequencedTaskRunner {
192 public: 192 public:
193 PoolSequencedTaskRunner( 193 PoolSequencedTaskRunner(
194 scoped_refptr<SequencedWorkerPool> pool, 194 scoped_refptr<SequencedWorkerPool> pool,
195 SequencedWorkerPool::SequenceToken token, 195 SequencedWorkerPool::SequenceToken token,
196 SequencedWorkerPool::WorkerShutdown shutdown_behavior); 196 SequencedWorkerPool::WorkerShutdown shutdown_behavior);
197 197
198 // TaskRunner implementation 198 // TaskRunner implementation
199 bool PostDelayedTask(const tracked_objects::Location& from_here, 199 bool PostDelayedTask(const tracked_objects::Location& from_here,
200 OnceClosure task, 200 OnceClosure task,
201 TimeDelta delay) override; 201 TimeDelta delay) override;
202 bool RunsTasksOnCurrentThread() const override; 202 bool RunsTasksInCurrentSequence() const override;
203
203 204
204 // SequencedTaskRunner implementation 205 // SequencedTaskRunner implementation
205 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, 206 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
206 OnceClosure task, 207 OnceClosure task,
207 TimeDelta delay) override; 208 TimeDelta delay) override;
208 209
209 private: 210 private:
210 ~PoolSequencedTaskRunner() override; 211 ~PoolSequencedTaskRunner() override;
211 212
212 const scoped_refptr<SequencedWorkerPool> pool_; 213 const scoped_refptr<SequencedWorkerPool> pool_;
(...skipping 23 matching lines...) Expand all
236 TimeDelta delay) { 237 TimeDelta delay) {
237 if (delay.is_zero()) { 238 if (delay.is_zero()) {
238 return pool_->PostSequencedWorkerTaskWithShutdownBehavior( 239 return pool_->PostSequencedWorkerTaskWithShutdownBehavior(
239 token_, from_here, std::move(task), shutdown_behavior_); 240 token_, from_here, std::move(task), shutdown_behavior_);
240 } 241 }
241 return pool_->PostDelayedSequencedWorkerTask(token_, from_here, 242 return pool_->PostDelayedSequencedWorkerTask(token_, from_here,
242 std::move(task), delay); 243 std::move(task), delay);
243 } 244 }
244 245
245 bool SequencedWorkerPool::PoolSequencedTaskRunner:: 246 bool SequencedWorkerPool::PoolSequencedTaskRunner::
246 RunsTasksOnCurrentThread() const { 247 RunsTasksInCurrentSequence() const {
247 return pool_->IsRunningSequenceOnCurrentThread(token_); 248 return pool_->IsRunningSequenceOnCurrentThread(token_);
248 } 249 }
249 250
250 bool SequencedWorkerPool::PoolSequencedTaskRunner::PostNonNestableDelayedTask( 251 bool SequencedWorkerPool::PoolSequencedTaskRunner::PostNonNestableDelayedTask(
251 const tracked_objects::Location& from_here, 252 const tracked_objects::Location& from_here,
252 OnceClosure task, 253 OnceClosure task,
253 TimeDelta delay) { 254 TimeDelta delay) {
254 // There's no way to run nested tasks, so simply forward to 255 // There's no way to run nested tasks, so simply forward to
255 // PostDelayedTask. 256 // PostDelayedTask.
256 return PostDelayedTask(from_here, std::move(task), delay); 257 return PostDelayedTask(from_here, std::move(task), delay);
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 // This function accepts a name and an ID. If the name is null, the 349 // This function accepts a name and an ID. If the name is null, the
349 // token ID is used. This allows us to implement the optional name lookup 350 // token ID is used. This allows us to implement the optional name lookup
350 // from a single function without having to enter the lock a separate time. 351 // from a single function without having to enter the lock a separate time.
351 bool PostTask(const std::string* optional_token_name, 352 bool PostTask(const std::string* optional_token_name,
352 SequenceToken sequence_token, 353 SequenceToken sequence_token,
353 WorkerShutdown shutdown_behavior, 354 WorkerShutdown shutdown_behavior,
354 const tracked_objects::Location& from_here, 355 const tracked_objects::Location& from_here,
355 OnceClosure task, 356 OnceClosure task,
356 TimeDelta delay); 357 TimeDelta delay);
357 358
358 bool RunsTasksOnCurrentThread() const; 359 bool RunsTasksOnCurrentThread() const;
gab 2017/04/18 15:00:33 We should be able to get rid of this one too? No e
360 bool RunsTasksInCurrentSequence() const;
359 361
360 bool IsRunningSequenceOnCurrentThread(SequenceToken sequence_token) const; 362 bool IsRunningSequenceOnCurrentThread(SequenceToken sequence_token) const;
361 363
362 void CleanupForTesting(); 364 void CleanupForTesting();
363 365
364 void SignalHasWorkForTesting(); 366 void SignalHasWorkForTesting();
365 367
366 int GetWorkSignalCountForTesting() const; 368 int GetWorkSignalCountForTesting() const;
367 369
368 void Shutdown(int max_blocking_tasks_after_shutdown); 370 void Shutdown(int max_blocking_tasks_after_shutdown);
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after
866 868
867 if (!task_runner) { 869 if (!task_runner) {
868 task_runner = sequence_token_id 870 task_runner = sequence_token_id
869 ? CreateSequencedTaskRunnerWithTraits(traits) 871 ? CreateSequencedTaskRunnerWithTraits(traits)
870 : CreateTaskRunnerWithTraits(traits); 872 : CreateTaskRunnerWithTraits(traits);
871 } 873 }
872 874
873 return task_runner; 875 return task_runner;
874 } 876 }
875 877
876 bool SequencedWorkerPool::Inner::RunsTasksOnCurrentThread() const { 878 bool SequencedWorkerPool::Inner::RunsTasksOnCurrentThread() const {
gab 2017/04/18 15:00:33 Same with this impl?
877 AutoLock lock(lock_); 879 AutoLock lock(lock_);
878 if (g_all_pools_state == AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER) { 880 if (g_all_pools_state == AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER) {
879 if (!runs_tasks_on_verifier_) { 881 if (!runs_tasks_on_verifier_) {
880 runs_tasks_on_verifier_ = CreateTaskRunnerWithTraits( 882 runs_tasks_on_verifier_ = CreateTaskRunnerWithTraits(
881 TaskTraits().MayBlock().WithBaseSyncPrimitives().WithPriority( 883 TaskTraits().MayBlock().WithBaseSyncPrimitives().WithPriority(
882 task_priority_)); 884 task_priority_));
883 } 885 }
884 return runs_tasks_on_verifier_->RunsTasksOnCurrentThread(); 886 return runs_tasks_on_verifier_->RunsTasksOnCurrentThread();
885 } else { 887 } else {
886 return ContainsKey(threads_, PlatformThread::CurrentId()); 888 return ContainsKey(threads_, PlatformThread::CurrentId());
887 } 889 }
888 } 890 }
889 891
892 bool SequencedWorkerPool::Inner::RunsTasksInCurrentSequence() const {
893 AutoLock lock(lock_);
894 if (g_all_pools_state == AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER) {
895 if (!runs_tasks_on_verifier_) {
896 runs_tasks_on_verifier_ = CreateTaskRunnerWithTraits(
897 TaskTraits().MayBlock().WithBaseSyncPrimitives().WithPriority(
898 task_priority_));
899 }
900 return runs_tasks_on_verifier_->RunsTasksInCurrentSequence();
901 } else {
902 return ContainsKey(threads_, PlatformThread::CurrentId());
903 }
904 }
905
890 bool SequencedWorkerPool::Inner::IsRunningSequenceOnCurrentThread( 906 bool SequencedWorkerPool::Inner::IsRunningSequenceOnCurrentThread(
891 SequenceToken sequence_token) const { 907 SequenceToken sequence_token) const {
892 DCHECK(sequence_token.IsValid()); 908 DCHECK(sequence_token.IsValid());
893 909
894 AutoLock lock(lock_); 910 AutoLock lock(lock_);
895 911
896 if (g_all_pools_state == AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER) { 912 if (g_all_pools_state == AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER) {
897 const auto sequenced_task_runner_it = 913 const auto sequenced_task_runner_it =
898 sequenced_task_runner_map_.find(sequence_token.id_); 914 sequenced_task_runner_map_.find(sequence_token.id_);
899 return sequenced_task_runner_it != sequenced_task_runner_map_.end() && 915 return sequenced_task_runner_it != sequenced_task_runner_map_.end() &&
(...skipping 708 matching lines...) Expand 10 before | Expand all | Expand 10 after
1608 std::move(task), TimeDelta()); 1624 std::move(task), TimeDelta());
1609 } 1625 }
1610 1626
1611 bool SequencedWorkerPool::PostDelayedTask( 1627 bool SequencedWorkerPool::PostDelayedTask(
1612 const tracked_objects::Location& from_here, 1628 const tracked_objects::Location& from_here,
1613 OnceClosure task, 1629 OnceClosure task,
1614 TimeDelta delay) { 1630 TimeDelta delay) {
1615 return PostDelayedWorkerTask(from_here, std::move(task), delay); 1631 return PostDelayedWorkerTask(from_here, std::move(task), delay);
1616 } 1632 }
1617 1633
1618 bool SequencedWorkerPool::RunsTasksOnCurrentThread() const { 1634 bool SequencedWorkerPool::RunsTasksInCurrentSequence() const {
1619 return inner_->RunsTasksOnCurrentThread(); 1635 return inner_->RunsTasksInCurrentSequence();
1620 } 1636 }
1621 1637
1622 void SequencedWorkerPool::FlushForTesting() { 1638 void SequencedWorkerPool::FlushForTesting() {
1623 DCHECK(!RunsTasksOnCurrentThread()); 1639 DCHECK(!RunsTasksOnCurrentThread());
1624 base::ThreadRestrictions::ScopedAllowWait allow_wait; 1640 base::ThreadRestrictions::ScopedAllowWait allow_wait;
1625 if (g_all_pools_state == AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER) { 1641 if (g_all_pools_state == AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER) {
1626 // TODO(gab): Remove this if http://crbug.com/622400 fails. 1642 // TODO(gab): Remove this if http://crbug.com/622400 fails.
1627 TaskScheduler::GetInstance()->FlushForTesting(); 1643 TaskScheduler::GetInstance()->FlushForTesting();
1628 } else { 1644 } else {
1629 inner_->CleanupForTesting(); 1645 inner_->CleanupForTesting();
(...skipping 12 matching lines...) Expand all
1642 bool SequencedWorkerPool::IsShutdownInProgress() { 1658 bool SequencedWorkerPool::IsShutdownInProgress() {
1643 return inner_->IsShutdownInProgress(); 1659 return inner_->IsShutdownInProgress();
1644 } 1660 }
1645 1661
1646 bool SequencedWorkerPool::IsRunningSequenceOnCurrentThread( 1662 bool SequencedWorkerPool::IsRunningSequenceOnCurrentThread(
1647 SequenceToken sequence_token) const { 1663 SequenceToken sequence_token) const {
1648 return inner_->IsRunningSequenceOnCurrentThread(sequence_token); 1664 return inner_->IsRunningSequenceOnCurrentThread(sequence_token);
1649 } 1665 }
1650 1666
1651 } // namespace base 1667 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698