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/scheduler_worker_stack.h" | 5 #include "base/task_scheduler/scheduler_worker_stack.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 | 10 |
11 namespace base { | 11 namespace base { |
12 namespace internal { | 12 namespace internal { |
13 | 13 |
14 SchedulerWorkerStack::SchedulerWorkerStack() = default; | 14 SchedulerWorkerStack::SchedulerWorkerStack() = default; |
15 | 15 |
16 SchedulerWorkerStack::~SchedulerWorkerStack() = default; | 16 SchedulerWorkerStack::~SchedulerWorkerStack() = default; |
17 | 17 |
18 void SchedulerWorkerStack::Push(SchedulerWorker* worker) { | 18 void SchedulerWorkerStack::Push(SchedulerWorker* worker) { |
19 DCHECK(std::find(stack_.begin(), stack_.end(), worker) == stack_.end()) | 19 DCHECK(!Contains(worker)) << "SchedulerWorker already on stack"; |
20 << "SchedulerWorker already on stack"; | |
21 stack_.push_back(worker); | 20 stack_.push_back(worker); |
22 } | 21 } |
23 | 22 |
24 SchedulerWorker* SchedulerWorkerStack::Pop() { | 23 SchedulerWorker* SchedulerWorkerStack::Pop() { |
25 if (IsEmpty()) | 24 if (IsEmpty()) |
26 return nullptr; | 25 return nullptr; |
27 SchedulerWorker* const worker = stack_.back(); | 26 SchedulerWorker* const worker = stack_.back(); |
28 stack_.pop_back(); | 27 stack_.pop_back(); |
29 return worker; | 28 return worker; |
30 } | 29 } |
31 | 30 |
| 31 SchedulerWorker* SchedulerWorkerStack::Peek() const { |
| 32 if (IsEmpty()) |
| 33 return nullptr; |
| 34 return stack_.back(); |
| 35 } |
| 36 |
| 37 bool SchedulerWorkerStack::Contains(const SchedulerWorker* worker) const { |
| 38 return std::find(stack_.begin(), stack_.end(), worker) != stack_.end(); |
| 39 } |
| 40 |
32 void SchedulerWorkerStack::Remove(const SchedulerWorker* worker) { | 41 void SchedulerWorkerStack::Remove(const SchedulerWorker* worker) { |
33 auto it = std::find(stack_.begin(), stack_.end(), worker); | 42 auto it = std::find(stack_.begin(), stack_.end(), worker); |
34 if (it != stack_.end()) | 43 if (it != stack_.end()) |
35 stack_.erase(it); | 44 stack_.erase(it); |
36 } | 45 } |
37 | 46 |
38 } // namespace internal | 47 } // namespace internal |
39 } // namespace base | 48 } // namespace base |
OLD | NEW |