improvement to the deadlock detection
[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 #include "OSThreads.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 #if defined(THREADED_RTS)
35 static void OSThreadProcAttr proddingThread(void *p);
36 #endif
37
38 /*
39  * Function: handle_tick()
40  *
41  * At each occurrence of a tick, the OS timer will invoke
42  * handle_tick().
43  */
44 static
45 void
46 handle_tick(int unused STG_UNUSED)
47 {
48 #ifdef PROFILING
49   handleProfTick();
50 #endif
51   if (RtsFlags.ConcFlags.ctxtSwitchTicks > 0) {
52       ticks_to_ctxt_switch--;
53       if (ticks_to_ctxt_switch <= 0) {
54           ticks_to_ctxt_switch = RtsFlags.ConcFlags.ctxtSwitchTicks;
55           context_switch = 1;   /* schedule a context switch */
56
57 #if defined(THREADED_RTS)
58           /* 
59            * If we've been inactive for idleGCDelayTicks (set by +RTS
60            * -I), tell the scheduler to wake up and do a GC, to check
61            * for threads that are deadlocked.
62            */
63           switch (recent_activity) {
64           case ACTIVITY_YES:
65               recent_activity = ACTIVITY_MAYBE_NO;
66               ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTicks;
67               break;
68           case ACTIVITY_MAYBE_NO: {
69               OSThreadId id;
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                   /* We can't prod the Capability from inside the
79                    * signal handler, because pthread_cond_signal()
80                    * doesn't work from signal handlers.  Let's hope
81                    * that pthread_create() works:
82                    */
83                   createOSThread(&id, proddingThread, NULL);
84               }
85               break;
86           }
87           default:
88               break;
89           }
90 #endif
91       }
92   }
93 }
94
95 #if defined(THREADED_RTS)
96 static void OSThreadProcAttr
97 proddingThread(void *p STG_UNUSED)
98 {
99     prodOneCapability();
100     // and exit again.
101 }
102 #endif
103
104 int
105 startTimer(nat ms)
106 {
107 #ifdef PROFILING
108   initProfTimer();
109 #endif
110
111   return startTicker(ms, handle_tick);
112 }
113
114 int
115 stopTimer()
116 {
117   return stopTicker();
118 }