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