[project @ 2004-02-12 02:04:59 by mthomas]
[ghc-hetmet.git] / ghc / rts / Timer.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1995-2003
4  *
5  * Interval timer service for profiling and pre-emptive scheduling.
6  *
7  * ---------------------------------------------------------------------------*/
8
9 /*
10  * The interval timer is used for profiling and for context switching in the
11  * threaded build. 
12  *
13  * This file defines the platform-independent view of interval timing, relying
14  * on platform-specific services to install and run the timers.
15  *
16  */
17 #include "Rts.h"
18 #include "RtsFlags.h"
19 #include "Proftimer.h"
20 #include "Schedule.h"
21 #include "Timer.h"
22
23 #if !defined(mingw32_TARGET_OS)
24 #include "Itimer.h"
25 #else
26 #include "win32/Ticker.h"
27 #endif
28
29 /* ticks left before next pre-emptive context switch */
30 static int ticks_to_ctxt_switch = 0;
31
32 /*
33  * Function: handle_tick()
34  *
35  * At each occurrence of a tick, the OS timer will invoke
36  * handle_tick().
37  */
38 static
39 void
40 handle_tick(int unused STG_UNUSED)
41 {
42 #ifdef PROFILING
43   handleProfTick();
44 #endif
45   if (RtsFlags.ConcFlags.ctxtSwitchTicks > 0) {
46       ticks_to_ctxt_switch--;
47       if (ticks_to_ctxt_switch <= 0) {
48           ticks_to_ctxt_switch = RtsFlags.ConcFlags.ctxtSwitchTicks;
49           context_switch = 1;   /* schedule a context switch */
50       }
51   }
52 }
53
54 int
55 startTimer(nat ms)
56 {
57 #ifdef PROFILING
58   initProfTimer();
59 #endif
60
61   return startTicker(ms, handle_tick);
62 }
63
64 int
65 stopTimer()
66 {
67   return stopTicker();
68 }