File: timer.c

package info (click to toggle)
abuse 2.00-12
  • links: PTS
  • area: main
  • in suites: slink
  • size: 12,708 kB
  • ctags: 15,389
  • sloc: ansic: 115,852; cpp: 6,792; lisp: 2,066; sh: 1,734; makefile: 1,601; asm: 264
file content (102 lines) | stat: -rw-r--r-- 1,704 bytes parent folder | download | duplicates (7)
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
#include "system.h"

#ifdef __UNIX_XWIN
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <signal.h>


//#ifndef setitimer
//extern "C" {
//int setitimer(int Which, struct itimerval *Value,
//				struct itimerval *Ovalue);
//} ;
//#endif

struct sigaction sa;
typedef void (*int_handler)();
#define SETSIG(sig,fun) (int_handler)sa.sa_handler=fun; \
			sa.sa_flags=0; \
			sigemptyset(&sa.sa_mask); \
			sigaddset(&sa.sa_mask,SIGALRM); \
			sigaction(sig,&sa,NULL);



//void timer_handler()
//{
//  SETSIG(SIGALRM,timer_handler);
//  printf("%ld\n",jclock++);
//  fflush(stdout);
//}


void init_timer(int_handler int_proc, long utime)
{
  itimerval newt;
  SETSIG(SIGALRM,int_proc);
  newt.it_interval.tv_sec=0;
  newt.it_interval.tv_usec=utime;
  newt.it_value.tv_sec=0;
  newt.it_value.tv_usec=utime;
  setitimer(ITIMER_REAL,&newt,NULL);
}
#else
  #ifdef __BCPLUSPLUS__
  #include <stdio.h>
  #include <dos.h>
  #include <conio.h>

  #define INTR 0X1C    /* The clock tick interrupt */
  void interrupt ( *oldhandler)(...);

  int count=0;

  void interrupt handler(...)
  {
  /* increase the global counter */
     count++;

  /* call the old routine */
     oldhandler();
  }

  int main(void)
  {
  /* save the old interrupt vector */
     oldhandler = getvect(INTR);

  /* install the new interrupt handler */
     setvect(INTR, handler);

  /* loop until the counter exceeds 20 */
     while (count < 20)
	printf("count is %d\n",count);

  /* reset the old interrupt handler */
     setvect(INTR, oldhandler);

     return 0;
  }



  #else
  #error Timer not supported for this compiler! Use Borland or gcc
  #endif
#endif