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