-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.cpp
108 lines (84 loc) · 2.36 KB
/
timer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* timer.cpp
*
* Created on: Mar 25, 2009
* Author: pushkar
*/
#include <stdlib.h>
#include "timer.h"
namespace Timer {
double min_frequency = 500;
int total_handles = 0;
void (*_callback[10])(void);
double _intervals[10];
double * _last_time[10];
int _handle_time_index[10];
struct sigaction sact;
struct itimerval value, ovalue, pvalue;
struct timespec current_time;
double _init_time, _cur_time;
int sigcount = 0, timer_set = 0;
double getCurrentTime() {
double t;
clock_gettime(CLOCK_REALTIME, ¤t_time);
t = (double) current_time.tv_sec + (double) current_time.tv_nsec / 1000000000;
return t;
}
void subscribeToTimer(void (*func)(void), double interval, double* last_time) {
_callback[total_handles] = func;
_handle_time_index[total_handles] = interval/min_frequency;
_intervals[total_handles] = interval / 1000000;
_last_time[total_handles] = last_time;
total_handles++;
}
double _diff_time;
void iHandler(int sig) {
sigcount++;
int i = 0;
sig = 0;
#ifdef DEBUG
_diff_time = Timer::getCurrentTime() - _cur_time;
#endif
_cur_time = Timer::getCurrentTime();
if(timer_set == 1 && sigcount > 1) {
while(i < total_handles) {
if(((_cur_time - *_last_time[i]) > _intervals[i])
&& _handle_time_index[i] == sigcount % 10) {
_callback[i]();
getitimer(ITIMER_REAL, &pvalue);
value.it_interval.tv_sec = 0;
value.it_interval.tv_usec = 0;
setitimer(ITIMER_REAL, &value, NULL);
}
i++;
}
}
}
int startTimer() {
if(timer_set == 1)
return 1;
for(int i = 0; i < 10; i++) {
_last_time[i] = (double*) malloc (sizeof(double));
}
_init_time = Timer::getCurrentTime();
sigemptyset(&sact.sa_mask);
sact.sa_flags = 0;
sact.sa_handler = iHandler;
sigaction( SIGALRM, &sact, NULL);
getitimer(ITIMER_REAL, &pvalue);
value.it_interval.tv_sec = 0;
value.it_interval.tv_usec = min_frequency;
value.it_value.tv_sec = 0;
value.it_value.tv_usec = 10000;
int result = setitimer(ITIMER_REAL, &value, &ovalue);
if( ovalue.it_interval.tv_sec != pvalue.it_interval.tv_sec ||
ovalue.it_interval.tv_usec != pvalue.it_interval.tv_sec ||
ovalue.it_value.tv_sec != pvalue.it_value.tv_sec ||
ovalue.it_value.tv_usec != pvalue.it_value.tv_usec ) {
fprintf(stderr, "Real time interval timer mismatch\n" );
result = -1;
}
timer_set++;
return result;
}
}