OLD | NEW |
---|---|
(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 | |
23 const TaskPriorityUnderlyingType priority_index = | |
24 static_cast<TaskPriorityUnderlyingType>(task->traits.priority()); | |
danakj
2016/03/15 23:16:31
Can you put code that doesn't access members above
fdoray
2016/03/16 19:11:43
|priority_index| was there to make the code simple
| |
25 ++num_tasks_per_priority_[priority_index]; | |
26 | |
27 queue_.push(std::move(task)); | |
28 | |
29 // Return true if the sequence was empty before the push. | |
30 return queue_.size() == 1; | |
31 } | |
32 | |
33 const Task* Sequence::PeekTask() const { | |
34 AutoSchedulerLock auto_lock(lock_); | |
35 | |
36 if (queue_.empty()) | |
37 return nullptr; | |
38 | |
39 return queue_.front().get(); | |
40 } | |
41 | |
42 bool Sequence::PopTask() { | |
43 AutoSchedulerLock auto_lock(lock_); | |
44 DCHECK(!queue_.empty()); | |
45 | |
46 const TaskPriorityUnderlyingType priority_index = | |
47 static_cast<TaskPriorityUnderlyingType>( | |
48 queue_.front()->traits.priority()); | |
49 --num_tasks_per_priority_[priority_index]; | |
danakj
2016/03/15 23:16:31
maybe DCHECK that num_tasks_per_priority > 0 befor
fdoray
2016/03/16 19:11:43
Done.
| |
50 | |
51 queue_.pop(); | |
52 return queue_.empty(); | |
53 } | |
54 | |
55 SequenceSortKey Sequence::GetSortKey() const { | |
56 AutoSchedulerLock auto_lock(lock_); | |
57 DCHECK(!queue_.empty()); | |
58 | |
59 // Find the highest task priority in the sequence. | |
60 static_assert(static_cast<TaskPriorityUnderlyingType>( | |
61 TaskPriority::HIGHEST) == internal::kNumTaskPriorities - 1, | |
danakj
2016/03/15 23:16:32
Do you even needs kNumTaskPriorities? Why not just
fdoray
2016/03/16 19:11:43
Done. kNumTaskPriorities is not needed.
| |
62 "The value of TaskPriority::HIGHEST must be the number of " | |
63 "priorities minus 1."); | |
64 TaskPriority priority = TaskPriority::LOWEST; | |
65 for (TaskPriorityUnderlyingType i = | |
66 static_cast<TaskPriorityUnderlyingType>(TaskPriority::HIGHEST); | |
danakj
2016/03/15 23:16:31
This would probably be a lot easier to read if you
fdoray
2016/03/16 19:11:43
Done.
| |
67 i > static_cast<TaskPriorityUnderlyingType>(TaskPriority::LOWEST); --i) { | |
68 if (num_tasks_per_priority_[i] > 0) { | |
69 priority = static_cast<TaskPriority>(i); | |
70 break; | |
71 } | |
72 } | |
73 | |
74 return SequenceSortKey(priority, queue_.front()->sequenced_time); | |
danakj
2016/03/15 23:16:31
this is a bit pendantic, but you could put the seq
fdoray
2016/03/16 19:11:43
Done.
| |
75 } | |
76 | |
77 Sequence::~Sequence() = default; | |
78 | |
79 } // namespace internal | |
80 } // namespace base | |
OLD | NEW |