Strings and comments only: 'to to ' fixes
[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 /* idle ticks left before we perform a GC */
32 static int ticks_to_gc = 0;
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 handleProfTick();
45 if (RtsFlags.ConcFlags.ctxtSwitchTicks > 0) {
46 ticks_to_ctxt_switch--;
47 if (ticks_to_ctxt_switch <= 0) {
48 ticks_to_ctxt_switch = RtsFlags.ConcFlags.ctxtSwitchTicks;
49 contextSwitchAllCapabilities(); /* schedule a context switch */
50 }
51 }
52
53 /*
54 * If we've been inactive for idleGCDelayTime (set by +RTS
55 * -I), tell the scheduler to wake up and do a GC, to check
56 * for threads that are deadlocked.
57 */
58 switch (recent_activity) {
59 case ACTIVITY_YES:
60 recent_activity = ACTIVITY_MAYBE_NO;
61 ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTime /
62 RtsFlags.MiscFlags.tickInterval;
63 break;
64 case ACTIVITY_MAYBE_NO:
65 if (ticks_to_gc == 0) {
66 if (RtsFlags.GcFlags.doIdleGC) {
67 recent_activity = ACTIVITY_INACTIVE;
68 #ifdef THREADED_RTS
69 wakeUpRts();
70 // The scheduler will call stopTimer() when it has done
71 // the GC.
72 #endif
73 } else {
74 recent_activity = ACTIVITY_DONE_GC;
75 // disable timer signals (see #1623, #5991)
76 // but only if we're not profiling
77 #ifndef PROFILING
78 stopTimer();
79 #endif
80 }
81 } else {
82 ticks_to_gc--;
83 }
84 break;
85 default:
86 break;
87 }
88 }
89
90 // This global counter is used to allow multiple threads to stop the
91 // timer temporarily with a stopTimer()/startTimer() pair. If
92 // timer_enabled == 0 timer is enabled
93 // timer_disabled == N, N > 0 timer is disabled by N threads
94 // When timer_enabled makes a transition to 0, we enable the timer,
95 // and when it makes a transition to non-0 we disable it.
96
97 static StgWord timer_disabled;
98
99 void
100 initTimer(void)
101 {
102 initProfTimer();
103 if (RtsFlags.MiscFlags.tickInterval != 0) {
104 initTicker(RtsFlags.MiscFlags.tickInterval, handle_tick);
105 }
106 timer_disabled = 1;
107 }
108
109 void
110 startTimer(void)
111 {
112 if (atomic_dec(&timer_disabled) == 0) {
113 if (RtsFlags.MiscFlags.tickInterval != 0) {
114 startTicker();
115 }
116 }
117 }
118
119 void
120 stopTimer(void)
121 {
122 if (atomic_inc(&timer_disabled) == 1) {
123 if (RtsFlags.MiscFlags.tickInterval != 0) {
124 stopTicker();
125 }
126 }
127 }
128
129 void
130 exitTimer (rtsBool wait)
131 {
132 if (RtsFlags.MiscFlags.tickInterval != 0) {
133 exitTicker(wait);
134 }
135 }