00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <sys/time.h>
00022 #include <unistd.h>
00023
00024
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027
00028 #include <archon/util/time.H>
00029 #include <archon/util/adaptive_skip.H>
00030
00031
00032 namespace Archon
00033 {
00034 namespace Utilities
00035 {
00036 AdaptiveSkip::AdaptiveSkip(unsigned long millisBetweenOutTicks,
00037 unsigned long checksPerOutTick):
00038 timeOfLastCheck(getTimeInMilliSeconds()),
00039 timeOfLastOutTick(timeOfLastCheck - millisBetweenOutTicks),
00040 millisBetweenOutTicks(millisBetweenOutTicks),
00041 checksPerOutTick(checksPerOutTick),
00042 skippedTicks(0),
00043 ticksPerCheck(1) {}
00044
00045 unsigned long AdaptiveSkip::getTimeInMilliSeconds()
00046 {
00047 return Time::now().getMilliSeconds();
00048 }
00049
00050 bool AdaptiveSkip::check()
00051 {
00052
00053 const unsigned long now = getTimeInMilliSeconds();
00054 unsigned long timeSinceLastCheck = now - timeOfLastCheck;
00055 if(timeSinceLastCheck == 0) timeSinceLastCheck = 1;
00056 ticksPerCheck = int(double(ticksPerCheck) / timeSinceLastCheck *
00057 millisBetweenOutTicks/checksPerOutTick);
00058 if(ticksPerCheck == 0) ticksPerCheck = 1;
00059 timeOfLastCheck = now;
00060 skippedTicks = 0;
00061
00062 if(now - timeOfLastOutTick > millisBetweenOutTicks)
00063 {
00064 timeOfLastOutTick = now;
00065 return true;
00066 }
00067
00068 return false;
00069 }
00070 }
00071 }