Skip to content

Commit e7afeea

Browse files
authored
Merge pull request #49 from sy-c/master
Timer::increment()
2 parents 51f9c7e + 1d2bb2a commit e7afeea

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

include/Common/Timer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ class Timer
4545
/// Reset timer by adding timeout value to starting time value.
4646
/// Usefull when called in loops, prevents loosing time elapsed after previous timeout.
4747
/// (e.g.: to implement timeout every 1 second without drift in time)
48-
void increment();
48+
/// If ensureFuture is true, value is incremented until next occurence is in the future (and only if timeout has occured already),
49+
/// to avoid possibly several successive immediate timeouts again when multiple timeout periods have been missed.
50+
void increment(bool ensureFuture = false);
4951

5052
/// Check if time elapsed since timer reset (or last increment set) is bigger than timeout value set.
5153
/// \return Returns 1 if timeout, 0 otherwise.

src/Timer.cxx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,17 @@ void Timer::reset(int timeout)
3636
tmax = timeout / 1000000.0;
3737
}
3838

39-
void Timer::increment()
39+
void Timer::increment(bool ensureFuture)
4040
{
41-
t0 += std::chrono::microseconds((int)(tmax * 1000000.0));
41+
if (ensureFuture) {
42+
double tnext = getRemainingTime();
43+
if ((tnext <= 0) && (tmax > 0)) {
44+
int n = (int)(1 - (tnext / tmax));
45+
t0 += std::chrono::microseconds((int)(n * tmax * 1000000.0));
46+
}
47+
} else {
48+
t0 += std::chrono::microseconds((int)(tmax * 1000000.0));
49+
}
4250
}
4351

4452
int Timer::isTimeout()
@@ -64,4 +72,3 @@ double Timer::getRemainingTime()
6472

6573
} // namespace Common
6674
} // namespace AliceO2
67-

test/testTimer.cxx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ BOOST_AUTO_TEST_CASE(timer_test)
4040
success = 1;
4141
}
4242

43+
// test increment
44+
printf("\nTest increment (ensureFuture=0)\n");
45+
// this should blindly increase by fixed amount of time
46+
t.reset(100000);
47+
usleep(350000);
48+
for (int i = 1; i <= 5; i++) {
49+
printf("iteration %d : timeout = %d timeRemaining = %.2fs\n", i, (int)t.isTimeout(), t.getRemainingTime());
50+
t.increment(0);
51+
}
52+
printf("\nTest increment (ensureFuture=1)\n");
53+
// this should update timeout to next occurence, with respect to start and given period
54+
t.reset(100000);
55+
usleep(250000);
56+
for (int i = 1; i <= 5; i++) {
57+
printf("iteration %d : timeout = %d timeRemaining = %.2fs\n", i, (int)t.isTimeout(), t.getRemainingTime());
58+
t.increment(1);
59+
}
60+
4361
BOOST_CHECK_EQUAL(success, 1);
4462
}
4563

0 commit comments

Comments
 (0)