mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Add a multithreaded test for ConcurrentReadableArray.
This commit is contained in:
@@ -13,6 +13,8 @@
|
||||
#include "swift/Runtime/Concurrent.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "ThreadingHelpers.h"
|
||||
|
||||
using namespace swift;
|
||||
|
||||
TEST(ConcurrentReadableArrayTest, SingleThreaded) {
|
||||
@@ -42,3 +44,48 @@ TEST(ConcurrentReadableArrayTest, SingleThreaded) {
|
||||
add(1000000);
|
||||
check();
|
||||
}
|
||||
|
||||
TEST(ConcurrentReadableArrayTest, MultiThreaded) {
|
||||
const int writerCount = 16;
|
||||
const int readerCount = 8;
|
||||
const int insertCount = 100000;
|
||||
|
||||
struct Value {
|
||||
int threadNumber;
|
||||
int x;
|
||||
};
|
||||
ConcurrentReadableArray<Value> array;
|
||||
|
||||
auto writer = [&](int threadNumber) {
|
||||
for (int i = 0; i < insertCount; i++)
|
||||
array.push_back({ threadNumber, i });
|
||||
};
|
||||
|
||||
auto reader = [&] {
|
||||
int maxByThread[writerCount];
|
||||
bool done = false;
|
||||
while (!done) {
|
||||
for (int i = 0; i < writerCount; i++)
|
||||
maxByThread[i] = -1;
|
||||
for (auto element : array.snapshot()) {
|
||||
ASSERT_LT(element.threadNumber, writerCount);
|
||||
ASSERT_GT(element.x, maxByThread[element.threadNumber]);
|
||||
maxByThread[element.threadNumber] = element.x;
|
||||
}
|
||||
done = true;
|
||||
for (int i = 0; i < writerCount; i++) {
|
||||
if (maxByThread[i] < insertCount - 1)
|
||||
done = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
threadedExecute(writerCount + readerCount, [&](int i) {
|
||||
if (i < writerCount)
|
||||
writer(i);
|
||||
else
|
||||
reader();
|
||||
});
|
||||
|
||||
ASSERT_EQ(array.snapshot().count(), writerCount * insertCount);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user