[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / runtime / main / Itimer.lc
1 %
2 % (c) The AQUA Project, Glasgow University, 1995
3 %
4 %************************************************************************
5 %*                                                                      *
6 \section[Itimer.lc]{Interval Timer}
7 %*                                                                      *
8 %************************************************************************
9
10 The interval timer is used for profiling and for context switching in the
11 threaded build.  Though POSIX 1003.4 includes a standard interface for
12 such things, no one really seems to be implementing them yet.  Even 
13 Solaris 2.3 only seems to provide support for @CLOCK_REAL@, whereas we're
14 keen on getting access to @CLOCK_VIRTUAL@.
15
16 Hence, we use the old-fashioned @setitimer@ that just about everyone seems
17 to support.  So much for standards.
18
19 \begin{code}
20
21 #if defined(USE_COST_CENTRES) || defined(CONCURRENT)
22
23 # include "platform.h"
24
25 # define NON_POSIX_SOURCE
26
27 # include "rtsdefs.h"
28
29 /* As recommended in the autoconf manual */
30 # ifdef TIME_WITH_SYS_TIME
31 #  include <sys/time.h>
32 #  include <time.h>
33 # else
34 #  ifdef HAVE_SYS_TIME_H
35 #   include <sys/time.h>
36 #  else
37 #   include <time.h>
38 #  endif
39 # endif
40
41 int
42 initialize_virtual_timer(ms)
43 int ms;
44 {
45 # ifndef HAVE_SETITIMER
46     fprintf(stderr, "No virtual timer on this system\n");
47     return -1;
48 # else
49     struct itimerval it;
50
51     it.it_value.tv_sec = ms / 1000;
52     it.it_value.tv_usec = 1000 * (ms - (1000 * it.it_value.tv_sec));
53     it.it_interval = it.it_value;
54     return (setitimer(ITIMER_VIRTUAL, &it, NULL));
55 # endif
56 }
57
58 # if 0
59 /* This is a potential POSIX version */
60 int
61 initialize_virtual_timer(ms)
62 int ms;
63 {
64     struct sigevent se;
65     struct itimerspec it;
66     timer_t tid;
67
68     se.sigev_notify = SIGEV_SIGNAL;
69     se.sigev_signo = SIGVTALRM;
70     se.sigev_value.sival_int = SIGVTALRM;
71     if (timer_create(CLOCK_VIRTUAL, &se, &tid)) {
72         fprintf(stderr, "Can't create virtual timer.\n");
73         EXIT(EXIT_FAILURE);
74     }
75     it.it_value.tv_sec = ms / 1000;
76     it.it_value.tv_nsec = 1000000 * (ms - 1000 * it.it_value.tv_sec);
77     it.it_interval = it.it_value;
78     timer_settime(tid, TIMER_RELTIME, &it, NULL);
79 }
80 # endif
81
82 #endif  /* USE_COST_CENTRES || CONCURRENT */
83
84 \end{code}