1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1995-2005
5 * Interval timer service for profiling and pre-emptive scheduling.
7 * ---------------------------------------------------------------------------*/
10 * The interval timer is used for profiling and for context switching in the
13 * This file defines the platform-independent view of interval timing, relying
14 * on platform-specific services to install and run the timers.
18 #include "PosixSource.h"
22 #include "Proftimer.h"
25 #include "Capability.h"
26 #include "RtsSignals.h"
28 /* ticks left before next pre-emptive context switch */
29 static int ticks_to_ctxt_switch = 0;
31 #if defined(THREADED_RTS)
32 /* idle ticks left before we perform a GC */
33 static int ticks_to_gc = 0;
37 * Function: handle_tick()
39 * At each occurrence of a tick, the OS timer will invoke
44 handle_tick(int unused STG_UNUSED)
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 */
55 #if defined(THREADED_RTS)
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.
61 switch (recent_activity) {
63 recent_activity = ACTIVITY_MAYBE_NO;
64 ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTime /
65 RtsFlags.MiscFlags.tickInterval;
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)
75 if (ticks_to_gc == 0) {
76 ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTime /
77 RtsFlags.MiscFlags.tickInterval;
78 recent_activity = ACTIVITY_INACTIVE;
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.
96 static StgWord timer_disabled;
102 if (RtsFlags.MiscFlags.tickInterval != 0) {
103 initTicker(RtsFlags.MiscFlags.tickInterval, handle_tick);
111 if (atomic_dec(&timer_disabled) == 0) {
112 if (RtsFlags.MiscFlags.tickInterval != 0) {
121 if (atomic_inc(&timer_disabled) == 1) {
122 if (RtsFlags.MiscFlags.tickInterval != 0) {
129 exitTimer (rtsBool wait)
131 if (RtsFlags.MiscFlags.tickInterval != 0) {