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

Side by Side 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: CR from Dana #36-37 Created 4 years, 9 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/sequence.h"
6
7 #include <utility>
8
9 #include "base/logging.h"
10 #include "base/time/time.h"
11
12 namespace base {
13 namespace internal {
14
15 Sequence::Sequence() = default;
16
17 bool Sequence::PushTask(scoped_ptr<Task> task) {
18 DCHECK(task->sequenced_time.is_null());
19 task->sequenced_time = base::TimeTicks::Now();
20
21 AutoSchedulerLock auto_lock(lock_);
22 ++num_tasks_per_priority_[static_cast<int>(task->traits.priority())];
23 queue_.push(std::move(task));
24
25 // Return true if the sequence was empty before the push.
26 return queue_.size() == 1;
27 }
28
29 const Task* Sequence::PeekTask() const {
30 AutoSchedulerLock auto_lock(lock_);
31
32 if (queue_.empty())
33 return nullptr;
34
35 return queue_.front().get();
36 }
37
38 bool Sequence::PopTask() {
39 AutoSchedulerLock auto_lock(lock_);
40 DCHECK(!queue_.empty());
41
42 const int priority_index =
43 static_cast<int>(queue_.front()->traits.priority());
44 DCHECK_GT(num_tasks_per_priority_[priority_index], 0U);
45 --num_tasks_per_priority_[priority_index];
46
47 queue_.pop();
48 return queue_.empty();
49 }
50
51 SequenceSortKey Sequence::GetSortKey() const {
52 scoped_ptr<AutoSchedulerLock> auto_lock(new AutoSchedulerLock(lock_));
danakj 2016/03/16 19:40:33 Rather than using a scoped_ptr which requires mall
fdoray 2016/03/17 00:42:33 Done.
53 DCHECK(!queue_.empty());
54
55 // Find the highest task priority in the sequence.
56 const int highest_priority_index = static_cast<int>(TaskPriority::HIGHEST);
57 const int lowest_priority_index = static_cast<int>(TaskPriority::LOWEST);
58 TaskPriority priority = TaskPriority::LOWEST;
59 for (int i = highest_priority_index; i > lowest_priority_index; --i) {
60 if (num_tasks_per_priority_[i] > 0) {
61 priority = static_cast<TaskPriority>(i);
62 break;
63 }
64 }
65
66 // Save the sequenced time of the next task in the sequence.
67 const base::TimeTicks next_task_sequenced_time =
68 queue_.front()->sequenced_time;
69
70 auto_lock.reset();
71 return SequenceSortKey(priority, next_task_sequenced_time);
72 }
73
74 Sequence::~Sequence() = default;
75
76 } // namespace internal
77 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698