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