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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
base / threading / scoped_thread_priority_unittest.cc [blame]
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/threading/scoped_thread_priority.h"
#include "base/test/gtest_util.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
// Tests in this file invoke an API that tracks state in static variable. They
// can therefore only be invoked once per process.
#define ASSERT_RUNS_ONCE() \
static int num_times_run = 0; \
++num_times_run; \
if (num_times_run > 1) \
ADD_FAILURE() << "This test cannot run multiple times in the same " \
"process.";
static ThreadType kAllThreadTypes[] = {
ThreadType::kRealtimeAudio, ThreadType::kDisplayCritical,
ThreadType::kDefault, ThreadType::kBackground};
static_assert(static_cast<int>(ThreadType::kBackground) == 0,
"kBackground isn't lowest");
static_assert(ThreadType::kRealtimeAudio == ThreadType::kMaxValue,
"kRealtimeAudio isn't highest");
class ScopedThreadPriorityTest : public testing::Test {
protected:
void SetUp() override {
// Ensures the default thread priority is set.
PlatformThread::SetCurrentThreadType(ThreadType::kDefault);
ASSERT_EQ(ThreadPriorityForTest::kNormal,
PlatformThread::GetCurrentThreadPriorityForTest());
}
};
using ScopedThreadPriorityDeathTest = ScopedThreadPriorityTest;
#if BUILDFLAG(IS_WIN)
void FunctionThatBoostsPriorityOnFirstInvoke(
ThreadPriorityForTest expected_priority) {
SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY();
EXPECT_EQ(expected_priority,
PlatformThread::GetCurrentThreadPriorityForTest());
}
void FunctionThatBoostsPriorityOnEveryInvoke() {
SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY_REPEATEDLY();
EXPECT_EQ(base::ThreadPriorityForTest::kNormal,
PlatformThread::GetCurrentThreadPriorityForTest());
}
#endif // BUILDFLAG(IS_WIN)
} // namespace
TEST_F(ScopedThreadPriorityTest, BasicTest) {
for (auto from : kAllThreadTypes) {
if (!PlatformThread::CanChangeThreadType(ThreadType::kDefault, from))
continue;
for (auto to : kAllThreadTypes) {
// ThreadType::kRealtimeAudio is not a valid |target_thread_type| for
// ScopedBoostPriority.
if (to == ThreadType::kRealtimeAudio)
continue;
Thread thread("ScopedThreadPriorityTest");
thread.StartWithOptions(Thread::Options(from));
thread.WaitUntilThreadStarted();
thread.task_runner()->PostTask(
FROM_HERE,
BindOnce(
[](ThreadType from, ThreadType to) {
EXPECT_EQ(PlatformThread::GetCurrentThreadType(), from);
{
ScopedBoostPriority scoped_boost_priority(to);
bool will_boost_priority =
from < to &&
PlatformThread::CanChangeThreadType(from, to) &&
PlatformThread::CanChangeThreadType(to, from);
EXPECT_EQ(PlatformThread::GetCurrentThreadType(),
will_boost_priority ? to : from);
}
EXPECT_EQ(PlatformThread::GetCurrentThreadType(), from);
},
from, to));
}
}
}
TEST_F(ScopedThreadPriorityDeathTest, NoRealTime) {
EXPECT_DCHECK_DEATH({
ScopedBoostPriority scoped_boost_priority(ThreadType::kRealtimeAudio);
});
}
TEST_F(ScopedThreadPriorityTest, WithoutPriorityBoost) {
ASSERT_RUNS_ONCE();
// Validates that a thread at normal priority keep the same priority.
{
SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY();
EXPECT_EQ(ThreadPriorityForTest::kNormal,
PlatformThread::GetCurrentThreadPriorityForTest());
}
EXPECT_EQ(ThreadPriorityForTest::kNormal,
PlatformThread::GetCurrentThreadPriorityForTest());
}
#if BUILDFLAG(IS_WIN)
TEST_F(ScopedThreadPriorityTest, WithPriorityBoost) {
ASSERT_RUNS_ONCE();
// Validates that a thread at background priority is boosted to normal
// priority.
PlatformThread::SetCurrentThreadType(ThreadType::kBackground);
{
SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY();
EXPECT_EQ(ThreadPriorityForTest::kNormal,
PlatformThread::GetCurrentThreadPriorityForTest());
}
EXPECT_EQ(ThreadPriorityForTest::kBackground,
PlatformThread::GetCurrentThreadPriorityForTest());
// Put back the default thread priority.
PlatformThread::SetCurrentThreadType(ThreadType::kDefault);
}
#endif // BUILDFLAG(IS_WIN)
#if BUILDFLAG(IS_WIN)
TEST_F(ScopedThreadPriorityTest, NestedScope) {
ASSERT_RUNS_ONCE();
PlatformThread::SetCurrentThreadType(ThreadType::kBackground);
{
SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY();
EXPECT_EQ(ThreadPriorityForTest::kNormal,
PlatformThread::GetCurrentThreadPriorityForTest());
{
SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY();
EXPECT_EQ(ThreadPriorityForTest::kNormal,
PlatformThread::GetCurrentThreadPriorityForTest());
}
EXPECT_EQ(ThreadPriorityForTest::kNormal,
PlatformThread::GetCurrentThreadPriorityForTest());
}
EXPECT_EQ(ThreadPriorityForTest::kBackground,
PlatformThread::GetCurrentThreadPriorityForTest());
// Put back the default thread priority.
PlatformThread::SetCurrentThreadType(ThreadType::kDefault);
}
#endif // BUILDFLAG(IS_WIN)
#if BUILDFLAG(IS_WIN)
TEST_F(ScopedThreadPriorityTest, FunctionThatBoostsPriorityOnFirstInvoke) {
ASSERT_RUNS_ONCE();
PlatformThread::SetCurrentThreadType(ThreadType::kBackground);
FunctionThatBoostsPriorityOnFirstInvoke(base::ThreadPriorityForTest::kNormal);
FunctionThatBoostsPriorityOnFirstInvoke(
base::ThreadPriorityForTest::kBackground);
// Put back the default thread priority.
PlatformThread::SetCurrentThreadType(ThreadType::kDefault);
}
TEST_F(ScopedThreadPriorityTest, FunctionThatBoostsPriorityOnEveryInvoke) {
PlatformThread::SetCurrentThreadType(ThreadType::kBackground);
FunctionThatBoostsPriorityOnEveryInvoke();
FunctionThatBoostsPriorityOnEveryInvoke();
// Put back the default thread priority.
PlatformThread::SetCurrentThreadType(ThreadType::kDefault);
}
#endif // BUILDFLAG(IS_WIN)
} // namespace base