#include #include "sucycletimer.h" ///////////////////////////////////////////////////////////////////////////// CCycleTimer::CCycleTimer(unsigned int nMaxCycleIntervalMilliSec1, unsigned int nMaxCycleIntervalMilliSec2, unsigned int nMaxCycleIntervalMilliSec3) : m_nMaxCycleNanoSec1(nMaxCycleIntervalMilliSec1 * _NSEC_PER_MSEC), m_nMaxCycleNanoSec2(nMaxCycleIntervalMilliSec2 * _NSEC_PER_MSEC), m_nMaxCycleNanoSec3(nMaxCycleIntervalMilliSec3 * _NSEC_PER_MSEC) { memset(&m_ts, 0, sizeof(m_ts)); ::clock_getres(CLOCK_MONOTONIC, &m_res); } CCycleTimer::CCycleTimer(const struct timespec *pts1, const struct timespec *pts2, const struct timespec *pts3) : m_nMaxCycleNanoSec1(pts1 ? Timespec2NanoSec(*pts1) : 0), m_nMaxCycleNanoSec2(pts2 ? Timespec2NanoSec(*pts2) : 0), m_nMaxCycleNanoSec3(pts3 ? Timespec2NanoSec(*pts3) : 0) { memset(&m_ts, 0, sizeof(m_ts)); ::clock_getres(CLOCK_MONOTONIC, &m_res); } CCycleTimer::~CCycleTimer(void) { } ///////////////////////////////////////////////////////////////////////////// cy_time_t CCycleTimer::GetNanoTick(struct timespec *pts) { struct timespec ts; struct timespec &rts = pts ? *pts : ts; ::clock_gettime(CLOCK_MONOTONIC, &rts); return Timespec2NanoSec(rts); } cy_time_t CCycleTimer::GetNanoSecElapsed(const struct timespec *ptsComparand) const { struct timespec ts; if(!ptsComparand) { ::clock_gettime(CLOCK_MONOTONIC, &ts); ptsComparand = &ts; } return TimespecDiffNanoSec(*ptsComparand, m_ts); } int CCycleTimer::Sleep(const cy_time_t &rnMaxCycleNanoSec, bool bResumeOnIntr) const { cy_time_t nIntv; struct timespec ts, rem; ::clock_gettime(CLOCK_MONOTONIC, &ts); if((nIntv = rnMaxCycleNanoSec - GetNanoSecElapsed(&ts)) > 0) { int nRet; NanoSec2Timespec(nIntv, ts); if((nRet = ::clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &rem))) { if(bResumeOnIntr) { while(nRet == EINTR) { nRet = ::clock_nanosleep(CLOCK_MONOTONIC, 0, &rem, &rem); } } } return nRet; } return 0; } void CCycleTimer::TimespecAddSubNanoSec(struct timespec &rts, cy_time_t nNanoSec) { cy_time_t n = Timespec2NanoSec(rts) + nNanoSec; NanoSec2Timespec(n, rts); }