Skip to content

Commit

Permalink
Merge pull request #5 from SLM-Audio/syl/pre-pr
Browse files Browse the repository at this point in the history
Syl/pre pr
  • Loading branch information
MeijisIrlnd authored Oct 1, 2024
2 parents 04c3f95 + 2855019 commit 8bc43ea
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
11 changes: 10 additions & 1 deletion source/utils/mostlyharmless_TaskThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@
#include <cassert>
#include <thread>
namespace mostly_harmless::utils {
TaskThread::~TaskThread() noexcept {
while (m_threadAboutToStart || isThreadRunning()) {
signalThreadShouldExit();
wake();
}
}

void TaskThread::perform() {
if (m_isThreadRunning) return;
m_threadAboutToStart = true;
if (!action) {
assert(false);
return;
}
auto actionWrapper = [this]() -> void {
m_threadAboutToStart.store(false);
m_isThreadRunning.store(true);
action();
m_isThreadRunning.store(false);
Expand All @@ -28,8 +37,8 @@ namespace mostly_harmless::utils {
}

void TaskThread::wake() {
std::lock_guard<std::mutex> guard{ m_mutex };
m_canWakeUp = true;
std::lock_guard<std::mutex> guard{ m_mutex };
m_conditionVariable.notify_one();
}

Expand Down
29 changes: 24 additions & 5 deletions tests/utils/mostlyharmless_TaskThreadTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <mostly_harmless/utils/mostlyharmless_TaskThread.h>
#include <mutex>
#include <thread>
#include <iostream>
namespace mostly_harmless::testing {
TEST_CASE("Test TaskThread") {
std::mutex mutex;
Expand All @@ -18,8 +19,7 @@ namespace mostly_harmless::testing {
};
taskThread.action = std::move(task);
taskThread.perform();
// give it a bit of time to spin up.. (remember that this is a syscall)
std::this_thread::sleep_for(std::chrono::milliseconds(5));
std::this_thread::sleep_for(std::chrono::milliseconds(1));
REQUIRE(taskThread.isThreadRunning());
// Sleep for a ms so the task has a chance to acquire the mutex..
std::this_thread::sleep_for(std::chrono::milliseconds(1));
Expand All @@ -30,11 +30,12 @@ namespace mostly_harmless::testing {

SECTION("Kill") {
auto task = [&taskThread]() -> void {
while(!taskThread.threadShouldExit());
while (!taskThread.threadShouldExit())
;
};
taskThread.action = std::move(task);
taskThread.perform();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
std::this_thread::sleep_for(std::chrono::milliseconds(5));
REQUIRE(taskThread.isThreadRunning());
taskThread.signalThreadShouldExit();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
Expand All @@ -53,5 +54,23 @@ namespace mostly_harmless::testing {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
REQUIRE(!taskThread.isThreadRunning());
}

SECTION("Out of scope") {
std::chrono::time_point<std::chrono::steady_clock> start;
{
utils::TaskThread scopedThread;
auto task = [&scopedThread]() -> void {
while (!scopedThread.threadShouldExit()) {
scopedThread.sleep();
}
};
scopedThread.action = std::move(task);
scopedThread.perform();
start = std::chrono::steady_clock::now();
}
const auto end = std::chrono::steady_clock::now();
const auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
REQUIRE(duration < std::chrono::milliseconds(5));
}
}
}
} // namespace mostly_harmless::testing
20 changes: 18 additions & 2 deletions tests/utils/mostlyharmless_TimerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <mostly_harmless/utils/mostlyharmless_Timer.h>
#include <iostream>
#include <cmath>
#include <thread>

namespace mostly_harmless::tests {
TEST_CASE("Test Timer") {
Expand All @@ -24,9 +25,24 @@ namespace mostly_harmless::tests {
};
timer.action = std::move(timerCallback);
timer.run(static_cast<int>(100));
while(callCount < 5);
while (callCount < 5)
;
timer.stop();
REQUIRE(callCount >= 5);
}

SECTION("Test out-of-scope timer") {
std::atomic<int> count{ 0 };
{
mostly_harmless::utils::Timer scopedTimer;
auto task = [&count]() -> void {
++count;
};
scopedTimer.action = std::move(task);
scopedTimer.run(1);
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
REQUIRE(count == 0);
}
}
}
} // namespace mostly_harmless::tests

0 comments on commit 8bc43ea

Please sign in to comment.