-
Notifications
You must be signed in to change notification settings - Fork 0
/
art_timer.cpp
290 lines (228 loc) · 6.4 KB
/
art_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "art_timer.h"
//------------------------------------------------------------------
//---------------- ART TIMER ----------------------------------
//------------------------------------------------------------------
ArtTimer::ArtTimer(float freq_init, bool verbose_init, int priority_init, int policy_init)
{
verbose = verbose_init;
errors.error = 0;
current_interval = 0;
timer_created = false;
setItimerSpec(timerconf, 0);
setThreadAttr(attr, priority_init, policy_init);
//Signal event tuning
clock_id = CLOCK_REALTIME;
sigval val;
val.sival_ptr = this;
ArtSignals:: createEventThread(sigev, thread_fun , &attr, val );
//Create timer
int create_result = -1;
create_result= timer_create(clock_id, &sigev, &timer_id );
//fprintf(stderr, "Errno=%s result=%d Signo=%d \n", strerror(errno), create_result, sigev.sigev_signo);
if (create_result)
{
errors.CREATING_ERROR = 1;
if(verbose) fprintf(stderr, "\nWARNING :: ArtTimer :: Can't create timer \n");
timer_created = false;
stop();
}
else
{
timer_created = true;
errors.CREATING_ERROR = 0;
}
if (timer_created && freq_init > 0)
{
startFreq(freq_init);
}
//cout<<"Timer :: Timer started :: "<<timer_created<<" "<<freq_init<<endl;
}
void ArtTimer::setItimerSpec(itimerspec& timerconf, long int nsec, long int delay_nsec)
{
timerconf.it_interval.tv_sec = nsec / 1000000000;
timerconf.it_interval.tv_nsec = nsec % 1000000000;
timerconf.it_value.tv_sec = delay_nsec / 1000000000;
timerconf.it_value.tv_nsec = delay_nsec % 1000000000 + 1;
//fprintf (stderr, "\nArtTimer :: \nSec=%ld \nNSec=%ld \nDelay_Sec=%ld \nDelay_NSec=%ld", (long int)timerconf.it_interval.tv_sec, (long int) timerconf.it_interval.tv_nsec, (long int)timerconf.it_value.tv_sec, (long int) timerconf.it_value.tv_nsec );
}
void ArtTimer::startNano(long int nsec)
{
if (nsec >= 0) current_interval = nsec;
setItimerSpec(timerconf, current_interval);
int settime_res = -1;
settime_res = timer_settime( timer_id, 0, &timerconf, 0 );
if(settime_res)
{
if(verbose) fprintf(stderr, "\nWARNING :: ArtTimer :: Can't set time while Starting \n");
//fprintf(stderr, "Errno=%s result=%d Signo=%d \n", strerror(errno), settime_res, sigev.sigev_signo);
errors.SETTIME_ERROR = 1;
}
else
{
errors.SETTIME_ERROR = 0;
}
}
void ArtTimer::stop()
{
if(!timer_created)
return;
setItimerSpec(timerconf, 0);
int settime_res = -1;
settime_res = timer_settime( timer_id, 0, &timerconf, 0 );
if(settime_res)
{
if(verbose) fprintf(stderr, "\nWARNING :: ArtTimer :: Can't set time while Stopping \n");
errors.SETTIME_ERROR = 1;
}
else
{
errors.SETTIME_ERROR = 0;
}
}
void ArtTimer::thread_fun(sigval val)
{
ArtTimer * obj = (ArtTimer *) val.sival_ptr;
obj->run();
}
void ArtTimer::run()
{
cout<<"ArtTimer :: Works !!!"<<endl;
}
void ArtTimer::setThreadAttr(pthread_attr_t& attr_init, int priority, int policy)
{
pthread_attr_init ( &attr_init );//Initialisation for the thread's attributes by default (inherited from the thread creater)
pthread_attr_setinheritsched(&(attr_init), PTHREAD_EXPLICIT_SCHED);
if (priority >= 0)
{
sched_param param;
param.sched_priority = priority;
pthread_attr_setschedparam (&attr_init, ¶m);
}
if (policy >= 0)
{
pthread_attr_setschedpolicy(&attr_init, policy);
}
}
//------------------------------------------------------------------
//---------------- ART WAIT TIMER ----------------------------------
//------------------------------------------------------------------
ArtWaitTimer::ArtWaitTimer( int type_init, bool verbose_init)
{
type = type_init;
verbose = verbose_init;
errors.error = 0;
current_interval = 0;
timer_created = false;
setItimerSpec(timerconf, 0);
sigemptyset(&sigmask_set);
}
ArtWaitTimer::~ArtWaitTimer()
{
stop();
}
void ArtWaitTimer:: startNano(long int nsec)
{
if (!timer_created)
{
//Signal event tuning
clock_id = CLOCK_REALTIME;
//pid = pthread_self();
pid = getpid();
switch(type)
{
case TYPE_PROCESS:
ArtSignals::createEventSignal(sigev);
break;
default:
ArtSignals::createEventThreadSignal(sigev, pid);
}
current_interval = nsec;
//Create timer
int create_result = -1;
create_result= timer_create(clock_id, &sigev, &timer_id );
//fprintf(stderr, "Errno=%s result=%d Signo=%d \n", strerror(errno), create_result, sigev.sigev_signo);
if (create_result)
{
errors.CREATING_ERROR = 1;
if(verbose) fprintf(stderr, "\nWARNING :: ArtTimer :: Can't create timer \n");
timer_created = false;
}
else
{
errors.CREATING_ERROR = 0;
timer_created = true;
}
}
ArtSignals:: setSigMaskAddSig(type, sigmask_set, sigev.sigev_signo );
if (nsec >= 0) current_interval = nsec;
setItimerSpec(timerconf, current_interval);
int settime_res = -1;
#ifdef __QNX__
settime_res = timer_settime( timer_id, 0, &timerconf, 0 );
#else
settime_res = timer_settime( timer_id, 0, &timerconf, 0 );
#endif
if(settime_res)
{
if(verbose) fprintf(stderr, "\nWARNING :: ArtTimer :: Can't set time while Starting \n");
fprintf(stderr, "Errno=%s result=%d Signo=%d \n", strerror(errno), settime_res, sigev.sigev_signo);
errors.SETTIME_ERROR = 1;
}
else
{
errors.SETTIME_ERROR = 0;
}
}
void ArtWaitTimer::pause()
{
if(!timer_created)
return;
setItimerSpec(timerconf, 0);
int settime_res = -1;
#ifdef __QNX__
settime_res = timer_settime( timer_id, 0, &timerconf, 0 );
#else
settime_res = timer_settime( timer_id, 0, &timerconf, 0 );
#endif
if(settime_res)
{
if(verbose) fprintf(stderr, "\nWARNING :: ArtTimer :: Can't set time while Pausing \n");
errors.SETTIME_ERROR = 1;
}
else
{
errors.SETTIME_ERROR = 0;
}
}
void ArtWaitTimer::stop()
{
if(!timer_created)
return;
timer_delete( timer_id );
timer_created = false;
}
void ArtWaitTimer::wait()
{
sigwait(&sigmask_set, &(sigev.sigev_signo) );
}
void ArtWaitTimer::setItimerSpec(itimerspec& timerconf, long int nsec, long int delay_nsec)
{
timerconf.it_interval.tv_sec = nsec / 1000000000;
timerconf.it_interval.tv_nsec = nsec % 1000000000;
timerconf.it_value.tv_sec = delay_nsec / 1000000000;
timerconf.it_value.tv_nsec = delay_nsec % 1000000000 + 1;
}
int ArtWaitTimer::getSignNo()
{
return sigev.sigev_signo;
}
sigset_t ArtWaitTimer::getSigMask()
{
return sigmask_set;
}
sigset_t ArtWaitTimer::setSigMask(int type_init)
{
sigset_t cur_mask;
ArtSignals:: setSigMaskAddSig(type_init, cur_mask, sigev.sigev_signo);
return cur_mask;
}