Skip to content

Commit

Permalink
Implemented numThread limiting
Browse files Browse the repository at this point in the history
Currently there is a delay when we try to kill the threads, I'm
not entirely sure why, but it makes it seem like the process takes
a long time to die. Something to investigate in the future, but
threading is fully implemented.
  • Loading branch information
Sir-NoChill committed Sep 8, 2024
1 parent 615a369 commit ab79324
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
3 changes: 3 additions & 0 deletions include/testharness/TestHarness.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class TestHarness {
ResultManager results;

private:
// thread control
void spawnThreads();

// test running
void threadRunTestsForToolChain(std::string tcId, std::string exeName);

Expand Down
45 changes: 35 additions & 10 deletions src/testharness/TestHarness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,53 @@ void swap(TestResult& first, TestResult& second) {

// Builds TestSet during object creation.
bool TestHarness::runTests() {
std::vector<std::thread> threadPool;
// initialize the threads
std::thread t(&TestHarness::spawnThreads, this);

// Initialize the threads
bool failed = false;
// Iterate over executables.
for (auto exePair : cfg.getExecutables()) {
// Iterate over toolchains.
for (auto& tcPair : cfg.getToolChains()) {
std::thread t(&TestHarness::threadRunTestsForToolChain, this, tcPair.first, exePair.first);
threadPool.push_back(std::move(t));
if (aggregateTestResultsForToolChain(tcPair.first, exePair.first) == 1)
failed = true;
}
}

bool failed = false;
// join the control thread
t.join();
return failed;
}

void TestHarness::spawnThreads() {
int16_t numThreads = 0;
std::vector<std::thread> threadPool;

// Initialize the threads
// Iterate over executables.
for (auto exePair : cfg.getExecutables()) {
// Iterate over toolchains.
for (auto& tcPair : cfg.getToolChains()) {
if (aggregateTestResultsForToolChain(tcPair.first, exePair.first) == 1)
failed = true;
// If we have already spawned the maximum number of threads,
// wait for the first one to finish before spawning another.
if (numThreads >= cfg.getNumThreads()) {
threadPool.back().join();
threadPool.pop_back();
numThreads--;
}

threadPool.back().join();
threadPool.pop_back();
// spawn a new thread executing its tests
std::thread t(&TestHarness::threadRunTestsForToolChain, this, tcPair.first, exePair.first);
threadPool.push_back(std::move(t));
numThreads++;
}
}
return failed;

// Join any stragglers
assert(threadPool.size() <= cfg.getNumThreads());
for (size_t i = 0; i < threadPool.size(); i++) {
threadPool[i].join();
}
}

std::string TestHarness::getTestInfo() const {
Expand Down Expand Up @@ -158,6 +180,9 @@ bool TestHarness::aggregateTestResultsForToolChain(std::string tcName, std::stri
}
std::cout << "\n";

std::cout << Colors::GREEN << "Completed Tests" << Colors::RESET << std::endl;
std::cout << "Hold on while we clean up any remaining threads, this might take a moment" << std::endl;

return failed;
}

Expand Down

0 comments on commit ab79324

Please sign in to comment.