Commit afdcc051 authored by aligungr's avatar aligungr

NTS pause capability

parent 06f2f969
......@@ -10,6 +10,7 @@
#include "common.hpp"
#define WAIT_TIME_IF_NO_TIMER 500
#define PAUSE_POLLING_PERIOD 150
static NtsMessage *TimerExpiredMessage(TimerInfo *timerInfo)
{
......@@ -204,7 +205,17 @@ void NtsTask::start()
{
if (this->isQuiting)
break;
this->onLoop();
if (pauseReqCount > 0)
{
utils::Sleep(PAUSE_POLLING_PERIOD);
pauseConfirmed = true;
}
else
{
pauseConfirmed = false;
this->onLoop();
}
}
}};
}
......@@ -233,3 +244,20 @@ void NtsTask::quit()
onQuit();
}
void NtsTask::requestPause()
{
if (++pauseReqCount < 0)
throw std::runtime_error("NTS pause overflow");
}
void NtsTask::requestUnpause()
{
if (--pauseReqCount < 0)
throw std::runtime_error("NTS un-pause underflow");
}
bool NtsTask::isPauseConfirmed()
{
return pauseConfirmed;
}
......@@ -112,7 +112,9 @@ class NtsTask
TimerBase timerBase{};
std::mutex mutex{};
std::condition_variable cv{};
std::atomic<bool> isQuiting{};
std::atomic_bool isQuiting{};
std::atomic_int pauseReqCount{};
std::atomic_bool pauseConfirmed{};
std::thread thread;
public:
......@@ -163,4 +165,16 @@ class NtsTask
// - Always call this function before destroying the task.
// - Calling quit() before calling start() is undefined behaviour.
void quit();
// - NTS task begin to be paused. The pause request is confirmed after the next loop() call.
// - If the task quits immediately and loop() never called again, this pause request will never be confirmed.
// - This should not be used if the task is quiting or quited.
void requestPause();
// - NTS task begin to be un-paused. The un-pause request is confirmed after the next loop() call.
// - This should not be used if the task is quiting or quited.
void requestUnpause();
// - Returns true iff pause was requested and now is confirmed.
bool isPauseConfirmed();
};
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment