1 /**
2 * @namespace biewlib
3 * @file biewlib/sysdep/generic/posix/timer.c
4 * @brief This file contains implementation of timer depended part for POSIX.
5 * @version -
6 * @remark this source file is part of Binary vIEW project (BIEW).
7 * The Binary vIEW (BIEW) is copyright (C) 1995 Nickols_K.
8 * All rights reserved. This software is redistributable under the
9 * licence given in the file "Licence.en" ("Licence.ru" in russian
10 * translation) distributed in the BIEW archive.
11 * @note Requires POSIX compatible development system
12 *
13 * @author Nickols_K
14 * @since 1999
15 * @note Development, fixes and improvements
16 **/
17 #include <sys/time.h>
18 #include <signal.h>
19 #include <stddef.h>
20
21 #include "biewlib/biewlib.h"
22
23 static timer_callback *user_func = NULL;
24 static struct itimerval otimer;
25 static void (*old_alrm)(int) = SIG_DFL;
26
my_alarm_handler(int signo)27 static void my_alarm_handler( int signo )
28 {
29 if(user_func) (*user_func)();
30 UNUSED(signo);
31 }
32
__OsSetTimerCallBack(unsigned ms,timer_callback func)33 unsigned __FASTCALL__ __OsSetTimerCallBack(unsigned ms,timer_callback func)
34 {
35 unsigned ret;
36 struct itimerval itimer;
37 user_func = func;
38 getitimer(ITIMER_REAL,&otimer);
39 old_alrm = signal(SIGALRM,my_alarm_handler);
40 signal(SIGALRM,my_alarm_handler);
41 itimer.it_interval.tv_sec = 0;
42 itimer.it_interval.tv_usec = ms*1000;
43 itimer.it_value.tv_sec = 0;
44 itimer.it_value.tv_usec = ms*1000;
45 setitimer(ITIMER_REAL,&itimer,NULL);
46 getitimer(ITIMER_REAL,&itimer);
47 ret = itimer.it_interval.tv_sec*1000 + itimer.it_interval.tv_usec/1000;
48 if(!ret) __OsRestoreTimer();
49 return ret;
50 }
51
52 /* Restore time callback function to original
53 state */
__OsRestoreTimer(void)54 void __FASTCALL__ __OsRestoreTimer(void)
55 {
56 signal(SIGALRM,old_alrm);
57 setitimer(ITIMER_REAL,&otimer,NULL);
58 }
59