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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include <stdint.h> // uint32_t
#include "nsString.h" // nsACString
#include "nsThreadUtils.h" // NS_ProcessNextEvent
#include "mozilla/Atomics.h" // Atomic
#include "mozilla/EventQueue.h" // EventQueuePriority
#include "mozilla/Mutex.h" // Mutex, MutexAutoLock
#include "mozilla/RefPtr.h" // RefPtr, do_AddRef
#include "mozilla/TaskController.h" // TaskController, Task
#include "prthread.h" // PR_Sleep
using namespace mozilla;
namespace TestTaskController {
class Logger {
public:
Logger() : mMutex("Logger") {}
void Add(const char* aText) {
MutexAutoLock lock(mMutex);
mLog += aText;
}
const nsAutoCString& GetLog() const { return mLog; }
private:
nsAutoCString mLog;
Mutex mMutex;
};
class ReschedulingTask : public Task {
static constexpr uint32_t LoopCount = 3;
public:
explicit ReschedulingTask(Kind aKind, Logger* aLogger, const char* aName)
: Task(aKind, EventQueuePriority::Normal),
mCount(0),
mIsDone(false),
mLogger(aLogger),
mName(aName) {}
TaskResult Run() override {
mLogger->Add(mName);
mCount++;
if (mCount < LoopCount) {
return TaskResult::Incomplete;
}
mIsDone = true;
return TaskResult::Complete;
}
#ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
bool GetName(nsACString& aName) override {
aName.AssignLiteral("AsyncScriptCompileTask");
return true;
}
#endif
bool IsDone() const { return mIsDone; }
private:
Atomic<uint32_t> mCount;
Atomic<bool> mIsDone;
Logger* mLogger;
const char* mName;
};
using namespace mozilla;
TEST(TaskController, RescheduleOnMainThread)
{
Logger logger;
RefPtr<ReschedulingTask> mainThreadTask =
new ReschedulingTask(Task::Kind::MainThreadOnly, &logger, "1");
TaskController::Get()->AddTask(do_AddRef(mainThreadTask));
while (NS_ProcessNextEvent(nullptr, false)) {
}
ASSERT_TRUE(mainThreadTask->IsDone());
ASSERT_TRUE(logger.GetLog() == "111");
}
TEST(TaskController, RescheduleOffMainThread)
{
Logger logger;
RefPtr<ReschedulingTask> offThreadTask =
new ReschedulingTask(Task::Kind::OffMainThreadOnly, &logger, "1");
TaskController::Get()->AddTask(do_AddRef(offThreadTask));
uint32_t count = 0;
while (!offThreadTask->IsDone() && count < 100) {
PR_Sleep(PR_MillisecondsToInterval(100));
count++;
}
ASSERT_TRUE(offThreadTask->IsDone());
ASSERT_TRUE(logger.GetLog() == "111");
}
TEST(TaskController, RescheduleMainAndOffMainThreads)
{
Logger logger;
RefPtr<ReschedulingTask> offThreadTask =
new ReschedulingTask(Task::Kind::OffMainThreadOnly, &logger, "1");
RefPtr<ReschedulingTask> mainThreadTask =
new ReschedulingTask(Task::Kind::MainThreadOnly, &logger, "2");
mainThreadTask->AddDependency(offThreadTask.get());
TaskController::Get()->AddTask(do_AddRef(offThreadTask));
TaskController::Get()->AddTask(do_AddRef(mainThreadTask));
uint32_t count = 0;
while (!offThreadTask->IsDone() && count < 100) {
PR_Sleep(PR_MillisecondsToInterval(100));
count++;
}
ASSERT_TRUE(offThreadTask->IsDone());
// At this point, the main thread task shouldn't have run.
ASSERT_TRUE(logger.GetLog() == "111");
while (NS_ProcessNextEvent(nullptr, false)) {
}
ASSERT_TRUE(mainThreadTask->IsDone());
ASSERT_TRUE(logger.GetLog() == "111222");
}
TEST(TaskController, RescheduleOrder)
{
Logger logger;
RefPtr<ReschedulingTask> mainThreadTask1 =
new ReschedulingTask(Task::Kind::MainThreadOnly, &logger, "1");
RefPtr<ReschedulingTask> mainThreadTask2 =
new ReschedulingTask(Task::Kind::MainThreadOnly, &logger, "2");
RefPtr<ReschedulingTask> mainThreadTask3 =
new ReschedulingTask(Task::Kind::MainThreadOnly, &logger, "3");
TaskController::Get()->AddTask(do_AddRef(mainThreadTask1));
TaskController::Get()->AddTask(do_AddRef(mainThreadTask2));
TaskController::Get()->AddTask(do_AddRef(mainThreadTask3));
while (NS_ProcessNextEvent(nullptr, false)) {
}
ASSERT_TRUE(mainThreadTask1->IsDone());
ASSERT_TRUE(mainThreadTask2->IsDone());
ASSERT_TRUE(mainThreadTask3->IsDone());
// Rescheduled tasks should be added to the beginning of the queue.
ASSERT_TRUE(logger.GetLog() == "111222333");
}
TEST(TaskController, RescheduleOrderOffMainThread)
{
Logger logger1;
Logger logger2;
Logger logger3;
RefPtr<ReschedulingTask> offThreadTask1 =
new ReschedulingTask(Task::Kind::OffMainThreadOnly, &logger1, "1");
RefPtr<ReschedulingTask> offThreadTask2 =
new ReschedulingTask(Task::Kind::OffMainThreadOnly, &logger2, "2");
RefPtr<ReschedulingTask> offThreadTask3 =
new ReschedulingTask(Task::Kind::OffMainThreadOnly, &logger3, "3");
TaskController::Get()->AddTask(do_AddRef(offThreadTask1));
TaskController::Get()->AddTask(do_AddRef(offThreadTask2));
TaskController::Get()->AddTask(do_AddRef(offThreadTask3));
uint32_t count = 0;
while (!(offThreadTask1->IsDone() && offThreadTask2->IsDone() &&
offThreadTask3->IsDone()) &&
count < 100) {
PR_Sleep(PR_MillisecondsToInterval(100));
count++;
}
ASSERT_TRUE(offThreadTask1->IsDone());
ASSERT_TRUE(offThreadTask2->IsDone());
ASSERT_TRUE(offThreadTask3->IsDone());
// Rescheduled tasks should be enqueued.
// The order between off-thread tasks are not deterministic.
ASSERT_TRUE(logger1.GetLog() == "111");
ASSERT_TRUE(logger2.GetLog() == "222");
ASSERT_TRUE(logger3.GetLog() == "333");
}
} // namespace TestTaskController
|