[project @ 2005-05-16 14:27:07 by simonmar]
[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 #include "Capability.h"
23
24 #if !defined(mingw32_HOST_OS)
25 #include "Itimer.h"
26 #else
27 #include "win32/Ticker.h"
28 #endif
29
30 /* ticks left before next pre-emptive context switch */
31 static int ticks_to_ctxt_switch = 0;
32
33 /* idle ticks left before we perform a GC */
34 static int ticks_to_gc = 0;
35
36 /*
37  * Function: handle_tick()
38  *
39  * At each occurrence of a tick, the OS timer will invoke
40  * handle_tick().
41  */
42 static
43 void
44 handle_tick(int unused STG_UNUSED)
45 {
46 #ifdef PROFILING
47   handleProfTick();
48 #endif
49   if (RtsFlags.ConcFlags.ctxtSwitchTicks > 0) {
50       ticks_to_ctxt_switch--;
51       if (ticks_to_ctxt_switch <= 0) {
52           ticks_to_ctxt_switch = RtsFlags.ConcFlags.ctxtSwitchTicks;
53           context_switch = 1;   /* schedule a context switch */
54
55 #if defined(RTS_SUPPORTS_THREADS)
56           /* 
57            * If we've been inactive for idleGCDelayTicks (set by +RTS
58            * -I), tell the scheduler to wake up and do a GC, to check
59            * for threads that are deadlocked.
60            */
61           switch (recent_activity) {
62           case ACTIVITY_YES:
63               recent_activity = ACTIVITY_MAYBE_NO;
64               ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTicks;
65               break;
66           case ACTIVITY_MAYBE_NO:
67               if (ticks_to_gc == 0) break; /* 0 ==> no idle GC */
68               ticks_to_gc--;
69               if (ticks_to_gc == 0) {
70                   ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTicks;
71                   recent_activity = ACTIVITY_INACTIVE;
72                   blackholes_need_checking = rtsTrue;
73                   /* hack: re-use the blackholes_need_checking flag */
74                   threadRunnable();
75                   /* ToDo: this threadRunnable only works if there's
76                    * another thread (not this one) waiting to be woken up
77                    */
78               }
79               break;
80           default:
81               break;
82           }
83 #endif
84       }
85   }
86 }
87
88 int
89 startTimer(nat ms)
90 {
91 #ifdef PROFILING
92   initProfTimer();
93 #endif
94
95   return startTicker(ms, handle_tick);
96 }
97
98 int
99 stopTimer()
100 {
101   return stopTicker();
102 }