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

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

Issue 1698183005: Reference CL for the new task scheduler. (Closed) Base URL: https://luckyluke-private.googlesource.com/src@bigmaster2
Patch Set: Created 4 years, 10 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 "base/bind.h"
8 #include "base/logging.h"
9 #include "base/task_scheduler/worker_thread.h"
10
11 namespace base {
12 namespace internal {
13
14 TaskSchedulerImpl::TaskSchedulerImpl() {
15 // Using Unretained() is safe because a TaskSchedulerImpl is never destroyed
16 // before all its ThreadPools have been destroyed.
17 const WorkerThread::ReinsertSequenceCallback reinsert_sequence_callback =
18 Bind(&TaskSchedulerImpl::ReinsertSequenceCallback, Unretained(this));
19
20 background_thread_pool_ = ThreadPool::CreateThreadPool(
21 ThreadPriority::BACKGROUND, 1, reinsert_sequence_callback,
22 &shutdown_manager_);
23 CHECK(background_thread_pool_.get());
24
25 background_file_io_thread_pool_ = ThreadPool::CreateThreadPool(
26 ThreadPriority::BACKGROUND, 1, reinsert_sequence_callback,
27 &shutdown_manager_);
28 CHECK(background_file_io_thread_pool_.get());
29
30 normal_thread_pool_ = ThreadPool::CreateThreadPool(ThreadPriority::NORMAL, 4,
31 reinsert_sequence_callback,
32 &shutdown_manager_);
33 CHECK(normal_thread_pool_.get());
34
35 normal_file_io_thread_pool_ = ThreadPool::CreateThreadPool(
36 ThreadPriority::NORMAL, 12, reinsert_sequence_callback,
37 &shutdown_manager_);
38 CHECK(normal_file_io_thread_pool_.get());
39 }
40
41 TaskSchedulerImpl::~TaskSchedulerImpl() {
42 // In production code, a TaskSchedulerImpl is never destroyed. In test code,
43 // ShutdownAndJoinAllThreadsForTesting() must be called before the
44 // TaskSchedulerImpl is destroyed.
45 DCHECK(!background_thread_pool_.get());
46 DCHECK(!background_file_io_thread_pool_.get());
47 DCHECK(!normal_thread_pool_.get());
48 DCHECK(!normal_file_io_thread_pool_.get());
49 }
50
51 void TaskSchedulerImpl::PostTaskWithTraits(
52 const tracked_objects::Location& from_here,
53 TaskTraits traits,
54 const Closure& task) {
55 CreateTaskRunnerWithTraits(traits, ExecutionMode::PARALLEL)
56 ->PostTask(from_here, task);
57 }
58
59 scoped_refptr<TaskRunner> TaskSchedulerImpl::CreateTaskRunnerWithTraits(
60 TaskTraits traits,
61 ExecutionMode execution_mode) {
62 return GetThreadPoolForTraits(traits)
63 ->CreateTaskRunnerWithTraits(traits, execution_mode);
64 }
65
66 void TaskSchedulerImpl::Shutdown() {
67 // TODO(fdoray): Increase the priority of BACKGROUND tasks blocking shutdown.
68 shutdown_manager_.Shutdown();
69 }
70
71 void TaskSchedulerImpl::ShutdownAndJoinAllThreadsForTesting() {
72 background_thread_pool_->ShutdownAndJoinAllThreadsForTesting();
73 background_thread_pool_.reset();
74 background_file_io_thread_pool_->ShutdownAndJoinAllThreadsForTesting();
75 background_file_io_thread_pool_.reset();
76 normal_thread_pool_->ShutdownAndJoinAllThreadsForTesting();
77 normal_thread_pool_.reset();
78 normal_file_io_thread_pool_->ShutdownAndJoinAllThreadsForTesting();
79 normal_file_io_thread_pool_.reset();
80 }
81
82 ThreadPool* TaskSchedulerImpl::GetThreadPoolForTraits(
83 const TaskTraits& traits) {
84 if (traits.with_file_io()) {
85 if (traits.priority() == TaskPriority::BACKGROUND)
86 return background_file_io_thread_pool_.get();
87 return normal_file_io_thread_pool_.get();
88 }
89
90 if (traits.priority() == TaskPriority::BACKGROUND)
91 return background_thread_pool_.get();
92 return normal_thread_pool_.get();
93 }
94
95 void TaskSchedulerImpl::ReinsertSequenceCallback(
96 scoped_refptr<Sequence> sequence,
97 const WorkerThread* worker_thread) {
98 const SequenceSortKey sort_key = sequence->GetSortKey();
99 const Task* next_task_in_sequence = sequence->PeekTask();
100 DCHECK(next_task_in_sequence);
101
102 TaskTraits traits = TaskTraits().WithPriority(sort_key.priority());
103 if (next_task_in_sequence->traits.with_file_io())
104 traits = traits.WithFileIO();
105
106 GetThreadPoolForTraits(traits)
107 ->ReinsertSequence(sequence, sort_key, worker_thread);
108 }
109
110 } // namespace internal
111 } // 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