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

Unified Diff: base/task_scheduler/task_scheduler_impl.cc

Issue 1685423002: Task Scheduler. (Closed) Base URL: https://luckyluke-private.googlesource.com/src@a_master
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 side-by-side diff with in-line comments
Download patch
Index: base/task_scheduler/task_scheduler_impl.cc
diff --git a/base/task_scheduler/task_scheduler_impl.cc b/base/task_scheduler/task_scheduler_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b2f2d1507ad9d0374db2bf82e2521a9340011800
--- /dev/null
+++ b/base/task_scheduler/task_scheduler_impl.cc
@@ -0,0 +1,99 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/task_scheduler/task_scheduler_impl.h"
+
+#include "base/bind.h"
+#include "base/logging.h"
+#include "base/task_scheduler/worker_thread.h"
+
+namespace base {
+namespace task_scheduler {
+
+TaskSchedulerImpl::TaskSchedulerImpl() {
+ const task_scheduler::WorkerThread::ReinsertSequenceCallback
+ reinsert_sequence_callback =
+ 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.
+
+ background_thread_pool_ = ThreadPool::CreateThreadPool(
+ ThreadPriority::BACKGROUND, 1, reinsert_sequence_callback,
+ &shutdown_manager_);
+ CHECK(background_thread_pool_.get());
+
+ background_file_io_thread_pool_ = ThreadPool::CreateThreadPool(
+ ThreadPriority::BACKGROUND, 1, reinsert_sequence_callback,
+ &shutdown_manager_);
+ CHECK(background_file_io_thread_pool_.get());
+
+ normal_thread_pool_ = ThreadPool::CreateThreadPool(ThreadPriority::NORMAL, 4,
+ reinsert_sequence_callback,
+ &shutdown_manager_);
+ CHECK(normal_thread_pool_.get());
+
+ normal_file_io_thread_pool_ = ThreadPool::CreateThreadPool(
+ ThreadPriority::NORMAL, 12, reinsert_sequence_callback,
+ &shutdown_manager_);
+ CHECK(normal_file_io_thread_pool_.get());
+}
+
+TaskSchedulerImpl::~TaskSchedulerImpl() {}
+
+void TaskSchedulerImpl::PostTaskWithTraits(
+ const tracked_objects::Location& from_here,
+ TaskTraits traits,
+ const Closure& task) {
+ // TODO(fdoray): Support WithSequenceToken().
robliao 2016/02/11 21:56:27 Remove this comment.
fdoray 2016/02/12 04:16:19 Done.
+ CreateTaskRunnerWithTraits(traits, ExecutionMode::PARALLEL)
+ ->PostTask(from_here, task);
+}
+
+scoped_refptr<TaskRunner> TaskSchedulerImpl::CreateTaskRunnerWithTraits(
+ TaskTraits traits,
+ ExecutionMode execution_mode) {
+ return GetThreadPoolForTraits(traits)
+ ->CreateTaskRunnerWithTraits(traits, execution_mode);
+}
+
+void TaskSchedulerImpl::Shutdown() {
+ // TODO(fdoray): Increase the priority of BACKGROUND tasks blocking shutdown.
+ shutdown_manager_.Shutdown();
+}
+
+void TaskSchedulerImpl::JoinAllThreadsForTesting() {
+ background_thread_pool_->JoinAllThreadsForTesting();
+ background_file_io_thread_pool_->JoinAllThreadsForTesting();
+ normal_thread_pool_->JoinAllThreadsForTesting();
+ normal_file_io_thread_pool_->JoinAllThreadsForTesting();
+}
+
+task_scheduler::ThreadPool* TaskSchedulerImpl::GetThreadPoolForTraits(
+ const TaskTraits& traits) {
+ if (traits.with_file_io()) {
+ if (traits.priority() == TaskPriority::BACKGROUND)
+ return background_file_io_thread_pool_.get();
+ return normal_file_io_thread_pool_.get();
+ }
+
+ if (traits.priority() == TaskPriority::BACKGROUND)
+ return background_thread_pool_.get();
+ return normal_thread_pool_.get();
+}
+
+void TaskSchedulerImpl::ReinsertSequenceCallback(
+ scoped_refptr<task_scheduler::Sequence> sequence,
+ const task_scheduler::WorkerThread* worker_thread) {
+ const task_scheduler::SequenceSortKey sort_key = sequence->GetSortKey();
+ const task_scheduler::Task* next_task_in_sequence = sequence->PeekTask();
+ DCHECK(next_task_in_sequence);
+
+ TaskTraits traits = TaskTraits().WithPriority(sort_key.priority());
+ if (next_task_in_sequence->traits.with_file_io())
+ 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.
+
+ GetThreadPoolForTraits(traits)
+ ->ReinsertSequence(sequence, sort_key, worker_thread);
+}
+
+} // namespace task_scheduler
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698