Skip to content

[test] Make Mutex.cpp unittest more reliable. #25049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 48 additions & 33 deletions unittests/runtime/Mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,20 +555,23 @@ TEST(StaticReadWriteLockTest, ScopedWriteUnlockThreaded) {
template <typename RW> void readLockWhileReadLockedThreaded(RW &lock) {
lock.readLock();

std::vector<bool> results;
results.assign(10, false);
const int threadCount = 10;

std::atomic<bool> results[threadCount] = {};

std::atomic<bool> done(false);
threadedExecute(10,
threadedExecute(threadCount,
[&](int index) {
while (!done) {
// Always perform at least one iteration of this loop to
// avoid spurious failures if this thread is slow to run.
do {
lock.withReadLock([&] {
results[index] = true;
std::this_thread::sleep_for(
std::chrono::milliseconds(5));
});
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
} while (!done);
},
[&] {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
Expand All @@ -577,7 +580,7 @@ template <typename RW> void readLockWhileReadLockedThreaded(RW &lock) {

lock.readUnlock();

for (auto result : results) {
for (auto &result : results) {
ASSERT_TRUE(result);
}
}
Expand All @@ -595,28 +598,31 @@ TEST(StaticReadWriteLockTest, ReadLockWhileReadLockedThreaded) {
template <typename RW> void readLockWhileWriteLockedThreaded(RW &lock) {
lock.writeLock();

std::vector<int> results;
results.assign(10, 0);
const int threadCount = 10;

std::atomic<int> results[threadCount] = {};

std::atomic<bool> done(false);
threadedExecute(10,
threadedExecute(threadCount,
[&](int index) {
while (!done) {
// Always perform at least one iteration of this loop to
// avoid spurious failures if this thread is slow to run.
do {
lock.withReadLock([&] {
results[index] += 1;
std::this_thread::sleep_for(
std::chrono::milliseconds(5));
});
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
} while (!done);
},
[&] {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
done = true;
lock.writeUnlock();
});

for (auto result : results) {
for (auto &result : results) {
ASSERT_EQ(result, 1);
}
}
Expand All @@ -636,28 +642,29 @@ template <typename RW> void writeLockWhileReadLockedThreaded(RW &lock) {

const int threadCount = 10;

std::vector<int> results;
results.assign(threadCount, 0);
std::atomic<int> results[threadCount] = {};

std::atomic<bool> done(false);
threadedExecute(threadCount,
[&](int index) {
while (!done) {
// Always perform at least one iteration of this loop to
// avoid spurious failures if this thread is slow to run.
do {
lock.withWriteLock([&] {
results[index] += 1;
std::this_thread::sleep_for(
std::chrono::milliseconds(5));
});
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
} while (!done);
},
[&] {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
done = true;
lock.readUnlock();
});

for (auto result : results) {
for (auto &result : results) {
ASSERT_EQ(result, 1);
}
}
Expand All @@ -677,28 +684,29 @@ template <typename RW> void writeLockWhileWriteLockedThreaded(RW &lock) {

const int threadCount = 10;

std::vector<int> results;
results.assign(threadCount, 0);
std::atomic<int> results[threadCount] = {};

std::atomic<bool> done(false);
threadedExecute(threadCount,
[&](int index) {
while (!done) {
// Always perform at least one iteration of this loop to
// avoid spurious failures if this thread is slow to run.
do {
lock.withWriteLock([&] {
results[index] += 1;
std::this_thread::sleep_for(
std::chrono::milliseconds(5));
});
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
} while (!done);
},
[&] {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
done = true;
lock.writeUnlock();
});

for (auto result : results) {
for (auto &result : results) {
ASSERT_EQ(result, 1);
}
}
Expand All @@ -719,10 +727,12 @@ template <typename RW> void tryReadLockWhileWriteLockedThreaded(RW &lock) {
std::atomic<bool> done(false);
threadedExecute(10,
[&](int) {
while (!done) {
// Always perform at least one iteration of this loop to
// avoid spurious failures if this thread is slow to run.
do {
ASSERT_FALSE(lock.try_readLock());
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
} while (!done);
},
[&] {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
Expand All @@ -747,19 +757,20 @@ template <typename RW> void tryReadLockWhileReadLockedThreaded(RW &lock) {

const int threadCount = 10;

std::vector<bool> results;
results.assign(threadCount, false);
std::atomic<bool> results[threadCount] = {};

std::atomic<bool> done(false);
threadedExecute(threadCount,
[&](int index) {
while (!done) {
// Always perform at least one iteration of this loop to
// avoid spurious failures if this thread is slow to run.
do {
ASSERT_TRUE(lock.try_readLock());
results[index] = true;
std::this_thread::sleep_for(std::chrono::milliseconds(5));
lock.readUnlock();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
} while (!done);
},
[&] {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
Expand All @@ -768,7 +779,7 @@ template <typename RW> void tryReadLockWhileReadLockedThreaded(RW &lock) {

lock.readUnlock();

for (auto result : results) {
for (auto &result : results) {
ASSERT_TRUE(result);
}
}
Expand All @@ -789,10 +800,12 @@ template <typename RW> void tryWriteLockWhileWriteLockedThreaded(RW &lock) {
std::atomic<bool> done(false);
threadedExecute(10,
[&](int) {
while (!done) {
// Always perform at least one iteration of this loop to
// avoid spurious failures if this thread is slow to run.
do {
ASSERT_FALSE(lock.try_writeLock());
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
} while (!done);
},
[&] {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
Expand All @@ -818,10 +831,12 @@ template <typename RW> void tryWriteLockWhileReadLockedThreaded(RW &lock) {
std::atomic<bool> done(false);
threadedExecute(10,
[&](int) {
while (!done) {
// Always perform at least one iteration of this loop to
// avoid spurious failures if this thread is slow to run.
do {
ASSERT_FALSE(lock.try_writeLock());
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
} while (!done);
},
[&] {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
Expand Down