forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_thread_stack.cc
56 lines (45 loc) · 1.44 KB
/
worker_thread_stack.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/task/thread_pool/worker_thread_stack.h"
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/ranges/algorithm.h"
#include "base/task/thread_pool/worker_thread.h"
namespace base {
namespace internal {
WorkerThreadStack::WorkerThreadStack() = default;
WorkerThreadStack::~WorkerThreadStack() = default;
void WorkerThreadStack::Push(WorkerThread* worker) {
DCHECK(!Contains(worker)) << "WorkerThread already on stack";
if (!IsEmpty())
stack_.back()->BeginUnusedPeriod();
stack_.push_back(worker);
}
WorkerThread* WorkerThreadStack::Pop() {
if (IsEmpty())
return nullptr;
WorkerThread* const worker = stack_.back();
stack_.pop_back();
if (!IsEmpty())
stack_.back()->EndUnusedPeriod();
return worker;
}
WorkerThread* WorkerThreadStack::Peek() const {
if (IsEmpty())
return nullptr;
return stack_.back();
}
bool WorkerThreadStack::Contains(const WorkerThread* worker) const {
return base::Contains(stack_, worker);
}
void WorkerThreadStack::Remove(const WorkerThread* worker) {
DCHECK(!IsEmpty());
DCHECK_NE(worker, stack_.back());
auto it = ranges::find(stack_, worker);
DCHECK(it != stack_.end());
DCHECK_NE(TimeTicks(), (*it)->GetLastUsedTime());
stack_.erase(it);
}
} // namespace internal
} // namespace base