-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmtimer.cpp
More file actions
95 lines (77 loc) · 2.56 KB
/
kmtimer.cpp
File metadata and controls
95 lines (77 loc) · 2.56 KB
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
/*
* Linux multithreaded timer
* =========================
* Author: DJ0ABR
*
* (c) DJ0ABR
* www.dj0abr.de
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*
* Repeatable Timer for various needs
* ==================================
*
*/
#include "kmtimer.h"
typedef struct _TIMERDEF_ {
int to_ms; // timeout value in ms
timer_t timerid;
void(*timer_func_handler_pntr)(void); // this user supplied function is called on timeout
struct itimerspec in, out;
} TIMERDEF;
#define MAXTIMERS 20 //maximum number of timers, may be increase if needed
TIMERDEF timers[MAXTIMERS];
int timernum = 0;
// callback function. The timer number is in val.sival_int
void timercallback(union sigval val)
{
int tnum = val.sival_int;
timers[tnum].timer_func_handler_pntr();
}
int start_timer(int mSec, void(*timer_func_handler)(void))
{
timers[timernum].timer_func_handler_pntr = timer_func_handler;
timers[timernum].to_ms = mSec;
pthread_attr_t attr;
pthread_attr_init( &attr );
struct sched_param parm;
parm.sched_priority = 255;
pthread_attr_setschedparam(&attr, &parm);
struct sigevent sig;
sig.sigev_notify = SIGEV_THREAD;
sig.sigev_notify_function = timercallback;
sig.sigev_value.sival_int = timernum;
sig.sigev_notify_attributes = &attr;
if (timer_create(CLOCK_REALTIME, &sig, &(timers[timernum].timerid)) == 0)
{
timers[timernum].in.it_value.tv_sec = mSec / 1000;
timers[timernum].in.it_value.tv_nsec = (mSec % 1000) * 1000000;
timers[timernum].in.it_interval.tv_sec = mSec / 1000;
timers[timernum].in.it_interval.tv_nsec = (mSec % 1000) * 1000000;
if(timer_settime(timers[timernum].timerid, 0, &timers[timernum].in, &timers[timernum].out) == -1)
{
printf("timer_settime() failed, error: %d\n", errno);
return -1;
}
}
else
{
printf("timer_create() failed, error: %d\n", errno);
return -1;
}
int r = timernum;
timernum++;
return r;
}
void stop_timer(int timer)
{
timer_delete(timers[timer].timerid);
}