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.
19 #include "Proftimer.h"
23 #include "Capability.h"
25 /* ticks left before next pre-emptive context switch */
26 static int ticks_to_ctxt_switch = 0;
28 #if defined(THREADED_RTS)
29 /* idle ticks left before we perform a GC */
30 static int ticks_to_gc = 0;
34 * Function: handle_tick()
36 * At each occurrence of a tick, the OS timer will invoke
41 handle_tick(int unused STG_UNUSED)
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 context_switch = 1; /* schedule a context switch */
52 #if defined(THREADED_RTS)
54 * If we've been inactive for idleGCDelayTicks (set by +RTS
55 * -I), tell the scheduler to wake up and do a GC, to check
56 * for threads that are deadlocked.
58 switch (recent_activity) {
60 recent_activity = ACTIVITY_MAYBE_NO;
61 ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTicks;
63 case ACTIVITY_MAYBE_NO:
64 if (ticks_to_gc == 0) break; /* 0 ==> no idle GC */
66 if (ticks_to_gc == 0) {
67 ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTicks;
68 recent_activity = ACTIVITY_INACTIVE;
69 blackholes_need_checking = rtsTrue;
70 /* hack: re-use the blackholes_need_checking flag */
72 /* ToDo: this doesn't work. Can't invoke
73 * pthread_cond_signal from a signal handler.
74 * Furthermore, we can't prod a capability that we
75 * might be holding. What can we do?
95 return startTicker(ms, handle_tick);