remove empty dir
[ghc-hetmet.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 #include "Rts.h"
18 #include "RtsFlags.h"
19 #include "Proftimer.h"
20 #include "Schedule.h"
21 #include "Timer.h"
22 #include "Ticker.h"
23 #include "Capability.h"
24
25 /* ticks left before next pre-emptive context switch */
26 static int ticks_to_ctxt_switch = 0;
27
28 #if defined(THREADED_RTS)
29 /* idle ticks left before we perform a GC */
30 static int ticks_to_gc = 0;
31 #endif
32
33 /*
34  * Function: handle_tick()
35  *
36  * At each occurrence of a tick, the OS timer will invoke
37  * handle_tick().
38  */
39 static
40 void
41 handle_tick(int unused STG_UNUSED)
42 {
43 #ifdef PROFILING
44   handleProfTick();
45 #endif
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 */
51       }
52   }
53
54 #if defined(THREADED_RTS)
55   /* 
56    * If we've been inactive for idleGCDelayTicks (set by +RTS
57    * -I), tell the scheduler to wake up and do a GC, to check
58    * for threads that are deadlocked.
59    */
60   switch (recent_activity) {
61   case ACTIVITY_YES:
62       recent_activity = ACTIVITY_MAYBE_NO;
63       ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTicks;
64       break;
65   case ACTIVITY_MAYBE_NO:
66       if (ticks_to_gc == 0) break; /* 0 ==> no idle GC */
67       ticks_to_gc--;
68       if (ticks_to_gc == 0) {
69           ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTicks;
70           recent_activity = ACTIVITY_INACTIVE;
71           blackholes_need_checking = rtsTrue;
72           /* hack: re-use the blackholes_need_checking flag */
73           
74           /* ToDo: this doesn't work.  Can't invoke
75            * pthread_cond_signal from a signal handler.
76            * Furthermore, we can't prod a capability that we
77            * might be holding.  What can we do?
78            */
79           prodOneCapability();
80       }
81       break;
82   default:
83       break;
84   }
85 #endif
86 }
87
88 int
89 startTimer(nat ms)
90 {
91 #ifdef PROFILING
92   initProfTimer();
93 #endif
94
95   return startTicker(ms, handle_tick);
96 }
97
98 int
99 stopTimer()
100 {
101   return stopTicker();
102 }