New implementation of BLACKHOLEs
[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 void
90 initTimer(void)
91 {
92 initProfTimer();
93 if (RtsFlags.MiscFlags.tickInterval != 0) {
94 initTicker(RtsFlags.MiscFlags.tickInterval, handle_tick);
95 }
96 }
97
98 void
99 startTimer(void)
100 {
101 if (RtsFlags.MiscFlags.tickInterval != 0) {
102 startTicker();
103 }
104 }
105
106 void
107 stopTimer(void)
108 {
109 if (RtsFlags.MiscFlags.tickInterval != 0) {
110 stopTicker();
111 }
112 }
113
114 void
115 exitTimer (rtsBool wait)
116 {
117 if (RtsFlags.MiscFlags.tickInterval != 0) {
118 exitTicker(wait);
119 }
120 }