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

Unified Diff: base/task_scheduler/sequence.cc

Issue 1705253002: TaskScheduler [3/9] Task and Sequence (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s_1_scheduler_lock
Patch Set: self review 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/sequence.cc
diff --git a/base/task_scheduler/sequence.cc b/base/task_scheduler/sequence.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3cf44badee1812e4a3e0ef74ff806460c9a5b9c3
--- /dev/null
+++ b/base/task_scheduler/sequence.cc
@@ -0,0 +1,67 @@
+// 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/sequence.h"
+
+#include <utility>
+
+#include "base/logging.h"
+
+namespace base {
+namespace internal {
+
+Sequence::Sequence() = default;
+
+bool Sequence::PushTask(scoped_ptr<Task> task) {
+ AutoSchedulerLock auto_lock(lock_);
+
+ ++num_tasks_per_priority_[static_cast<TaskPriorityUnderlyingType>(
robliao 2016/02/19 02:33:43 Maybe a temporary for the static_cast would make t
fdoray 2016/02/19 14:12:14 Done.
+ task->traits.priority())];
+ queue_.push(std::move(task));
+
+ // Return true if the sequence was empty before the push.
+ return queue_.size() == 1;
+}
+
+const Task* Sequence::PeekTask() const {
+ AutoSchedulerLock auto_lock(lock_);
+
+ if (queue_.empty())
+ return nullptr;
+
+ return queue_.front().get();
+}
+
+bool Sequence::PopTask() {
+ AutoSchedulerLock auto_lock(lock_);
+ DCHECK(!queue_.empty());
robliao 2016/02/19 02:33:43 I'm thinking this should be a CHECK. It is undefin
fdoray 2016/02/19 14:12:14 Done. https://www.chromium.org/developers/coding-
gab 2016/02/19 16:50:47 I don't think this should be a CHECK. It's enforc
fdoray 2016/02/19 22:28:29 Done. I do prefer DCHECK, but I agreed to change i
gab 2016/02/22 17:12:21 Agreed that for identifiable user input errors a n
fdoray 2016/02/22 18:07:54 Ack.
+
+ --num_tasks_per_priority_[static_cast<TaskPriorityUnderlyingType>(
+ queue_.front()->traits.priority())];
+ queue_.pop();
+ return queue_.empty();
+}
+
+SequenceSortKey Sequence::GetSortKey() const {
+ AutoSchedulerLock auto_lock(lock_);
+ DCHECK(!queue_.empty());
fdoray 2016/02/19 14:12:14 CHECK here too, because front() has an undefined b
+
+ // Find the highest task priority in the sequence.
+ TaskPriority priority = TaskPriority::LOWEST;
+ for (size_t i =
+ static_cast<TaskPriorityUnderlyingType>(TaskPriority::HIGHEST);
+ i > static_cast<TaskPriorityUnderlyingType>(TaskPriority::LOWEST); --i) {
+ if (num_tasks_per_priority_[i] > 0) {
+ priority = static_cast<TaskPriority>(i);
+ break;
+ }
+ }
+
+ return SequenceSortKey(priority, queue_.front()->sequenced_time);
+}
+
+Sequence::~Sequence() = default;
+
+} // namespace internal
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698