fix possible ^C problems
[ghc-hetmet.git] / 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 #include "RtsSignals.h"
25
26 /* ticks left before next pre-emptive context switch */
27 static int ticks_to_ctxt_switch = 0;
28
29 #if defined(THREADED_RTS)
30 /* idle ticks left before we perform a GC */
31 static int ticks_to_gc = 0;
32 #endif
33
34 /*
35  * Function: handle_tick()
36  *
37  * At each occurrence of a tick, the OS timer will invoke
38  * handle_tick().
39  */
40 static
41 void
42 handle_tick(int unused STG_UNUSED)
43 {
44 #ifdef PROFILING
45   handleProfTick();
46 #endif
47   if (RtsFlags.ConcFlags.ctxtSwitchTicks > 0) {
48       ticks_to_ctxt_switch--;
49       if (ticks_to_ctxt_switch <= 0) {
50           ticks_to_ctxt_switch = RtsFlags.ConcFlags.ctxtSwitchTicks;
51           context_switch = 1;   /* schedule a context switch */
52       }
53   }
54
55 #if defined(THREADED_RTS)
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           
75       }
76       wakeUpRts();
77       break;
78   default:
79       break;
80   }
81 #endif
82 }
83
84 int
85 startTimer(nat ms)
86 {
87 #ifdef PROFILING
88   initProfTimer();
89 #endif
90
91   return startTicker(ms, handle_tick);
92 }
93
94 int
95 stopTimer()
96 {
97   return stopTicker();
98 }