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 "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "base/task_scheduler/worker_thread.h" | |
10 | |
11 namespace base { | |
12 namespace task_scheduler { | |
13 | |
14 TaskSchedulerImpl::TaskSchedulerImpl() { | |
15 const task_scheduler::WorkerThread::ReinsertSequenceCallback | |
16 reinsert_sequence_callback = | |
17 Bind(&TaskSchedulerImpl::ReinsertSequenceCallback, Unretained(this)); | |
robliao
2016/02/11 21:56:27
We'll want to comment that the TaskScheduler lives
fdoray
2016/02/12 04:16:20
Done.
| |
18 | |
19 background_thread_pool_ = ThreadPool::CreateThreadPool( | |
20 ThreadPriority::BACKGROUND, 1, reinsert_sequence_callback, | |
21 &shutdown_manager_); | |
22 CHECK(background_thread_pool_.get()); | |
23 | |
24 background_file_io_thread_pool_ = ThreadPool::CreateThreadPool( | |
25 ThreadPriority::BACKGROUND, 1, reinsert_sequence_callback, | |
26 &shutdown_manager_); | |
27 CHECK(background_file_io_thread_pool_.get()); | |
28 | |
29 normal_thread_pool_ = ThreadPool::CreateThreadPool(ThreadPriority::NORMAL, 4, | |
30 reinsert_sequence_callback, | |
31 &shutdown_manager_); | |
32 CHECK(normal_thread_pool_.get()); | |
33 | |
34 normal_file_io_thread_pool_ = ThreadPool::CreateThreadPool( | |
35 ThreadPriority::NORMAL, 12, reinsert_sequence_callback, | |
36 &shutdown_manager_); | |
37 CHECK(normal_file_io_thread_pool_.get()); | |
38 } | |
39 | |
40 TaskSchedulerImpl::~TaskSchedulerImpl() {} | |
41 | |
42 void TaskSchedulerImpl::PostTaskWithTraits( | |
43 const tracked_objects::Location& from_here, | |
44 TaskTraits traits, | |
45 const Closure& task) { | |
46 // TODO(fdoray): Support WithSequenceToken(). | |
robliao
2016/02/11 21:56:27
Remove this comment.
fdoray
2016/02/12 04:16:19
Done.
| |
47 CreateTaskRunnerWithTraits(traits, ExecutionMode::PARALLEL) | |
48 ->PostTask(from_here, task); | |
49 } | |
50 | |
51 scoped_refptr<TaskRunner> TaskSchedulerImpl::CreateTaskRunnerWithTraits( | |
52 TaskTraits traits, | |
53 ExecutionMode execution_mode) { | |
54 return GetThreadPoolForTraits(traits) | |
55 ->CreateTaskRunnerWithTraits(traits, execution_mode); | |
56 } | |
57 | |
58 void TaskSchedulerImpl::Shutdown() { | |
59 // TODO(fdoray): Increase the priority of BACKGROUND tasks blocking shutdown. | |
60 shutdown_manager_.Shutdown(); | |
61 } | |
62 | |
63 void TaskSchedulerImpl::JoinAllThreadsForTesting() { | |
64 background_thread_pool_->JoinAllThreadsForTesting(); | |
65 background_file_io_thread_pool_->JoinAllThreadsForTesting(); | |
66 normal_thread_pool_->JoinAllThreadsForTesting(); | |
67 normal_file_io_thread_pool_->JoinAllThreadsForTesting(); | |
68 } | |
69 | |
70 task_scheduler::ThreadPool* TaskSchedulerImpl::GetThreadPoolForTraits( | |
71 const TaskTraits& traits) { | |
72 if (traits.with_file_io()) { | |
73 if (traits.priority() == TaskPriority::BACKGROUND) | |
74 return background_file_io_thread_pool_.get(); | |
75 return normal_file_io_thread_pool_.get(); | |
76 } | |
77 | |
78 if (traits.priority() == TaskPriority::BACKGROUND) | |
79 return background_thread_pool_.get(); | |
80 return normal_thread_pool_.get(); | |
81 } | |
82 | |
83 void TaskSchedulerImpl::ReinsertSequenceCallback( | |
84 scoped_refptr<task_scheduler::Sequence> sequence, | |
85 const task_scheduler::WorkerThread* worker_thread) { | |
86 const task_scheduler::SequenceSortKey sort_key = sequence->GetSortKey(); | |
87 const task_scheduler::Task* next_task_in_sequence = sequence->PeekTask(); | |
88 DCHECK(next_task_in_sequence); | |
89 | |
90 TaskTraits traits = TaskTraits().WithPriority(sort_key.priority()); | |
91 if (next_task_in_sequence->traits.with_file_io()) | |
92 traits.WithFileIO(); | |
robliao
2016/02/11 21:56:27
traits = traits.WithFileIO() to avoid the return v
fdoray
2016/02/12 04:16:20
Done.
| |
93 | |
94 GetThreadPoolForTraits(traits) | |
95 ->ReinsertSequence(sequence, sort_key, worker_thread); | |
96 } | |
97 | |
98 } // namespace task_scheduler | |
99 } // namespace base | |
OLD | NEW |