Files

143 lines
4.2 KiB
C++

/**
* @file include/ActivMiddleware/ActivBase/HighAccuracyWait.h
*
* @brief High accuracy wait function object.
*/
#if (!defined ACTIV_BASE_HIGH_ACCURACY_WAIT_H)
#define ACTIV_BASE_HIGH_ACCURACY_WAIT_H
#include "ActivMiddleware/ActivBase/External.h"
#include "ActivMiddleware/ActivBase/IntervalTimer.h"
//! Activ namespace.
namespace Activ
{
/**
* @brief Do a smooth, high(er) accuracy wait.
*
* RETURN_TYPE the return type of the function operator and the wait function passed to it.
* TIMEOUT_RETURN_VALUE the value that indicated a timeout from the sleep function passed to the function operator.
*/
template<typename RETURN_TYPE, RETURN_TYPE TIMEOUT_RETURN_VALUE>
class HighAccuracyWait
{
public:
//! Infinite timeout constant.
static const uint64_t infiniteTimeout = Boost::integer_traits<uint64_t>::const_max;
/**
* @brief Function operator.
*
* The function will wait at least timeoutPeriod milliseconds, unless waitFn returns something other than
* TIMEOUT_RETURN_VALUE.
*
* May invoke the provided waitFn multiple times in order to achieve an accurate wait of the requested period.
*
* @param timeoutPeriod period to wait, in milliseconds.
* @param waitFn wait function object returning TIMEOUT_RETURN_VALUE if it times out. waitFn should take
* a uint64_t parameter with the *microseconds* to wait.
*
* @return TIMEOUT_RETURN_VALUE, or the value returned by waitFn if not TIMEOUT_RETURN_VALUE.
*/
template<typename SLEEP_FN>
const RETURN_TYPE operator()(const uint32_t timeoutPeriod, const SLEEP_FN &waitFn) const
{
const uint64_t maxSmoothWaitMicroseconds = 50000;
#if (defined ACTIV_PLATFORM_WINDOWS)
const uint64_t clockResolution = IntervalTimer::GetClockResolution();
#else
const uint64_t clockResolution = GetMaximum<uint64_t>(IntervalTimer::GetClockResolution(), 50);
#endif
const uint64_t halfClockResolution = clockResolution / 2;
const uint64_t startTimeStamp = IntervalTimer::GetTimeStamp();
const uint64_t targetTimeStamp = startTimeStamp + ((timeoutPeriod * IntervalTimer::GetFrequency()) / 1000);
uint64_t currentTimeStamp = startTimeStamp;
bool isDone = false;
size_t numberOfWaits = 0;
while (!isDone)
{
uint64_t thisTimeoutUs;
if (0 == timeoutPeriod)
{
// With a 0 timeout, we still want to wait/sleep at least once so don't break out immediately.
thisTimeoutUs = 0;
isDone = true;
}
#if (defined ACTIV_PLATFORM_WINDOWS)
else if ((1 == timeoutPeriod) && (0 == numberOfWaits))
{
// Initial wait must be of at least 1ms, otherwise we could be busy waiting a lot.
// Downside is 1 ms waits might not be particularly accurate, depending on the clock resolution.
thisTimeoutUs = 1000;
}
#endif
else if (ACTIV_WAIT_INFINITE == timeoutPeriod)
{
thisTimeoutUs = infiniteTimeout;
}
else
{
if (ACTIV_LIKELY(targetTimeStamp >= startTimeStamp))
{
if ((currentTimeStamp >= targetTimeStamp) || (currentTimeStamp < startTimeStamp))
break;
}
else
{
if ((currentTimeStamp >= targetTimeStamp) && (currentTimeStamp < startTimeStamp))
break;
}
// Remaining period in microseconds.
thisTimeoutUs = ((targetTimeStamp - currentTimeStamp) * 1000000) / IntervalTimer::GetFrequency();
if (thisTimeoutUs < maxSmoothWaitMicroseconds)
{
const uint64_t resolutionRemainder = (thisTimeoutUs % clockResolution);
if (resolutionRemainder >= halfClockResolution)
{
if (thisTimeoutUs >= clockResolution)
thisTimeoutUs -= clockResolution;
else if (numberOfWaits > 0)
thisTimeoutUs = 0;
}
else
{
if (thisTimeoutUs >= halfClockResolution)
thisTimeoutUs -= halfClockResolution;
else if (numberOfWaits > 0)
thisTimeoutUs = 0;
}
}
else
{
// Quite a big wait. Just wait the whole period - super accuracy not as important with long waits.
}
}
const RETURN_TYPE returnValue = waitFn(thisTimeoutUs);
++numberOfWaits;
if (TIMEOUT_RETURN_VALUE != returnValue)
return returnValue;
currentTimeStamp = IntervalTimer::GetTimeStamp();
}
return TIMEOUT_RETURN_VALUE;
}
};
} // namespace Activ
#endif // (!defined ACTIV_BASE_HIGH_ACCURACY_WAIT_H)