diff --git a/src/common/wait.c b/src/common/wait.c index 1106fdaa8..d38fa4cbb 100644 --- a/src/common/wait.c +++ b/src/common/wait.c @@ -16,11 +16,14 @@ struct Wait { MemContext *memContext; // Context that contains the wait handler TimeMSec waitTime; // Total time to wait (in usec) + TimeMSec remainTime; // Wait time remaining (in usec) TimeMSec sleepTime; // Next sleep time (in usec) TimeMSec sleepPrevTime; // Previous time slept (in usec) TimeMSec beginTime; // Time the wait began (in epoch usec) }; +OBJECT_DEFINE_GET(Remaining, const, WAIT, TimeMSec, remainTime); + OBJECT_DEFINE_FREE(WAIT); /**********************************************************************************************************************************/ @@ -45,6 +48,7 @@ waitNew(TimeMSec waitTime) { .memContext = MEM_CONTEXT_NEW(), .waitTime = waitTime, + .remainTime = waitTime, }; // Calculate first sleep time -- start with 1/10th of a second for anything >= 1 second @@ -96,6 +100,7 @@ waitMore(Wait *this) // Store new sleep times this->sleepPrevTime = this->sleepTime; this->sleepTime = sleepNextTime; + this->remainTime = this->waitTime - elapsedTime; } // Else set sleep to zero so next call will return false else diff --git a/src/common/wait.h b/src/common/wait.h index ab7acb9f9..e91b8b184 100644 --- a/src/common/wait.h +++ b/src/common/wait.h @@ -25,6 +25,12 @@ Functions // Wait and return whether the caller has more time left bool waitMore(Wait *this); +/*********************************************************************************************************************************** +Getters/Setters +***********************************************************************************************************************************/ +// How much time is remaining? Recalculated each time waitMore() is called. +TimeMSec waitRemaining(const Wait *this); + /*********************************************************************************************************************************** Destructor ***********************************************************************************************************************************/ diff --git a/test/src/module/common/waitTest.c b/test/src/module/common/waitTest.c index 573cc2264..46e727188 100644 --- a/test/src/module/common/waitTest.c +++ b/test/src/module/common/waitTest.c @@ -22,6 +22,7 @@ testRun(void) TimeMSec begin = timeMSec(); TEST_ASSIGN(wait, waitNew(200), "new wait = 0.2 sec"); + TEST_RESULT_UINT(waitRemaining(wait), 200, " check remaining time"); TEST_RESULT_UINT(wait->waitTime, 200, " check wait time"); TEST_RESULT_UINT(wait->sleepTime, 20, " check sleep time"); TEST_RESULT_UINT(wait->sleepPrevTime, 0, " check sleep prev time");