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"
24 #include "OSThreads.h"
26 /* ticks left before next pre-emptive context switch */
27 static int ticks_to_ctxt_switch = 0;
29 #if defined(THREADED_RTS)
30 /* idle ticks left before we perform a GC */
31 static int ticks_to_gc = 0;
34 #if defined(THREADED_RTS)
35 static void OSThreadProcAttr proddingThread(void *p);
39 * Function: handle_tick()
41 * At each occurrence of a tick, the OS timer will invoke
46 handle_tick(int unused STG_UNUSED)
51 if (RtsFlags.ConcFlags.ctxtSwitchTicks > 0) {
52 ticks_to_ctxt_switch--;
53 if (ticks_to_ctxt_switch <= 0) {
54 ticks_to_ctxt_switch = RtsFlags.ConcFlags.ctxtSwitchTicks;
55 context_switch = 1; /* schedule a context switch */
57 #if defined(THREADED_RTS)
59 * If we've been inactive for idleGCDelayTicks (set by +RTS
60 * -I), tell the scheduler to wake up and do a GC, to check
61 * for threads that are deadlocked.
63 switch (recent_activity) {
65 recent_activity = ACTIVITY_MAYBE_NO;
66 ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTicks;
68 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 /* We can't prod the Capability from inside the
79 * signal handler, because pthread_cond_signal()
80 * doesn't work from signal handlers. Let's hope
81 * that pthread_create() works:
83 createOSThread(&id, proddingThread, NULL);
95 #if defined(THREADED_RTS)
96 static void OSThreadProcAttr
97 proddingThread(void *p STG_UNUSED)
111 return startTicker(ms, handle_tick);