11 #define QT_ALLOC_STEP 4
15 time_t qt_period
; //timer period
16 time_t qt_nextcall
; //next call time (in secs)
17 unsigned int qt_times
; //number of times to be activated <0 = infinity
18 void (* qt_call
)(); //funct. to call
19 void *qt_arg
; // opt arg to the funct.
22 struct qt_timer
**qth
; // head of the active timer array
23 struct qt_timer
*qtf
; // free list
24 int maxqt
; //size of active timer array
26 static time_t gqtime
; // global time in secs, secs from the epoch
27 static int activeqt
; // number of active timers
28 static int countqt
; // counter for timer ID
30 time_t qtime() // returns global time (faster than time())
35 static sigset_t ss_alarm
, ss_old
;
38 if (sigprocmask(SIG_BLOCK
,&ss_alarm
,&ss_old
) < 0)
39 printlog(LOG_WARNING
,"error qtime_csenter\n");
44 if (sigprocmask(SIG_SETMASK
,&ss_old
,NULL
) < 0)
45 printlog(LOG_WARNING
,"error qtime_csexit\n");
48 unsigned int qtimer_add(time_t period
,int times
,void (*call
)(),void *arg
)
51 if (period
>0 && call
&& times
>=0) {
53 if (activeqt
>= maxqt
) {
54 int newmaxqt
=maxqt
+QT_ALLOC_STEP
;
55 qth
=realloc(qth
,newmaxqt
*sizeof(struct qt_timer
*));
59 /* it is not possible to use unitialized elements */
60 /*memset(qth+maxqt,0,QT_ALLOC_STEP*sizeof(struct qt_timer *));*/
65 qtf
=malloc(sizeof(struct qt_timer
));
69 /*all the fields but qt_arg get initialized */
70 /*memset(qtf,0,sizeof(struct qt_timer));*/
75 qth
[n
]->qt_n
=countqt
++;
76 qth
[n
]->qt_period
=period
;
77 qth
[n
]->qt_nextcall
=gqtime
+period
;
80 qth
[n
]->qt_times
=(times
==0)?-1:times
;
87 void qtimer_del(unsigned int n
)
90 for (i
=0; i
<activeqt
; i
++) {
91 if (n
==qth
[i
]->qt_n
) {
98 static void sig_alarm(int sig
)
103 //printf("%d\n",gqtime);
104 for (i
=0,j
=0; i
<activeqt
; i
++) {
105 if (qth
[i
]->qt_times
== 0)
107 //printf("timer %d eliminated\n",qth[i]->qt_n);
112 if (gqtime
>= qth
[i
]->qt_nextcall
) {
113 //printf("timer %d fires\n",qth[i]->qt_n);
114 qth
[i
]->qt_call(qth
[i
]->qt_arg
);
115 qth
[i
]->qt_nextcall
+=qth
[i
]->qt_period
;
116 if (qth
[i
]->qt_times
> 0 )
117 (qth
[i
]->qt_times
)--;
119 //printf("%d -> %d \n",i,j);
120 if (i
-j
) qth
[j
]=qth
[i
];
132 sa
.sa_handler
= sig_alarm
;
133 sa
.sa_flags
= SA_RESTART
;
134 if(sigaction(SIGALRM
, &sa
, NULL
) < 0){
135 printlog(LOG_WARNING
,"Setting handler for SIGALRM %s", strerror(errno
));
139 sigemptyset(&ss_alarm
);
140 sigaddset(&ss_alarm
,SIGALRM
);
142 it
.it_value
.tv_sec
= 1;
143 it
.it_value
.tv_usec
= 0 ;
144 it
.it_interval
.tv_sec
= 1;
145 it
.it_interval
.tv_usec
= 0 ;
146 setitimer(ITIMER_REAL
, &it
, NULL
);
160 qtimer_add(7,0,fun,NULL);
161 qtimer_add(3,0,fun,NULL);
162 qtimer_add(4,2,fun,NULL);