[project @ 2005-05-21 16:09:18 by panne]
[ghc-hetmet.git] / ghc / rts / Timer.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1995-2003
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 "Capability.h"
23
24 #if !defined(mingw32_HOST_OS)
25 #include "Itimer.h"
26 #else
27 #include "win32/Ticker.h"
28 #endif
29
30 /* ticks left before next pre-emptive context switch */
31 static int ticks_to_ctxt_switch = 0;
32
33 #if defined(RTS_SUPPORTS_THREADS)
34 /* idle ticks left before we perform a GC */
35 static int ticks_to_gc = 0;
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(RTS_SUPPORTS_THREADS)
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               if (ticks_to_gc == 0) break; /* 0 ==> no idle GC */
70               ticks_to_gc--;
71               if (ticks_to_gc == 0) {
72                   ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTicks;
73                   recent_activity = ACTIVITY_INACTIVE;
74                   blackholes_need_checking = rtsTrue;
75                   /* hack: re-use the blackholes_need_checking flag */
76                   threadRunnable();
77                   /* ToDo: this threadRunnable only works if there's
78                    * another thread (not this one) waiting to be woken up
79                    */
80               }
81               break;
82           default:
83               break;
84           }
85 #endif
86       }
87   }
88 }
89
90 int
91 startTimer(nat ms)
92 {
93 #ifdef PROFILING
94   initProfTimer();
95 #endif
96
97   return startTicker(ms, handle_tick);
98 }
99
100 int
101 stopTimer()
102 {
103   return stopTicker();
104 }