[project @ 2005-04-25 15:36:28 by simonmar]
[ghc-hetmet.git] / ghc / rts / Timer.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1995-2003
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 "Capability.h"
23
24 #if !defined(mingw32_HOST_OS)
25 #include "Itimer.h"
26 #else
27 #include "win32/Ticker.h"
28 #endif
29
30 /* ticks left before next pre-emptive context switch */
31 static int ticks_to_ctxt_switch = 0;
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 #if defined(RTS_SUPPORTS_THREADS)
53           /* 
54            * If we've been inactive for a whole time slice, tell the
55            * scheduler to wake up and do a GC, to check for threads
56            * that are deadlocked.
57            */
58           switch (recent_activity) {
59           case ACTIVITY_YES:
60               recent_activity = ACTIVITY_MAYBE_NO;
61               break;
62           case ACTIVITY_MAYBE_NO:
63               recent_activity = ACTIVITY_INACTIVE;
64               blackholes_need_checking = rtsTrue;
65               /* hack: re-use the blackholes_need_checking flag */
66               threadRunnable();
67               /* ToDo: this threadRunnable only works if there's
68                * another thread (not this one) waiting to be woken up
69                */
70               break;
71           default:
72               break;
73           }
74 #endif
75       }
76   }
77 }
78
79 int
80 startTimer(nat ms)
81 {
82 #ifdef PROFILING
83   initProfTimer();
84 #endif
85
86   return startTicker(ms, handle_tick);
87 }
88
89 int
90 stopTimer()
91 {
92   return stopTicker();
93 }