RTS tidyup sweep, first phase
[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
18 #include "PosixSource.h"
19 #include "Rts.h"
20
21 #include "Timer.h"
22 #include "Proftimer.h"
23 #include "Schedule.h"
24 #include "Ticker.h"
25 #include "Capability.h"
26 #include "RtsSignals.h"
27
28 /* ticks left before next pre-emptive context switch */
29 static int ticks_to_ctxt_switch = 0;
30
31 #if defined(THREADED_RTS)
32 /* idle ticks left before we perform a GC */
33 static int ticks_to_gc = 0;
34 #endif
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   handleProfTick();
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           setContextSwitches(); /* schedule a context switch */
52       }
53   }
54
55 #if defined(THREADED_RTS)
56   /* 
57    * If we've been inactive for idleGCDelayTime (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.idleGCDelayTime /
65                     RtsFlags.MiscFlags.tickInterval;
66       break;
67   case ACTIVITY_MAYBE_NO:
68       if (ticks_to_gc == 0) {
69           /* 0 ==> no idle GC */
70           recent_activity = ACTIVITY_DONE_GC;
71           // disable timer signals (see #1623)
72           stopTimer();
73       } else {
74           ticks_to_gc--;
75           if (ticks_to_gc == 0) {
76               ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTime /
77                   RtsFlags.MiscFlags.tickInterval;
78               recent_activity = ACTIVITY_INACTIVE;
79               blackholes_need_checking = rtsTrue;
80               /* hack: re-use the blackholes_need_checking flag */
81               wakeUpRts();
82           }
83       }
84       break;
85   default:
86       break;
87   }
88 #endif
89 }
90
91 void
92 initTimer(void)
93 {
94     initProfTimer();
95     if (RtsFlags.MiscFlags.tickInterval != 0) {
96         initTicker(RtsFlags.MiscFlags.tickInterval, handle_tick);
97     }
98 }
99
100 void
101 startTimer(void)
102 {
103     if (RtsFlags.MiscFlags.tickInterval != 0) {
104         startTicker();
105     }
106 }
107
108 void
109 stopTimer(void)
110 {
111     if (RtsFlags.MiscFlags.tickInterval != 0) {
112         stopTicker();
113     }
114 }
115
116 void
117 exitTimer(void)
118 {
119     if (RtsFlags.MiscFlags.tickInterval != 0) {
120         exitTicker();
121     }
122 }