2 % (c) The AQUA Project, Glasgow University, 1995
4 %************************************************************************
6 \section[Itimer.lc]{Interval Timer}
8 %************************************************************************
10 The interval timer is used for profiling and for context switching in the
11 threaded build. Though POSIX 1003.1b 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@.
16 Hence, we use the old-fashioned @setitimer@ that just about everyone seems
17 to support. So much for standards.
21 #if defined(PROFILING) || defined(CONCURRENT)
23 /* OLD: # include "platform.h" */
28 # define NON_POSIX_SOURCE
33 /* As recommended in the autoconf manual */
34 # ifdef TIME_WITH_SYS_TIME
35 # include <sys/time.h>
38 # ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
46 Handling timer events under cygwin32 is not done with signal/setitimer.
47 Instead of the two steps of first registering a signal handler to handle
48 \tr{SIGVTALRM} and then start generating them via @setitimer()@, we use
49 the Multimedia API (MM) and its @timeSetEvent@. (Internally, the MM API
50 creates a separate thread that will notify the main thread of timer
54 #if defined(cygwin32_TARGET_OS)
56 #include <windows.h> /* OK, bring it all in... */
59 vtalrm_handler is assigned and set up in
62 vtalrm_id (defined in main/Signals.lc) holds
63 the system id for the current timer (used to
64 later block/kill the timer)
67 extern TIMECALLBACK *vtalrm_cback;
70 initialize_virtual_timer(ms)
73 /* VTALRM is currently not supported by cygwin32,
74 so we use the Timer support provided by the
75 MultiMedia API that is part of Win32. The
76 parameters to timeSetEvent may require some tweaking.
78 unsigned int delay,vtalrm_id;
80 delay = timeBeginPeriod(1);
81 if (delay == TIMERR_NOCANDO) { /* error of some sort. */
85 timeSetEvent(ms, /* event every `delay' milliseconds. */
86 1, /* precision is within 5 millisecs. */
87 (LPTIMECALLBACK)vtalrm_cback,
96 initialize_virtual_timer(ms)
99 # ifndef HAVE_SETITIMER
100 fprintf(stderr, "No virtual timer on this system\n");
105 it.it_value.tv_sec = ms / 1000;
106 it.it_value.tv_usec = 1000 * (ms - (1000 * it.it_value.tv_sec));
107 it.it_interval = it.it_value;
108 return (setitimer(ITIMER_VIRTUAL, &it, NULL));
112 #endif /* !cygwin32_TARGET_OS */
115 /* This is a potential POSIX version */
117 initialize_virtual_timer(ms)
121 struct itimerspec it;
124 se.sigev_notify = SIGEV_SIGNAL;
125 se.sigev_signo = SIGVTALRM;
126 se.sigev_value.sival_int = SIGVTALRM;
127 if (timer_create(CLOCK_VIRTUAL, &se, &tid)) {
128 fprintf(stderr, "Can't create virtual timer.\n");
131 it.it_value.tv_sec = ms / 1000;
132 it.it_value.tv_nsec = 1000000 * (ms - 1000 * it.it_value.tv_sec);
133 it.it_interval = it.it_value;
134 timer_settime(tid, TIMER_RELTIME, &it, NULL);
138 #endif /* PROFILING || CONCURRENT */