[project @ 2005-10-21 14:47:36 by simonmar]
[ghc-hetmet.git] / ghc / rts / Timer.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1995-2005
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 "Ticker.h"
23 #include "Capability.h"
24
25 /* ticks left before next pre-emptive context switch */
26 static int ticks_to_ctxt_switch = 0;
27
28 #if defined(THREADED_RTS)
29 /* idle ticks left before we perform a GC */
30 static int ticks_to_gc = 0;
31 #endif
32
33 /*
34  * Function: handle_tick()
35  *
36  * At each occurrence of a tick, the OS timer will invoke
37  * handle_tick().
38  */
39 static
40 void
41 handle_tick(int unused STG_UNUSED)
42 {
43 #ifdef PROFILING
44   handleProfTick();
45 #endif
46   if (RtsFlags.ConcFlags.ctxtSwitchTicks > 0) {
47       ticks_to_ctxt_switch--;
48       if (ticks_to_ctxt_switch <= 0) {
49           ticks_to_ctxt_switch = RtsFlags.ConcFlags.ctxtSwitchTicks;
50           context_switch = 1;   /* schedule a context switch */
51
52 #if defined(THREADED_RTS)
53           /* 
54            * If we've been inactive for idleGCDelayTicks (set by +RTS
55            * -I), tell the scheduler to wake up and do a GC, to check
56            * for threads that are deadlocked.
57            */
58           switch (recent_activity) {
59           case ACTIVITY_YES:
60               recent_activity = ACTIVITY_MAYBE_NO;
61               ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTicks;
62               break;
63           case ACTIVITY_MAYBE_NO:
64               if (ticks_to_gc == 0) break; /* 0 ==> no idle GC */
65               ticks_to_gc--;
66               if (ticks_to_gc == 0) {
67                   ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTicks;
68                   recent_activity = ACTIVITY_INACTIVE;
69                   blackholes_need_checking = rtsTrue;
70                   /* hack: re-use the blackholes_need_checking flag */
71
72                   /* ToDo: this doesn't work.  Can't invoke
73                    * pthread_cond_signal from a signal handler.
74                    * Furthermore, we can't prod a capability that we
75                    * might be holding.  What can we do?
76                    */
77                   prodOneCapability();
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 }