Skip to content

Commit 3fcdacf

Browse files
authored
Merge pull request #25049 from gottesmm/swift-5.1-branch-rdar51093916
[test] Make Mutex.cpp unittest more reliable.
2 parents 57f31fd + 7d940f2 commit 3fcdacf

File tree

1 file changed

+48
-33
lines changed

1 file changed

+48
-33
lines changed

unittests/runtime/Mutex.cpp

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -555,20 +555,23 @@ TEST(StaticReadWriteLockTest, ScopedWriteUnlockThreaded) {
555555
template <typename RW> void readLockWhileReadLockedThreaded(RW &lock) {
556556
lock.readLock();
557557

558-
std::vector<bool> results;
559-
results.assign(10, false);
558+
const int threadCount = 10;
559+
560+
std::atomic<bool> results[threadCount] = {};
560561

561562
std::atomic<bool> done(false);
562-
threadedExecute(10,
563+
threadedExecute(threadCount,
563564
[&](int index) {
564-
while (!done) {
565+
// Always perform at least one iteration of this loop to
566+
// avoid spurious failures if this thread is slow to run.
567+
do {
565568
lock.withReadLock([&] {
566569
results[index] = true;
567570
std::this_thread::sleep_for(
568571
std::chrono::milliseconds(5));
569572
});
570573
std::this_thread::sleep_for(std::chrono::milliseconds(1));
571-
}
574+
} while (!done);
572575
},
573576
[&] {
574577
std::this_thread::sleep_for(std::chrono::milliseconds(100));
@@ -577,7 +580,7 @@ template <typename RW> void readLockWhileReadLockedThreaded(RW &lock) {
577580

578581
lock.readUnlock();
579582

580-
for (auto result : results) {
583+
for (auto &result : results) {
581584
ASSERT_TRUE(result);
582585
}
583586
}
@@ -595,28 +598,31 @@ TEST(StaticReadWriteLockTest, ReadLockWhileReadLockedThreaded) {
595598
template <typename RW> void readLockWhileWriteLockedThreaded(RW &lock) {
596599
lock.writeLock();
597600

598-
std::vector<int> results;
599-
results.assign(10, 0);
601+
const int threadCount = 10;
602+
603+
std::atomic<int> results[threadCount] = {};
600604

601605
std::atomic<bool> done(false);
602-
threadedExecute(10,
606+
threadedExecute(threadCount,
603607
[&](int index) {
604-
while (!done) {
608+
// Always perform at least one iteration of this loop to
609+
// avoid spurious failures if this thread is slow to run.
610+
do {
605611
lock.withReadLock([&] {
606612
results[index] += 1;
607613
std::this_thread::sleep_for(
608614
std::chrono::milliseconds(5));
609615
});
610616
std::this_thread::sleep_for(std::chrono::milliseconds(1));
611-
}
617+
} while (!done);
612618
},
613619
[&] {
614620
std::this_thread::sleep_for(std::chrono::milliseconds(100));
615621
done = true;
616622
lock.writeUnlock();
617623
});
618624

619-
for (auto result : results) {
625+
for (auto &result : results) {
620626
ASSERT_EQ(result, 1);
621627
}
622628
}
@@ -636,28 +642,29 @@ template <typename RW> void writeLockWhileReadLockedThreaded(RW &lock) {
636642

637643
const int threadCount = 10;
638644

639-
std::vector<int> results;
640-
results.assign(threadCount, 0);
645+
std::atomic<int> results[threadCount] = {};
641646

642647
std::atomic<bool> done(false);
643648
threadedExecute(threadCount,
644649
[&](int index) {
645-
while (!done) {
650+
// Always perform at least one iteration of this loop to
651+
// avoid spurious failures if this thread is slow to run.
652+
do {
646653
lock.withWriteLock([&] {
647654
results[index] += 1;
648655
std::this_thread::sleep_for(
649656
std::chrono::milliseconds(5));
650657
});
651658
std::this_thread::sleep_for(std::chrono::milliseconds(1));
652-
}
659+
} while (!done);
653660
},
654661
[&] {
655662
std::this_thread::sleep_for(std::chrono::milliseconds(100));
656663
done = true;
657664
lock.readUnlock();
658665
});
659666

660-
for (auto result : results) {
667+
for (auto &result : results) {
661668
ASSERT_EQ(result, 1);
662669
}
663670
}
@@ -677,28 +684,29 @@ template <typename RW> void writeLockWhileWriteLockedThreaded(RW &lock) {
677684

678685
const int threadCount = 10;
679686

680-
std::vector<int> results;
681-
results.assign(threadCount, 0);
687+
std::atomic<int> results[threadCount] = {};
682688

683689
std::atomic<bool> done(false);
684690
threadedExecute(threadCount,
685691
[&](int index) {
686-
while (!done) {
692+
// Always perform at least one iteration of this loop to
693+
// avoid spurious failures if this thread is slow to run.
694+
do {
687695
lock.withWriteLock([&] {
688696
results[index] += 1;
689697
std::this_thread::sleep_for(
690698
std::chrono::milliseconds(5));
691699
});
692700
std::this_thread::sleep_for(std::chrono::milliseconds(1));
693-
}
701+
} while (!done);
694702
},
695703
[&] {
696704
std::this_thread::sleep_for(std::chrono::milliseconds(100));
697705
done = true;
698706
lock.writeUnlock();
699707
});
700708

701-
for (auto result : results) {
709+
for (auto &result : results) {
702710
ASSERT_EQ(result, 1);
703711
}
704712
}
@@ -719,10 +727,12 @@ template <typename RW> void tryReadLockWhileWriteLockedThreaded(RW &lock) {
719727
std::atomic<bool> done(false);
720728
threadedExecute(10,
721729
[&](int) {
722-
while (!done) {
730+
// Always perform at least one iteration of this loop to
731+
// avoid spurious failures if this thread is slow to run.
732+
do {
723733
ASSERT_FALSE(lock.try_readLock());
724734
std::this_thread::sleep_for(std::chrono::milliseconds(1));
725-
}
735+
} while (!done);
726736
},
727737
[&] {
728738
std::this_thread::sleep_for(std::chrono::milliseconds(100));
@@ -747,19 +757,20 @@ template <typename RW> void tryReadLockWhileReadLockedThreaded(RW &lock) {
747757

748758
const int threadCount = 10;
749759

750-
std::vector<bool> results;
751-
results.assign(threadCount, false);
760+
std::atomic<bool> results[threadCount] = {};
752761

753762
std::atomic<bool> done(false);
754763
threadedExecute(threadCount,
755764
[&](int index) {
756-
while (!done) {
765+
// Always perform at least one iteration of this loop to
766+
// avoid spurious failures if this thread is slow to run.
767+
do {
757768
ASSERT_TRUE(lock.try_readLock());
758769
results[index] = true;
759770
std::this_thread::sleep_for(std::chrono::milliseconds(5));
760771
lock.readUnlock();
761772
std::this_thread::sleep_for(std::chrono::milliseconds(1));
762-
}
773+
} while (!done);
763774
},
764775
[&] {
765776
std::this_thread::sleep_for(std::chrono::milliseconds(100));
@@ -768,7 +779,7 @@ template <typename RW> void tryReadLockWhileReadLockedThreaded(RW &lock) {
768779

769780
lock.readUnlock();
770781

771-
for (auto result : results) {
782+
for (auto &result : results) {
772783
ASSERT_TRUE(result);
773784
}
774785
}
@@ -789,10 +800,12 @@ template <typename RW> void tryWriteLockWhileWriteLockedThreaded(RW &lock) {
789800
std::atomic<bool> done(false);
790801
threadedExecute(10,
791802
[&](int) {
792-
while (!done) {
803+
// Always perform at least one iteration of this loop to
804+
// avoid spurious failures if this thread is slow to run.
805+
do {
793806
ASSERT_FALSE(lock.try_writeLock());
794807
std::this_thread::sleep_for(std::chrono::milliseconds(1));
795-
}
808+
} while (!done);
796809
},
797810
[&] {
798811
std::this_thread::sleep_for(std::chrono::milliseconds(100));
@@ -818,10 +831,12 @@ template <typename RW> void tryWriteLockWhileReadLockedThreaded(RW &lock) {
818831
std::atomic<bool> done(false);
819832
threadedExecute(10,
820833
[&](int) {
821-
while (!done) {
834+
// Always perform at least one iteration of this loop to
835+
// avoid spurious failures if this thread is slow to run.
836+
do {
822837
ASSERT_FALSE(lock.try_writeLock());
823838
std::this_thread::sleep_for(std::chrono::milliseconds(1));
824-
}
839+
} while (!done);
825840
},
826841
[&] {
827842
std::this_thread::sleep_for(std::chrono::milliseconds(100));

0 commit comments

Comments
 (0)