TaskScheduler: Initialize TaskTraits with the priority of the current thread.
BUG=553459
TBR=danakj@chromium.org
Review-Url: https://codereview.chromium.org/2541353003
Cr-Commit-Position: refs/heads/master@{#435965}
diff --git a/base/BUILD.gn b/base/BUILD.gn
index 8b44cdc..ca954695 100644
--- a/base/BUILD.gn
+++ b/base/BUILD.gn
@@ -1980,6 +1980,7 @@
"task_scheduler/task_scheduler_impl_unittest.cc",
"task_scheduler/task_tracker_posix_unittest.cc",
"task_scheduler/task_tracker_unittest.cc",
+ "task_scheduler/task_traits_unittest.cc",
"task_scheduler/task_unittest.cc",
"task_scheduler/test_task_factory.cc",
"task_scheduler/test_task_factory.h",
diff --git a/base/task_scheduler/task_traits.cc b/base/task_scheduler/task_traits.cc
index 437189c0..9ebe821 100644
--- a/base/task_scheduler/task_traits.cc
+++ b/base/task_scheduler/task_traits.cc
@@ -9,6 +9,7 @@
#include <ostream>
#include "base/logging.h"
+#include "base/task_scheduler/scoped_set_task_priority_for_current_thread.h"
namespace base {
@@ -18,7 +19,7 @@
TaskTraits::TaskTraits()
: with_file_io_(false),
with_wait_(false),
- priority_(TaskPriority::BACKGROUND),
+ priority_(internal::GetTaskPriorityForCurrentThread()),
shutdown_behavior_(TaskShutdownBehavior::SKIP_ON_SHUTDOWN) {}
TaskTraits::~TaskTraits() = default;
diff --git a/base/task_scheduler/task_traits.h b/base/task_scheduler/task_traits.h
index 12ff6d3..0fcde2dc 100644
--- a/base/task_scheduler/task_traits.h
+++ b/base/task_scheduler/task_traits.h
@@ -80,9 +80,10 @@
public:
// Constructs a default TaskTraits for tasks with
// (1) no I/O,
- // (2) low priority, and
+ // (2) priority inherited from the calling context, and
// (3) may block shutdown or be skipped on shutdown.
- // Tasks that require stricter guarantees should highlight those by requesting
+ // Tasks that require stricter guarantees and/or know the specific
+ // TaskPriority appropriate for them should highlight those by requesting
// explicit traits below.
TaskTraits();
TaskTraits(const TaskTraits& other) = default;
diff --git a/base/task_scheduler/task_traits_unittest.cc b/base/task_scheduler/task_traits_unittest.cc
new file mode 100644
index 0000000..fed3f99
--- /dev/null
+++ b/base/task_scheduler/task_traits_unittest.cc
@@ -0,0 +1,32 @@
+// 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_traits.h"
+
+#include "base/task_scheduler/scoped_set_task_priority_for_current_thread.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace base {
+
+// Verify that TaskTraits is initialized with the priority of the task running
+// on the current thread.
+TEST(TaskSchedulerTaskTraitsTest, DefaultPriority) {
+ {
+ internal::ScopedSetTaskPriorityForCurrentThread scope(
+ TaskPriority::BACKGROUND);
+ EXPECT_EQ(TaskPriority::BACKGROUND, TaskTraits().priority());
+ }
+ {
+ internal::ScopedSetTaskPriorityForCurrentThread scope(
+ TaskPriority::USER_VISIBLE);
+ EXPECT_EQ(TaskPriority::USER_VISIBLE, TaskTraits().priority());
+ }
+ {
+ internal::ScopedSetTaskPriorityForCurrentThread scope(
+ TaskPriority::USER_BLOCKING);
+ EXPECT_EQ(TaskPriority::USER_BLOCKING, TaskTraits().priority());
+ }
+}
+
+} // namespace base