Fix #4074 (I hope).
[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
18 #include "PosixSource.h"
19 #include "Rts.h"
20
21 #include "Timer.h"
22 #include "Proftimer.h"
23 #include "Schedule.h"
24 #include "Ticker.h"
25 #include "Capability.h"
26 #include "RtsSignals.h"
27
28 /* ticks left before next pre-emptive context switch */
29 static int ticks_to_ctxt_switch = 0;
30
31 #if defined(THREADED_RTS)
32 /* idle ticks left before we perform a GC */
33 static int ticks_to_gc = 0;
34 #endif
35
36 /*
37 * Function: handle_tick()
38 *
39 * At each occurrence of a tick, the OS timer will invoke
40 * handle_tick().
41 */
42 static
43 void
44 handle_tick(int unused STG_UNUSED)
45 {
46 handleProfTick();
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 setContextSwitches(); /* schedule a context switch */
52 }
53 }
54
55 #if defined(THREADED_RTS)
56 /*
57 * If we've been inactive for idleGCDelayTime (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.idleGCDelayTime /
65 RtsFlags.MiscFlags.tickInterval;
66 break;
67 case ACTIVITY_MAYBE_NO:
68 if (ticks_to_gc == 0) {
69 /* 0 ==> no idle GC */
70 recent_activity = ACTIVITY_DONE_GC;
71 // disable timer signals (see #1623)
72 stopTimer();
73 } else {
74 ticks_to_gc--;
75 if (ticks_to_gc == 0) {
76 ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTime /
77 RtsFlags.MiscFlags.tickInterval;
78 recent_activity = ACTIVITY_INACTIVE;
79 wakeUpRts();
80 }
81 }
82 break;
83 default:
84 break;
85 }
86 #endif
87 }
88
89 // This global counter is used to allow multiple threads to stop the
90 // timer temporarily with a stopTimer()/startTimer() pair. If
91 // timer_enabled == 0 timer is enabled
92 // timer_disabled == N, N > 0 timer is disabled by N threads
93 // When timer_enabled makes a transition to 0, we enable the timer,
94 // and when it makes a transition to non-0 we disable it.
95
96 static StgWord timer_disabled;
97
98 void
99 initTimer(void)
100 {
101 initProfTimer();
102 if (RtsFlags.MiscFlags.tickInterval != 0) {
103 initTicker(RtsFlags.MiscFlags.tickInterval, handle_tick);
104 }
105 timer_disabled = 1;
106 }
107
108 void
109 startTimer(void)
110 {
111 if (atomic_dec(&timer_disabled) == 0) {
112 if (RtsFlags.MiscFlags.tickInterval != 0) {
113 startTicker();
114 }
115 }
116 }
117
118 void
119 stopTimer(void)
120 {
121 if (atomic_inc(&timer_disabled) == 1) {
122 if (RtsFlags.MiscFlags.tickInterval != 0) {
123 stopTicker();
124 }
125 }
126 }
127
128 void
129 exitTimer (rtsBool wait)
130 {
131 if (RtsFlags.MiscFlags.tickInterval != 0) {
132 exitTicker(wait);
133 }
134 }