OLD | NEW |
---|---|
(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 TaskSchedulerImpl::TaskSchedulerImpl() | |
20 // TODO(robliao): Wake up the service thread instead of calling DoNothing() | |
21 // when the delayed run time changes. | |
22 : delayed_task_manager_(Bind(&DoNothing)) | |
23 #if DCHECK_IS_ON() | |
24 , | |
25 join_for_testing_returned_(true, false) | |
26 #endif | |
27 { | |
28 Initialize(); | |
29 } | |
30 | |
31 TaskSchedulerImpl::~TaskSchedulerImpl() { | |
32 // Use DLOG_ASSERT instead of DCHECK to avoid referencing | |
33 // |join_for_testing_returned_| in non-DCHECK builds. | |
34 static_assert(DCHECK_IS_ON() == ::logging::DEBUG_MODE, | |
35 "DCHECK_IS_ON() and ::logging::DEBUG_MODE, which defines the " | |
36 "behavior of DLOG_ASSERT, have diverged."); | |
gab
2016/04/28 22:44:59
Why does that matter? The code below doesn't depen
fdoray
2016/04/29 13:19:49
The definition of |join_for_testing_returned_| in
| |
37 DLOG_ASSERT(join_for_testing_returned_.IsSignaled()); | |
38 } | |
39 | |
40 void TaskSchedulerImpl::PostTaskWithTraits( | |
41 const tracked_objects::Location& from_here, | |
42 const TaskTraits& traits, | |
43 const Closure& task) { | |
44 // Post |task| as part of a one-off single-task Sequence. | |
45 GetThreadPoolForTraits(traits)->PostTaskWithSequence( | |
46 WrapUnique(new Task(from_here, task, traits, TimeDelta())), | |
47 make_scoped_refptr(new Sequence), nullptr); | |
48 } | |
49 | |
50 scoped_refptr<TaskRunner> TaskSchedulerImpl::CreateTaskRunnerWithTraits( | |
51 const TaskTraits& traits, | |
52 ExecutionMode execution_mode) { | |
53 return GetThreadPoolForTraits(traits)->CreateTaskRunnerWithTraits( | |
54 traits, execution_mode); | |
55 } | |
56 | |
57 void TaskSchedulerImpl::Shutdown() { | |
58 // TODO(fdoray): Increase the priority of BACKGROUND tasks blocking shutdown. | |
59 task_tracker_.Shutdown(); | |
60 } | |
61 | |
62 void TaskSchedulerImpl::JoinForTesting() { | |
63 DLOG_ASSERT(!join_for_testing_returned_.IsSignaled()); | |
64 background_thread_pool_->JoinForTesting(); | |
65 background_file_io_thread_pool_->JoinForTesting(); | |
66 normal_thread_pool_->JoinForTesting(); | |
67 normal_file_io_thread_pool_->JoinForTesting(); | |
68 #if DCHECK_IS_ON() | |
69 join_for_testing_returned_.Signal(); | |
70 #endif | |
71 } | |
72 | |
73 void TaskSchedulerImpl::Initialize() { | |
74 using IORestriction = SchedulerThreadPoolImpl::IORestriction; | |
75 | |
76 const SchedulerThreadPoolImpl::ReEnqueueSequenceCallback | |
77 re_enqueue_sequence_callback = | |
78 Bind(&TaskSchedulerImpl::ReEnqueueSequenceCallback, Unretained(this)); | |
79 | |
80 // TODO(fdoray): Derive the number of threads per pool from hardware | |
81 // characteristics rather than using hard-coded constants. | |
82 | |
83 // Passing pointers to objects owned by |this| to | |
84 // SchedulerThreadPoolImpl::Create() is safe because a TaskSchedulerImpl can't | |
85 // be deleted before all its thread pools have been joined. | |
86 background_thread_pool_ = SchedulerThreadPoolImpl::Create( | |
87 ThreadPriority::BACKGROUND, 1U, IORestriction::DISALLOWED, | |
88 re_enqueue_sequence_callback, &task_tracker_, &delayed_task_manager_); | |
89 CHECK(background_thread_pool_); | |
90 | |
91 background_file_io_thread_pool_ = SchedulerThreadPoolImpl::Create( | |
92 ThreadPriority::BACKGROUND, 1U, IORestriction::ALLOWED, | |
93 re_enqueue_sequence_callback, &task_tracker_, &delayed_task_manager_); | |
94 CHECK(background_file_io_thread_pool_); | |
95 | |
96 normal_thread_pool_ = SchedulerThreadPoolImpl::Create( | |
97 ThreadPriority::NORMAL, 4U, IORestriction::DISALLOWED, | |
98 re_enqueue_sequence_callback, &task_tracker_, &delayed_task_manager_); | |
99 CHECK(normal_thread_pool_); | |
100 | |
101 normal_file_io_thread_pool_ = SchedulerThreadPoolImpl::Create( | |
102 ThreadPriority::NORMAL, 12U, IORestriction::ALLOWED, | |
103 re_enqueue_sequence_callback, &task_tracker_, &delayed_task_manager_); | |
104 CHECK(normal_file_io_thread_pool_); | |
105 } | |
106 | |
107 SchedulerThreadPool* TaskSchedulerImpl::GetThreadPoolForTraits( | |
108 const TaskTraits& traits) { | |
109 if (traits.with_file_io()) { | |
110 if (traits.priority() == TaskPriority::BACKGROUND) | |
111 return background_file_io_thread_pool_.get(); | |
112 return normal_file_io_thread_pool_.get(); | |
113 } | |
114 | |
115 if (traits.priority() == TaskPriority::BACKGROUND) | |
116 return background_thread_pool_.get(); | |
117 return normal_thread_pool_.get(); | |
118 } | |
119 | |
120 void TaskSchedulerImpl::ReEnqueueSequenceCallback( | |
121 scoped_refptr<Sequence> sequence) { | |
122 DCHECK(sequence); | |
123 | |
124 const SequenceSortKey sort_key = sequence->GetSortKey(); | |
125 TaskTraits traits(sequence->PeekTask()->traits); | |
126 | |
127 // Update the priority of |traits| so that the next task in |sequence| runs | |
128 // with the highest priority in |sequence| as opposed to the next task's | |
129 // specific priority. | |
130 traits.WithPriority(sort_key.priority); | |
131 | |
132 GetThreadPoolForTraits(traits)->ReEnqueueSequence(std::move(sequence), | |
133 sort_key); | |
134 } | |
135 | |
136 } // namespace internal | |
137 } // namespace base | |
OLD | NEW |