annotate linux/timer.c @ 6654:8047e1bdb4b0

sync
author nexus
date Sat, 06 Jul 2002 08:20:07 +0000
parents df50da00260a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4863
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
1 /* POSIX compatible timer callback */
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
2 #include <sys/time.h>
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
3 #include <signal.h>
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
4 #include <stddef.h>
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
5
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
6 #include "timer.h"
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
7
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
8 static timer_callback *user_func = NULL;
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
9 static struct itimerval otimer;
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
10 static void (*old_alrm)(int) = SIG_DFL;
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
11
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
12 static void my_alarm_handler( int signo )
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
13 {
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
14 if(user_func) (*user_func)();
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
15 }
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
16
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
17 unsigned set_timer_callback(unsigned ms,timer_callback func)
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
18 {
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
19 unsigned ret;
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
20 struct itimerval itimer;
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
21 user_func = func;
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
22 getitimer(ITIMER_REAL,&otimer);
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
23 old_alrm = signal(SIGALRM,my_alarm_handler);
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
24 signal(SIGALRM,my_alarm_handler);
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
25 itimer.it_interval.tv_sec = 0;
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
26 itimer.it_interval.tv_usec = ms*1000;
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
27 itimer.it_value.tv_sec = 0;
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
28 itimer.it_value.tv_usec = ms*1000;
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
29 setitimer(ITIMER_REAL,&itimer,NULL);
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
30 getitimer(ITIMER_REAL,&itimer);
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
31 ret = itimer.it_interval.tv_sec*1000 + itimer.it_interval.tv_usec/1000;
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
32 if(!ret) restore_timer();
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
33 return ret;
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
34 }
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
35
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
36 void restore_timer(void)
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
37 {
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
38 signal(SIGALRM,old_alrm);
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
39 setitimer(ITIMER_REAL,&otimer,NULL);
df50da00260a POSIX compatible timer's callback
nick
parents:
diff changeset
40 }