xref: /aosp_15_r20/external/libchrome/base/task_scheduler/scheduler_worker_stack.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
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/scheduler_worker_stack.h"
6 
7 #include <algorithm>
8 
9 #include "base/logging.h"
10 #include "base/stl_util.h"
11 #include "base/task_scheduler/scheduler_worker.h"
12 
13 namespace base {
14 namespace internal {
15 
16 SchedulerWorkerStack::SchedulerWorkerStack() = default;
17 
18 SchedulerWorkerStack::~SchedulerWorkerStack() = default;
19 
Push(SchedulerWorker * worker)20 void SchedulerWorkerStack::Push(SchedulerWorker* worker) {
21   DCHECK(!Contains(worker)) << "SchedulerWorker already on stack";
22   if (!IsEmpty())
23     stack_.back()->BeginUnusedPeriod();
24   stack_.push_back(worker);
25 }
26 
Pop()27 SchedulerWorker* SchedulerWorkerStack::Pop() {
28   if (IsEmpty())
29     return nullptr;
30   SchedulerWorker* const worker = stack_.back();
31   stack_.pop_back();
32   if (!IsEmpty())
33     stack_.back()->EndUnusedPeriod();
34   return worker;
35 }
36 
Peek() const37 SchedulerWorker* SchedulerWorkerStack::Peek() const {
38   if (IsEmpty())
39     return nullptr;
40   return stack_.back();
41 }
42 
Contains(const SchedulerWorker * worker) const43 bool SchedulerWorkerStack::Contains(const SchedulerWorker* worker) const {
44   return ContainsValue(stack_, worker);
45 }
46 
Remove(const SchedulerWorker * worker)47 void SchedulerWorkerStack::Remove(const SchedulerWorker* worker) {
48   DCHECK(!IsEmpty());
49   DCHECK_NE(worker, stack_.back());
50   auto it = std::find(stack_.begin(), stack_.end(), worker);
51   DCHECK(it != stack_.end());
52   DCHECK_NE(TimeTicks(), (*it)->GetLastUsedTime());
53   stack_.erase(it);
54 }
55 
56 }  // namespace internal
57 }  // namespace base
58