markSignalHandlers(): implementation was unnecessary, and had a bug
[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 #if !defined(mingw32_HOST_OS)
76           // This forces the IO Manager thread to wakeup, which will
77           // in turn ensure that some OS thread wakes up and runs the
78           // scheduler loop, which will cause a GC and deadlock check.
79           ioManagerWakeup();
80 #else
81           /* ToDo: this doesn't work.  Can't invoke
82            * pthread_cond_signal from a signal handler.
83            * Furthermore, we can't prod a capability that we
84            * might be holding.  What can we do?
85            */
86           prodOneCapability();
87 #endif
88       }
89       break;
90   default:
91       break;
92   }
93 #endif
94 }
95
96 int
97 startTimer(nat ms)
98 {
99 #ifdef PROFILING
100   initProfTimer();
101 #endif
102
103   return startTicker(ms, handle_tick);
104 }
105
106 int
107 stopTimer()
108 {
109   return stopTicker();
110 }