OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/task_scheduler/sequence.h" | 5 #include "base/task_scheduler/sequence.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 namespace internal { | 13 namespace internal { |
14 | 14 |
15 Sequence::Sequence() = default; | 15 Sequence::Sequence() = default; |
16 | 16 |
17 bool Sequence::PushTask(std::unique_ptr<Task> task) { | 17 bool Sequence::PushTask(std::unique_ptr<Task> task) { |
18 DCHECK(task->sequenced_time.is_null()); | 18 DCHECK(task->sequenced_time.is_null()); |
19 task->sequenced_time = base::TimeTicks::Now(); | 19 task->sequenced_time = base::TimeTicks::Now(); |
20 | 20 |
21 AutoSchedulerLock auto_lock(lock_); | 21 AutoSchedulerLock auto_lock(lock_); |
22 ++num_tasks_per_priority_[static_cast<int>(task->traits.priority())]; | 22 ++num_tasks_per_priority_[static_cast<int>(task->traits.priority())]; |
23 queue_.push(std::move(task)); | 23 queue_.push(std::move(task)); |
24 | 24 |
25 // Return true if the sequence was empty before the push. | 25 // Return true if the sequence was empty before the push. |
26 return queue_.size() == 1; | 26 return queue_.size() == 1; |
27 } | 27 } |
28 | 28 |
29 const Task* Sequence::PeekTask() const { | 29 std::unique_ptr<Task> Sequence::TakeTask() { |
30 AutoSchedulerLock auto_lock(lock_); | 30 AutoSchedulerLock auto_lock(lock_); |
| 31 DCHECK(!queue_.empty()); |
| 32 DCHECK(queue_.front()); |
31 | 33 |
32 if (queue_.empty()) | 34 const int priority_index = |
33 return nullptr; | 35 static_cast<int>(queue_.front()->traits.priority()); |
| 36 DCHECK_GT(num_tasks_per_priority_[priority_index], 0U); |
| 37 --num_tasks_per_priority_[priority_index]; |
34 | 38 |
35 return queue_.front().get(); | 39 return std::move(queue_.front()); |
36 } | 40 } |
37 | 41 |
38 bool Sequence::PopTask() { | 42 TaskTraits Sequence::PeekTaskTraits() const { |
39 // Delete the popped task outside the scope of |lock_|. This prevents a double | 43 AutoSchedulerLock auto_lock(lock_); |
40 // acquisition of |lock_| if the task's destructor tries to post a task to | 44 DCHECK(!queue_.empty()); |
41 // this Sequence and reduces contention. | 45 DCHECK(queue_.front()); |
42 std::unique_ptr<Task> delete_outside_lock_scope; | 46 return queue_.front()->traits; |
43 bool sequence_empty_after_pop = false; | 47 } |
44 | 48 |
45 { | 49 bool Sequence::Pop() { |
46 AutoSchedulerLock auto_lock(lock_); | 50 AutoSchedulerLock auto_lock(lock_); |
47 DCHECK(!queue_.empty()); | 51 DCHECK(!queue_.empty()); |
48 | 52 DCHECK(!queue_.front()); |
49 const int priority_index = | 53 queue_.pop(); |
50 static_cast<int>(queue_.front()->traits.priority()); | 54 return queue_.empty(); |
51 DCHECK_GT(num_tasks_per_priority_[priority_index], 0U); | |
52 --num_tasks_per_priority_[priority_index]; | |
53 | |
54 delete_outside_lock_scope = std::move(queue_.front()); | |
55 queue_.pop(); | |
56 sequence_empty_after_pop = queue_.empty(); | |
57 } | |
58 | |
59 return sequence_empty_after_pop; | |
60 } | 55 } |
61 | 56 |
62 SequenceSortKey Sequence::GetSortKey() const { | 57 SequenceSortKey Sequence::GetSortKey() const { |
63 TaskPriority priority = TaskPriority::LOWEST; | 58 TaskPriority priority = TaskPriority::LOWEST; |
64 base::TimeTicks next_task_sequenced_time; | 59 base::TimeTicks next_task_sequenced_time; |
65 | 60 |
66 { | 61 { |
67 AutoSchedulerLock auto_lock(lock_); | 62 AutoSchedulerLock auto_lock(lock_); |
68 DCHECK(!queue_.empty()); | 63 DCHECK(!queue_.empty()); |
69 | 64 |
(...skipping 11 matching lines...) Expand all Loading... |
81 next_task_sequenced_time = queue_.front()->sequenced_time; | 76 next_task_sequenced_time = queue_.front()->sequenced_time; |
82 } | 77 } |
83 | 78 |
84 return SequenceSortKey(priority, next_task_sequenced_time); | 79 return SequenceSortKey(priority, next_task_sequenced_time); |
85 } | 80 } |
86 | 81 |
87 Sequence::~Sequence() = default; | 82 Sequence::~Sequence() = default; |
88 | 83 |
89 } // namespace internal | 84 } // namespace internal |
90 } // namespace base | 85 } // namespace base |
OLD | NEW |