Split GC.c, and move storage manager into sm/ directory
[ghc.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 "Storage.h"
21 #include "Schedule.h"
22 #include "Timer.h"
23 #include "Ticker.h"
24 #include "Capability.h"
25 #include "RtsSignals.h"
26
27 /* ticks left before next pre-emptive context switch */
28 static int ticks_to_ctxt_switch = 0;
29
30 #if defined(THREADED_RTS)
31 /* idle ticks left before we perform a GC */
32 static int ticks_to_gc = 0;
33 #endif
34
35 /*
36 * Function: handle_tick()
37 *
38 * At each occurrence of a tick, the OS timer will invoke
39 * handle_tick().
40 */
41 static
42 void
43 handle_tick(int unused STG_UNUSED)
44 {
45 #ifdef PROFILING
46 handleProfTick();
47 #endif
48 if (RtsFlags.ConcFlags.ctxtSwitchTicks > 0) {
49 ticks_to_ctxt_switch--;
50 if (ticks_to_ctxt_switch <= 0) {
51 ticks_to_ctxt_switch = RtsFlags.ConcFlags.ctxtSwitchTicks;
52 context_switch = 1; /* schedule a context switch */
53 }
54 }
55
56 #if defined(THREADED_RTS)
57 /*
58 * If we've been inactive for idleGCDelayTime (set by +RTS
59 * -I), tell the scheduler to wake up and do a GC, to check
60 * for threads that are deadlocked.
61 */
62 switch (recent_activity) {
63 case ACTIVITY_YES:
64 recent_activity = ACTIVITY_MAYBE_NO;
65 ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTime /
66 RtsFlags.MiscFlags.tickInterval;
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.idleGCDelayTime /
73 RtsFlags.MiscFlags.tickInterval;
74 recent_activity = ACTIVITY_INACTIVE;
75 blackholes_need_checking = rtsTrue;
76 /* hack: re-use the blackholes_need_checking flag */
77 wakeUpRts();
78 }
79 break;
80 default:
81 break;
82 }
83 #endif
84 }
85
86 int
87 startTimer(void)
88 {
89 #ifdef PROFILING
90 initProfTimer();
91 #endif
92
93 return startTicker(RtsFlags.MiscFlags.tickInterval, handle_tick);
94 }
95
96 int
97 stopTimer(void)
98 {
99 return stopTicker();
100 }