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

Side by Side Diff: base/task_scheduler/task_scheduler_impl.cc

Issue 1701343003: TaskScheduler [13] TaskSchedulerImpl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s_6_threadpool
Patch Set: fix ios build error Created 4 years, 7 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
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/task_scheduler/task_scheduler_impl.h"
6
7 #include <utility>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/task_scheduler/sequence_sort_key.h"
13 #include "base/task_scheduler/task.h"
14 #include "base/time/time.h"
15
16 namespace base {
17 namespace internal {
18
19 // static
20 std::unique_ptr<TaskSchedulerImpl> TaskSchedulerImpl::Create() {
21 std::unique_ptr<TaskSchedulerImpl> scheduler(new TaskSchedulerImpl);
22 scheduler->Initialize();
23 return scheduler;
24 }
25
26 TaskSchedulerImpl::~TaskSchedulerImpl() {
27 #if DCHECK_IS_ON()
28 DCHECK(join_for_testing_returned_.IsSignaled());
29 #endif
30 }
31
32 void TaskSchedulerImpl::PostTaskWithTraits(
33 const tracked_objects::Location& from_here,
34 const TaskTraits& traits,
35 const Closure& task) {
36 // Post |task| as part of a one-off single-task Sequence.
37 GetThreadPoolForTraits(traits)->PostTaskWithSequence(
38 WrapUnique(new Task(from_here, task, traits, TimeDelta())),
39 make_scoped_refptr(new Sequence), nullptr);
40 }
41
42 scoped_refptr<TaskRunner> TaskSchedulerImpl::CreateTaskRunnerWithTraits(
43 const TaskTraits& traits,
44 ExecutionMode execution_mode) {
45 return GetThreadPoolForTraits(traits)->CreateTaskRunnerWithTraits(
46 traits, execution_mode);
47 }
48
49 void TaskSchedulerImpl::Shutdown() {
50 // TODO(fdoray): Increase the priority of BACKGROUND tasks blocking shutdown.
51 task_tracker_.Shutdown();
52 }
53
54 void TaskSchedulerImpl::JoinForTesting() {
55 #if DCHECK_IS_ON()
56 DCHECK(!join_for_testing_returned_.IsSignaled());
57 #endif
58 background_thread_pool_->JoinForTesting();
59 background_file_io_thread_pool_->JoinForTesting();
60 normal_thread_pool_->JoinForTesting();
61 normal_file_io_thread_pool_->JoinForTesting();
62 #if DCHECK_IS_ON()
63 join_for_testing_returned_.Signal();
64 #endif
65 }
66
67 TaskSchedulerImpl::TaskSchedulerImpl()
68 // TODO(robliao): Wake up the service thread instead of calling DoNothing()
69 // when the delayed run time changes.
70 : delayed_task_manager_(Bind(&DoNothing))
71 #if DCHECK_IS_ON()
72 ,
73 join_for_testing_returned_(true, false)
74 #endif
75 {
76 }
77
78 void TaskSchedulerImpl::Initialize() {
79 using IORestriction = SchedulerThreadPoolImpl::IORestriction;
80
81 const SchedulerThreadPoolImpl::ReEnqueueSequenceCallback
82 re_enqueue_sequence_callback =
83 Bind(&TaskSchedulerImpl::ReEnqueueSequenceCallback, Unretained(this));
84
85 // TODO(fdoray): Derive the number of threads per pool from hardware
86 // characteristics rather than using hard-coded constants.
87
88 // Passing pointers to objects owned by |this| to
89 // SchedulerThreadPoolImpl::Create() is safe because a TaskSchedulerImpl can't
90 // be deleted before all its thread pools have been joined.
91 background_thread_pool_ = SchedulerThreadPoolImpl::Create(
92 ThreadPriority::BACKGROUND, 1U, IORestriction::DISALLOWED,
93 re_enqueue_sequence_callback, &task_tracker_, &delayed_task_manager_);
94 CHECK(background_thread_pool_);
95
96 background_file_io_thread_pool_ = SchedulerThreadPoolImpl::Create(
97 ThreadPriority::BACKGROUND, 1U, IORestriction::ALLOWED,
98 re_enqueue_sequence_callback, &task_tracker_, &delayed_task_manager_);
99 CHECK(background_file_io_thread_pool_);
100
101 normal_thread_pool_ = SchedulerThreadPoolImpl::Create(
102 ThreadPriority::NORMAL, 4U, IORestriction::DISALLOWED,
103 re_enqueue_sequence_callback, &task_tracker_, &delayed_task_manager_);
104 CHECK(normal_thread_pool_);
105
106 normal_file_io_thread_pool_ = SchedulerThreadPoolImpl::Create(
107 ThreadPriority::NORMAL, 12U, IORestriction::ALLOWED,
108 re_enqueue_sequence_callback, &task_tracker_, &delayed_task_manager_);
109 CHECK(normal_file_io_thread_pool_);
110 }
111
112 SchedulerThreadPool* TaskSchedulerImpl::GetThreadPoolForTraits(
113 const TaskTraits& traits) {
114 if (traits.with_file_io()) {
115 if (traits.priority() == TaskPriority::BACKGROUND)
116 return background_file_io_thread_pool_.get();
117 return normal_file_io_thread_pool_.get();
118 }
119
120 if (traits.priority() == TaskPriority::BACKGROUND)
121 return background_thread_pool_.get();
122 return normal_thread_pool_.get();
123 }
124
125 void TaskSchedulerImpl::ReEnqueueSequenceCallback(
126 scoped_refptr<Sequence> sequence) {
127 DCHECK(sequence);
128
129 const SequenceSortKey sort_key = sequence->GetSortKey();
130 TaskTraits traits(sequence->PeekTask()->traits);
131
132 // Update the priority of |traits| so that the next task in |sequence| runs
133 // with the highest priority in |sequence| as opposed to the next task's
134 // specific priority.
135 traits.WithPriority(sort_key.priority());
136
137 GetThreadPoolForTraits(traits)->ReEnqueueSequence(std::move(sequence),
138 sort_key);
139 }
140
141 } // namespace internal
142 } // namespace base
OLDNEW
« no previous file with comments | « base/task_scheduler/task_scheduler_impl.h ('k') | base/task_scheduler/task_scheduler_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698