Bug 1731564: Use motivated SpinEventLoopUntil inside xpcom/*. r=xpcom-reviewers,nika

Depends on D127236

Differential Revision: https://phabricator.services.mozilla.com/D127237
This commit is contained in:
Jens Stutte 2021-10-07 08:06:16 +00:00
Родитель a10ac08a4d
Коммит 7700e81876
13 изменённых файлов: 86 добавлений и 43 удалений

Просмотреть файл

@ -227,7 +227,7 @@ void AppShutdown::MaybeFastShutdown(ShutdownPhase aPhase) {
nsCOMPtr<nsICertStorage> certStorage =
do_GetService("@mozilla.org/security/certstorage;1", &rv);
if (NS_SUCCEEDED(rv)) {
SpinEventLoopUntil([&]() {
SpinEventLoopUntil("AppShutdown::MaybeFastShutdown"_ns, [&]() {
int32_t remainingOps;
nsresult rv = certStorage->GetRemainingOperationCount(&remainingOps);
NS_ASSERTION(NS_SUCCEEDED(rv),

Просмотреть файл

@ -49,7 +49,7 @@ bool WaitUntil(const ConditionT& aCondition, uint32_t aTimeoutMs) {
},
&isTimeout, aTimeoutMs, nsITimer::TYPE_ONE_SHOT, __func__);
SpinEventLoopUntil([&]() -> bool {
SpinEventLoopUntil("xpcom-tests:WaitUntil"_ns, [&]() -> bool {
if (isTimeout) {
return true;
}

Просмотреть файл

@ -64,7 +64,8 @@ TEST(EventPriorities, IdleAfterNormal)
NS_DispatchToMainThread(evNormal);
MOZ_ALWAYS_TRUE(
SpinEventLoopUntil([&]() { return normalRan == 3 && idleRan == 3; }));
SpinEventLoopUntil("xpcom:TEST(EventPriorities, IdleAfterNormal)"_ns,
[&]() { return normalRan == 3 && idleRan == 3; }));
}
TEST(EventPriorities, HighNormal)
@ -85,5 +86,6 @@ TEST(EventPriorities, HighNormal)
NS_DispatchToMainThread(evHigh);
MOZ_ALWAYS_TRUE(
SpinEventLoopUntil([&]() { return normalRan == 3 && highRan == 3; }));
SpinEventLoopUntil("xpcom:TEST(EventPriorities, HighNormal)"_ns,
[&]() { return normalRan == 3 && highRan == 3; }));
}

Просмотреть файл

@ -27,7 +27,9 @@ TEST(TestInputStreamLengthHelper, NonLengthStream)
called = true;
});
MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return called; }));
MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
"xpcom:TEST(TestInputStreamLengthHelper, NonLengthStream)"_ns,
[&]() { return called; }));
}
class LengthStream final : public nsIInputStreamLength,
@ -103,7 +105,9 @@ TEST(TestInputStreamLengthHelper, LengthStream)
called = true;
});
MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return called; }));
MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
"xpcom:TEST(TestInputStreamLengthHelper, LengthStream)"_ns,
[&]() { return called; }));
}
TEST(TestInputStreamLengthHelper, InvalidLengthStream)
@ -117,7 +121,9 @@ TEST(TestInputStreamLengthHelper, InvalidLengthStream)
called = true;
});
MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return called; }));
MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
"xpcom:TEST(TestInputStreamLengthHelper, InvalidLengthStream)"_ns,
[&]() { return called; }));
}
TEST(TestInputStreamLengthHelper, AsyncLengthStream)
@ -131,7 +137,9 @@ TEST(TestInputStreamLengthHelper, AsyncLengthStream)
called = true;
});
MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return called; }));
MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
"xpcom:TEST(TestInputStreamLengthHelper, AsyncLengthStream)"_ns,
[&]() { return called; }));
}
TEST(TestInputStreamLengthHelper, FallbackLengthStream)
@ -145,5 +153,7 @@ TEST(TestInputStreamLengthHelper, FallbackLengthStream)
called = true;
});
MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return called; }));
MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
"xpcom:TEST(TestInputStreamLengthHelper, FallbackLengthStream)"_ns,
[&]() { return called; }));
}

Просмотреть файл

@ -131,22 +131,26 @@ TEST(MemoryPressure, Singlethread)
{
RefPtr observer(new MemoryPressureObserver);
NS_NotifyOfEventualMemoryPressure(MemoryPressureState::LowMemory);
SpinEventLoopUntil([&observer]() { return observer->GetCount() == 1; });
SpinEventLoopUntil("xpcom:TEST(MemoryPressure, Singlethread) 1"_ns,
[&observer]() { return observer->GetCount() == 1; });
EXPECT_EQ(observer->Top(), MemoryPressureEventType::LowMemory);
observer->Reset();
NS_NotifyOfEventualMemoryPressure(MemoryPressureState::LowMemory);
SpinEventLoopUntil([&observer]() { return observer->GetCount() == 1; });
SpinEventLoopUntil("xpcom:TEST(MemoryPressure, Singlethread) 2"_ns,
[&observer]() { return observer->GetCount() == 1; });
EXPECT_EQ(observer->Top(), MemoryPressureEventType::LowMemoryOngoing);
observer->Reset();
NS_NotifyOfEventualMemoryPressure(MemoryPressureState::LowMemory);
SpinEventLoopUntil([&observer]() { return observer->GetCount() == 1; });
SpinEventLoopUntil("xpcom:TEST(MemoryPressure, Singlethread) 3"_ns,
[&observer]() { return observer->GetCount() == 1; });
EXPECT_EQ(observer->Top(), MemoryPressureEventType::LowMemoryOngoing);
observer->Reset();
NS_NotifyOfEventualMemoryPressure(MemoryPressureState::NoPressure);
SpinEventLoopUntil([&observer]() { return observer->GetCount() == 1; });
SpinEventLoopUntil("xpcom:TEST(MemoryPressure, Singlethread) 4"_ns,
[&observer]() { return observer->GetCount() == 1; });
EXPECT_EQ(observer->Top(), MemoryPressureEventType::Stop);
}
@ -183,6 +187,7 @@ TEST(MemoryPressure, Multithread)
// We cannot sleep here because the main thread needs to keep running.
SpinEventLoopUntil(
"xpcom:TEST(MemoryPressure, Multithread)"_ns,
[&observer]() { return observer->GetCount() >= kNumEventsToValidate; });
shouldContinue = false;

Просмотреть файл

@ -352,7 +352,9 @@ TEST(MultiplexInputStream, AsyncWait_withEventTarget)
ASSERT_FALSE(cb->Called());
// Eventually it is called.
MOZ_ALWAYS_TRUE(mozilla::SpinEventLoopUntil([&]() { return cb->Called(); }));
MOZ_ALWAYS_TRUE(mozilla::SpinEventLoopUntil(
"xpcom:TEST(MultiplexInputStream, AsyncWait_withEventTarget)"_ns,
[&]() { return cb->Called(); }));
ASSERT_TRUE(cb->Called());
}
@ -393,7 +395,9 @@ TEST(MultiplexInputStream, AsyncWait_withEventTarget_closureOnly)
ASSERT_FALSE(cb->Called());
// Eventually it is called.
MOZ_ALWAYS_TRUE(mozilla::SpinEventLoopUntil([&]() { return cb->Called(); }));
MOZ_ALWAYS_TRUE(mozilla::SpinEventLoopUntil(
"xpcom:TEST(MultiplexInputStream, AsyncWait_withEventTarget_closureOnly)"_ns,
[&]() { return cb->Called(); }));
ASSERT_TRUE(cb->Called());
}
@ -776,7 +780,9 @@ TEST(MultiplexInputStream, LengthInputStream)
rv = afsis->AsyncLengthWait(callback, GetCurrentSerialEventTarget());
ASSERT_EQ(NS_OK, rv);
MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return callback->Called(); }));
MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
"xpcom:TEST(MultiplexInputStream, LengthInputStream) 1"_ns,
[&]() { return callback->Called(); }));
ASSERT_EQ(buf.Length() * 3, callback->Size());
// Now a negative stream
@ -808,7 +814,9 @@ TEST(MultiplexInputStream, LengthInputStream)
rv = afsis->AsyncLengthWait(callback2, GetCurrentSerialEventTarget());
ASSERT_EQ(NS_OK, rv);
MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return callback2->Called(); }));
MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
"xpcom:TEST(MultiplexInputStream, LengthInputStream) 2"_ns,
[&]() { return callback2->Called(); }));
ASSERT_FALSE(callback1->Called());
ASSERT_TRUE(callback2->Called());
}

Просмотреть файл

@ -135,7 +135,9 @@ TEST(TestNonBlockingAsyncInputStream, AsyncWait_Simple)
ASSERT_EQ(NS_OK, async->AsyncWait(cb, 0, 0, thread));
ASSERT_FALSE(cb->Called());
MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return cb->Called(); }));
MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
"xpcom:TEST(TestNonBlockingAsyncInputStream, AsyncWait_Simple)"_ns,
[&]() { return cb->Called(); }));
ASSERT_TRUE(cb->Called());
// Read works fine.
@ -202,7 +204,9 @@ TEST(TestNonBlockingAsyncInputStream, AsyncWait_ClosureOnly_withEventTarget)
ASSERT_EQ(NS_OK, async->Close());
ASSERT_FALSE(cb->Called());
MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return cb->Called(); }));
MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
"xpcom:TEST(TestNonBlockingAsyncInputStream, AsyncWait_ClosureOnly_withEventTarget)"_ns,
[&]() { return cb->Called(); }));
ASSERT_TRUE(cb->Called());
}

Просмотреть файл

@ -597,7 +597,9 @@ TEST(TestSlicedInputStream, AsyncInputStreamLength)
nsresult rv = qi->AsyncLengthWait(callback, GetCurrentSerialEventTarget());
ASSERT_EQ(NS_OK, rv);
MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return callback->Called(); }));
MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
"xpcom:TEST(TestSlicedInputStream, AsyncInputStreamLength)"_ns,
[&]() { return callback->Called(); }));
ASSERT_EQ(5, callback->Size());
}
@ -622,7 +624,9 @@ TEST(TestSlicedInputStream, NegativeAsyncInputStreamLength)
nsresult rv = qi->AsyncLengthWait(callback, GetCurrentSerialEventTarget());
ASSERT_EQ(NS_OK, rv);
MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return callback->Called(); }));
MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
"xpcom:TEST(TestSlicedInputStream, NegativeAsyncInputStreamLength)"_ns,
[&]() { return callback->Called(); }));
ASSERT_EQ(-1, callback->Size());
}
@ -650,7 +654,9 @@ TEST(TestSlicedInputStream, AbortLengthCallback)
rv = qi->AsyncLengthWait(callback2, GetCurrentSerialEventTarget());
ASSERT_EQ(NS_OK, rv);
MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return callback2->Called(); }));
MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
"xpcom:TEST(TestSlicedInputStream, AbortLengthCallback)"_ns,
[&]() { return callback2->Called(); }));
ASSERT_TRUE(!callback1->Called());
ASSERT_EQ(-1, callback2->Size());
}

Просмотреть файл

@ -47,7 +47,7 @@ class SpinRunnable final : public Runnable {
do_GetService("@mozilla.org/thread-manager;1");
mResult = threadMan->SpinEventLoopUntil(
"gtest TestThreadManager.cpp: SpinRunnable->Run()"_ns, mCondition);
"xpcom:TestThreadManager.cpp:SpinRunnable->Run()"_ns, mCondition);
return NS_OK;
}

Просмотреть файл

@ -836,29 +836,35 @@ TEST(ThreadUtils, IdleTaskRunner)
TimeDuration::FromMilliseconds(3), false, nullptr);
// Firstly we wait until the two repeating tasks reach their limits.
MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return cnt1 >= 100; }));
MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return cnt2 >= 100; }));
MOZ_ALWAYS_TRUE(
SpinEventLoopUntil("xpcom:TEST(ThreadUtils, IdleTaskRunner) cnt1"_ns,
[&]() { return cnt1 >= 100; }));
MOZ_ALWAYS_TRUE(
SpinEventLoopUntil("xpcom:TEST(ThreadUtils, IdleTaskRunner) cnt2"_ns,
[&]() { return cnt2 >= 100; }));
// At any point ==> 0 <= cnt3 <= 2 since MayStopProcessing() would return
// true when cnt3 >= 2.
MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() {
if (cnt3 > 2) {
EXPECT_TRUE(false) << "MaybeContinueProcess() doesn't work.";
return true; // Stop on failure.
}
return cnt3 == 2; // Stop finish if we have reached its max value.
}));
MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
"xpcom:TEST(ThreadUtils, IdleTaskRunner) cnt3"_ns, [&]() {
if (cnt3 > 2) {
EXPECT_TRUE(false) << "MaybeContinueProcess() doesn't work.";
return true; // Stop on failure.
}
return cnt3 == 2; // Stop finish if we have reached its max value.
}));
// At any point ==> 0 <= cnt4 <= 1 since this is a non-repeating
// idle runner.
MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() {
// At any point: 0 <= cnt4 <= 1
if (cnt4 > 1) {
EXPECT_TRUE(false) << "The 'mRepeating' flag doesn't work.";
return true; // Stop on failure.
}
return cnt4 == 1;
}));
MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
"xpcom:TEST(ThreadUtils, IdleTaskRunner) cnt4"_ns, [&]() {
// At any point: 0 <= cnt4 <= 1
if (cnt4 > 1) {
EXPECT_TRUE(false) << "The 'mRepeating' flag doesn't work.";
return true; // Stop on failure.
}
return cnt4 == 1;
}));
// The repeating timers require an explicit Cancel() call.
runner1->Cancel();

Просмотреть файл

@ -85,7 +85,7 @@ bool SharedThreadPool::IsEmpty() {
/* static */
void SharedThreadPool::SpinUntilEmpty() {
MOZ_ASSERT(NS_IsMainThread());
SpinEventLoopUntil([]() -> bool {
SpinEventLoopUntil("SharedThreadPool::SpinUntilEmpty"_ns, []() -> bool {
sMonitor->AssertNotCurrentThreadIn();
return IsEmpty();
});

Просмотреть файл

@ -86,6 +86,7 @@ ThreadEventTarget::Dispatch(already_AddRefed<nsIRunnable> aEvent,
// Allows waiting; ensure no locks are held that would deadlock us!
SpinEventLoopUntil(
"ThreadEventTarget::Dispatch"_ns,
[&, wrapper]() -> bool { return !wrapper->IsPending(); });
return NS_OK;

Просмотреть файл

@ -348,8 +348,9 @@ nsThreadPool::Dispatch(already_AddRefed<nsIRunnable> aEvent, uint32_t aFlags) {
new nsThreadSyncDispatch(thread.forget(), std::move(aEvent));
PutEvent(wrapper);
SpinEventLoopUntil(
[&, wrapper]() -> bool { return !wrapper->IsPending(); });
SpinEventLoopUntil("nsThreadPool::Dispatch"_ns, [&, wrapper]() -> bool {
return !wrapper->IsPending();
});
} else {
NS_ASSERTION(aFlags == NS_DISPATCH_NORMAL || aFlags == NS_DISPATCH_AT_END,
"unexpected dispatch flags");