Move the context_switch flag into the Capability
[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 handleProfTick();
46 if (RtsFlags.ConcFlags.ctxtSwitchTicks > 0) {
47 ticks_to_ctxt_switch--;
48 if (ticks_to_ctxt_switch <= 0) {
49 ticks_to_ctxt_switch = RtsFlags.ConcFlags.ctxtSwitchTicks;
50 setContextSwitches(); /* schedule a context switch */
51 }
52 }
53
54 #if defined(THREADED_RTS)
55 /*
56 * If we've been inactive for idleGCDelayTime (set by +RTS
57 * -I), tell the scheduler to wake up and do a GC, to check
58 * for threads that are deadlocked.
59 */
60 switch (recent_activity) {
61 case ACTIVITY_YES:
62 recent_activity = ACTIVITY_MAYBE_NO;
63 ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTime /
64 RtsFlags.MiscFlags.tickInterval;
65 break;
66 case ACTIVITY_MAYBE_NO:
67 if (ticks_to_gc == 0) {
68 /* 0 ==> no idle GC */
69 recent_activity = ACTIVITY_DONE_GC;
70 // disable timer signals (see #1623)
71 stopTimer();
72 } else {
73 ticks_to_gc--;
74 if (ticks_to_gc == 0) {
75 ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTime /
76 RtsFlags.MiscFlags.tickInterval;
77 recent_activity = ACTIVITY_INACTIVE;
78 blackholes_need_checking = rtsTrue;
79 /* hack: re-use the blackholes_need_checking flag */
80 wakeUpRts();
81 }
82 }
83 break;
84 default:
85 break;
86 }
87 #endif
88 }
89
90 void
91 initTimer(void)
92 {
93 initProfTimer();
94 if (RtsFlags.MiscFlags.tickInterval != 0) {
95 initTicker(RtsFlags.MiscFlags.tickInterval, handle_tick);
96 }
97 }
98
99 void
100 startTimer(void)
101 {
102 if (RtsFlags.MiscFlags.tickInterval != 0) {
103 startTicker();
104 }
105 }
106
107 void
108 stopTimer(void)
109 {
110 if (RtsFlags.MiscFlags.tickInterval != 0) {
111 stopTicker();
112 }
113 }
114
115 void
116 exitTimer(void)
117 {
118 if (RtsFlags.MiscFlags.tickInterval != 0) {
119 exitTicker();
120 }
121 }