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..c4f09b5080c099201b6340f51e5fbbbb3aec0941 |
--- /dev/null |
+++ b/base/task_scheduler/sequence.cc |
@@ -0,0 +1,64 @@ |
+// 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() : num_tasks_per_priority_() {} |
gab
2016/02/18 03:00:45
Does |num_task_per_priority_| really need to be ex
fdoray
2016/02/18 14:56:12
http://stackoverflow.com/questions/15212261/defaul
|
+ |
+void Sequence::PushTask(scoped_ptr<Task> task, size_t* prev_num_tasks) { |
+ AutoSchedulerLock auto_lock(lock_); |
+ |
+ ++num_tasks_per_priority_[static_cast<TaskPriorityUnderlyingType>( |
+ task->traits.priority())]; |
+ *prev_num_tasks = queue_.size(); |
+ queue_.push(std::move(task)); |
+} |
+ |
+const Task* Sequence::PeekTask() { |
+ AutoSchedulerLock auto_lock(lock_); |
+ |
+ if (queue_.empty()) |
+ return nullptr; |
+ |
+ return queue_.front().get(); |
+} |
+ |
+void Sequence::PopTask(size_t* new_num_tasks) { |
+ DCHECK(new_num_tasks); |
+ AutoSchedulerLock auto_lock(lock_); |
fdoray
2016/02/18 01:46:11
Put AutoSchedulerLock first, to be consistent with
gab
2016/02/18 03:00:45
I actually like having it second but would put an
fdoray
2016/02/18 14:56:12
Done (new_num_tasks is gone)
|
+ DCHECK(!queue_.empty()); |
+ |
+ --num_tasks_per_priority_[static_cast<TaskPriorityUnderlyingType>( |
+ queue_.front()->traits.priority())]; |
+ queue_.pop(); |
+ *new_num_tasks = queue_.size(); |
+} |
+ |
+SequenceSortKey Sequence::GetSortKey() { |
+ AutoSchedulerLock auto_lock(lock_); |
+ DCHECK(!queue_.empty()); |
+ |
+ // Find the highest task priority in the sequence. |
+ TaskPriority priority = TaskPriority::BACKGROUND; |
fdoray
2016/02/18 01:46:11
This loop relies on the fact that the priority val
gab
2016/02/18 03:00:45
Agreed, also line 57 in header ( size_t num_tasks_
|
+ for (TaskPriorityUnderlyingType i = kNumTaskPriorities - 1; i >= 0; --i) { |
fdoray
2016/02/18 01:46:11
static_assert(std::is_signed<TaskPriorityUnderlyin
gab
2016/02/18 03:00:45
Hmmm, why? I'm actually proposing using uint8 abov
fdoray
2016/02/18 14:56:12
|i| needs to be signed. Otherwise, the i >=0 condi
|
+ if (num_tasks_per_priority_[i] > 0) { |
+ priority = static_cast<TaskPriority>(i); |
+ break; |
+ } |
+ } |
+ |
+ return SequenceSortKey(priority, queue_.front()->post_time); |
+} |
+ |
+Sequence::~Sequence() = default; |
+ |
+} // namespace internal |
+} // namespace base |