123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #include <ctime>
- #include <sys/time.h>
- #include <cerrno>
-
-
-
- #ifdef WIN32
- #include <windows.h>
- #endif
-
- #include "timing.hpp"
-
-
-
- using namespace std;
-
-
-
-
-
-
-
-
-
- #ifdef WIN32
-
-
-
-
-
-
-
-
-
- void Sleeper::doRawSleep(int x) {
- Sleep(x);
- }
-
- #else
-
-
-
-
-
- void Sleeper::doRawSleep(int x) {
- struct timespec sleeptime;
- struct timespec remaining;
- int result;
- remaining.tv_sec = x/1000;
- remaining.tv_nsec = (x%1000)*1000000;
- do {
- sleeptime.tv_sec = remaining.tv_sec;
- sleeptime.tv_nsec = remaining.tv_nsec;
- result = nanosleep(&sleeptime,&remaining);
- } while(0>result && EINTR==errno);
- }
-
- #endif
-
- Sleeper::Sleeper()
- :MillisecondsToSleep(0) {
- }
-
- Sleeper::Sleeper(int x) {
- setMillisecondsToSleep(x);
- }
-
- int Sleeper::setMillisecondsToSleep(int x) {
- if(x < MinimumSleeperTime ||
- x > MaximumSleeperTime)
- throw BadSleeperValue();
- MillisecondsToSleep = x;
- return MillisecondsToSleep;
- }
-
- int Sleeper::getMillisecondsToSleep() {
- return MillisecondsToSleep;
- }
-
- void Sleeper::sleep() {
- if(MillisecondsToSleep > 0) {
- doRawSleep(MillisecondsToSleep);
- } else {
- throw BadSleeperValue();
- }
- }
-
- void Sleeper::sleep(int x) {
- setMillisecondsToSleep(x);
- sleep();
- }
-
- void Sleeper::operator()() {
- sleep();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- PollTimer::PollTimer(int Nom, int Max) :
- NominalPollTime(MinimumSleeperTime),
- MaximumPollTime(MinimumSleeperTime) {
- setNominalPollTime(Nom);
- setMaximumPollTime(Max);
- }
-
- int PollTimer::setNominalPollTime(int Nom) {
- if(Nom < MinimumSleeperTime ||
- Nom > MaximumSleeperTime)
- throw BadPollTimerValue();
-
- NominalPollTime = Nom;
-
- if(MaximumPollTime < NominalPollTime)
- MaximumPollTime = NominalPollTime;
-
- reset();
- return NominalPollTime;
- }
-
- int PollTimer::setMaximumPollTime(int Max) {
- if(Max < MinimumSleeperTime ||
- Max > MaximumSleeperTime)
- throw BadPollTimerValue();
-
- MaximumPollTime = Max;
-
- if(MaximumPollTime < NominalPollTime)
- MaximumPollTime = NominalPollTime;
-
- reset();
- return MaximumPollTime;
- }
-
- void PollTimer::reset() {
- FibA = NominalPollTime;
- FibB = 0;
- LimitReached=false;
- }
-
- int PollTimer::pause() {
- int SleepThisTime = MaximumPollTime;
- if(LimitReached) {
- mySleeper.sleep(SleepThisTime);
- } else {
- SleepThisTime = FibA+FibB;
- if(SleepThisTime >= MaximumPollTime) {
- SleepThisTime = MaximumPollTime;
- LimitReached = true;
- } else {
- FibB=FibA;
- FibA=SleepThisTime;
- }
- mySleeper.sleep(SleepThisTime);
- }
- return SleepThisTime;
- }
-
-
-
-
-
-
- #ifdef WIN32
-
-
-
- #define TimerIsUnixBased (false)
-
- msclock Timer::getLocalRawClock() const {
- FILETIME t;
- msclock c;
- GetSystemTimeAsFileTime(&t);
- c = (unsigned long long int) t.dwHighDateTime << 32LL;
- c |= t.dwLowDateTime;
- c /= 10000;
- c -= EPOCH_DELTA_IN_MSEC;
- return c;
- }
-
- #else
-
-
-
- #define TimerIsUnixBased (true)
-
- msclock Timer::getLocalRawClock() const {
- struct timeval t;
- msclock c;
- gettimeofday(&t,NULL);
- c = t.tv_sec * 1000;
- c += t.tv_usec / 1000;
- return c;
- }
-
- #endif
-
- Timer::Timer() {
- start();
- }
-
- Timer::Timer(msclock startt):
- RunningFlag(true),
- StartTime(startt),
- StopTime(startt) {
- }
-
- void Timer::clear() {
- StartTime = StopTime = getLocalRawClock();
- RunningFlag = false;
- }
-
- msclock Timer::start() {
- return start(getLocalRawClock());
- }
-
- msclock Timer::start(msclock startt) {
- StartTime = StopTime = startt;
- RunningFlag = true;
- return StartTime;
- }
-
- msclock Timer::getStartClock() { return StartTime; }
-
- bool Timer::isRunning() { return RunningFlag; }
-
- msclock Timer::getElapsedTime() const {
- msclock AssumedStopTime;
- if(RunningFlag) {
- AssumedStopTime = getLocalRawClock();
- } else {
- AssumedStopTime = StopTime;
- }
- msclock delta = AssumedStopTime - StartTime;
- return delta;
- }
-
- msclock Timer::stop() {
- StopTime = getLocalRawClock();
- RunningFlag=false;
- return StopTime;
- }
-
- msclock Timer::getStopClock() { return StopTime; }
-
- double Timer::getElapsedSeconds() const {
- msclock e = getElapsedTime();
- double secs = (double) e / 1000.0;
- return secs;
- }
-
- bool Timer::isUnixBased() { return TimerIsUnixBased; }
-
- msclock Timer::toWindowsEpoch(msclock unixt) {
- return (unixt + EPOCH_DELTA_IN_MSEC);
- }
-
- msclock Timer::toUnixEpoch(msclock win32t) {
- return (win32t - EPOCH_DELTA_IN_MSEC);
- }
-
-
-
-
-
- Timeout::Timeout(msclock duration):myDuration(duration) { }
-
- msclock Timeout::setDuration(msclock duration) {
- myDuration = duration;
- return myDuration;
- }
-
- msclock Timeout::getDuration() {
- return myDuration;
- }
-
- msclock Timeout::restart() {
- return myTimer.start();
- }
-
- msclock Timeout::getElapsedTime() {
- return myTimer.getElapsedTime();
- }
-
- msclock Timeout::getRemainingTime() {
- msclock remaining = 0ULL;
- msclock elapsed = myTimer.getElapsedTime();
- if(elapsed < myDuration) {
- remaining = myDuration - elapsed;
- }
- return remaining;
- }
-
- bool Timeout::isExpired() {
- return (!(myTimer.getElapsedTime() < myDuration));
- }
|