(partial) Merge branch 'master' into type-nats
[ghc.git] / rts / Schedule.h
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team 1998-2005
4 *
5 * Prototypes for functions in Schedule.c
6 * (RTS internal scheduler interface)
7 *
8 * -------------------------------------------------------------------------*/
9
10 #ifndef SCHEDULE_H
11 #define SCHEDULE_H
12
13 #include "rts/OSThreads.h"
14 #include "Capability.h"
15 #include "Trace.h"
16
17 #include "BeginPrivate.h"
18
19 /* initScheduler(), exitScheduler()
20 * Called from STG : no
21 * Locks assumed : none
22 */
23 void initScheduler (void);
24 void exitScheduler (rtsBool wait_foreign);
25 void freeScheduler (void);
26 void markScheduler (evac_fn evac, void *user);
27
28 // Place a new thread on the run queue of the current Capability
29 void scheduleThread (Capability *cap, StgTSO *tso);
30
31 // Place a new thread on the run queue of a specified Capability
32 // (cap is the currently owned Capability, cpu is the number of
33 // the desired Capability).
34 void scheduleThreadOn(Capability *cap, StgWord cpu, StgTSO *tso);
35
36 /* wakeUpRts()
37 *
38 * Causes an OS thread to wake up and run the scheduler, if necessary.
39 */
40 #if defined(THREADED_RTS)
41 void wakeUpRts(void);
42 #endif
43
44 /* raiseExceptionHelper */
45 StgWord raiseExceptionHelper (StgRegTable *reg, StgTSO *tso, StgClosure *exception);
46
47 /* findRetryFrameHelper */
48 StgWord findRetryFrameHelper (Capability *cap, StgTSO *tso);
49
50 /* Entry point for a new worker */
51 void scheduleWorker (Capability *cap, Task *task);
52
53 /* The state of the scheduler. This is used to control the sequence
54 * of events during shutdown, and when the runtime is interrupted
55 * using ^C.
56 */
57 #define SCHED_RUNNING 0 /* running as normal */
58 #define SCHED_INTERRUPTING 1 /* ^C detected, before threads are deleted */
59 #define SCHED_SHUTTING_DOWN 2 /* final shutdown */
60
61 extern volatile StgWord sched_state;
62
63 /*
64 * flag that tracks whether we have done any execution in this time slice.
65 */
66 #define ACTIVITY_YES 0 /* there has been activity in the current slice */
67 #define ACTIVITY_MAYBE_NO 1 /* no activity in the current slice */
68 #define ACTIVITY_INACTIVE 2 /* a complete slice has passed with no activity */
69 #define ACTIVITY_DONE_GC 3 /* like 2, but we've done a GC too */
70
71 /* Recent activity flag.
72 * Locks required : Transition from MAYBE_NO to INACTIVE
73 * happens in the timer signal, so it is atomic. Trnasition from
74 * INACTIVE to DONE_GC happens under sched_mutex. No lock required
75 * to set it to ACTIVITY_YES.
76 */
77 extern volatile StgWord recent_activity;
78
79 /* Thread queues.
80 * Locks required : sched_mutex
81 *
82 * In GranSim we have one run/blocked_queue per PE.
83 */
84 extern StgTSO *blackhole_queue;
85 #if !defined(THREADED_RTS)
86 extern StgTSO *blocked_queue_hd, *blocked_queue_tl;
87 extern StgTSO *sleeping_queue;
88 #endif
89
90 extern rtsBool heap_overflow;
91
92 #if defined(THREADED_RTS)
93 extern Mutex sched_mutex;
94 #endif
95
96 /* Called by shutdown_handler(). */
97 void interruptStgRts (void);
98
99 void resurrectThreads (StgTSO *);
100
101 /* -----------------------------------------------------------------------------
102 * Some convenient macros/inline functions...
103 */
104
105 #if !IN_STG_CODE
106
107 /* END_TSO_QUEUE and friends now defined in includes/StgMiscClosures.h */
108
109 /* Add a thread to the end of the run queue.
110 * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
111 * ASSUMES: cap->running_task is the current task.
112 */
113 EXTERN_INLINE void
114 appendToRunQueue (Capability *cap, StgTSO *tso);
115
116 EXTERN_INLINE void
117 appendToRunQueue (Capability *cap, StgTSO *tso)
118 {
119 ASSERT(tso->_link == END_TSO_QUEUE);
120 if (cap->run_queue_hd == END_TSO_QUEUE) {
121 cap->run_queue_hd = tso;
122 tso->block_info.prev = END_TSO_QUEUE;
123 } else {
124 setTSOLink(cap, cap->run_queue_tl, tso);
125 setTSOPrev(cap, tso, cap->run_queue_tl);
126 }
127 cap->run_queue_tl = tso;
128 traceEventThreadRunnable (cap, tso);
129 }
130
131 /* Push a thread on the beginning of the run queue.
132 * ASSUMES: cap->running_task is the current task.
133 */
134 EXTERN_INLINE void
135 pushOnRunQueue (Capability *cap, StgTSO *tso);
136
137 EXTERN_INLINE void
138 pushOnRunQueue (Capability *cap, StgTSO *tso)
139 {
140 setTSOLink(cap, tso, cap->run_queue_hd);
141 tso->block_info.prev = END_TSO_QUEUE;
142 if (cap->run_queue_hd != END_TSO_QUEUE) {
143 setTSOPrev(cap, cap->run_queue_hd, tso);
144 }
145 cap->run_queue_hd = tso;
146 if (cap->run_queue_tl == END_TSO_QUEUE) {
147 cap->run_queue_tl = tso;
148 }
149 }
150
151 /* Pop the first thread off the runnable queue.
152 */
153 INLINE_HEADER StgTSO *
154 popRunQueue (Capability *cap)
155 {
156 StgTSO *t = cap->run_queue_hd;
157 ASSERT(t != END_TSO_QUEUE);
158 cap->run_queue_hd = t->_link;
159 if (t->_link != END_TSO_QUEUE) {
160 t->_link->block_info.prev = END_TSO_QUEUE;
161 }
162 t->_link = END_TSO_QUEUE; // no write barrier req'd
163 if (cap->run_queue_hd == END_TSO_QUEUE) {
164 cap->run_queue_tl = END_TSO_QUEUE;
165 }
166 return t;
167 }
168
169 extern void removeFromRunQueue (Capability *cap, StgTSO *tso);
170
171 /* Add a thread to the end of the blocked queue.
172 */
173 #if !defined(THREADED_RTS)
174 INLINE_HEADER void
175 appendToBlockedQueue(StgTSO *tso)
176 {
177 ASSERT(tso->_link == END_TSO_QUEUE);
178 if (blocked_queue_hd == END_TSO_QUEUE) {
179 blocked_queue_hd = tso;
180 } else {
181 setTSOLink(&MainCapability, blocked_queue_tl, tso);
182 }
183 blocked_queue_tl = tso;
184 }
185 #endif
186
187 /* Check whether various thread queues are empty
188 */
189 INLINE_HEADER rtsBool
190 emptyQueue (StgTSO *q)
191 {
192 return (q == END_TSO_QUEUE);
193 }
194
195 INLINE_HEADER rtsBool
196 emptyRunQueue(Capability *cap)
197 {
198 return emptyQueue(cap->run_queue_hd);
199 }
200
201 #if !defined(THREADED_RTS)
202 #define EMPTY_BLOCKED_QUEUE() (emptyQueue(blocked_queue_hd))
203 #define EMPTY_SLEEPING_QUEUE() (emptyQueue(sleeping_queue))
204 #endif
205
206 INLINE_HEADER rtsBool
207 emptyThreadQueues(Capability *cap)
208 {
209 return emptyRunQueue(cap)
210 #if !defined(THREADED_RTS)
211 && EMPTY_BLOCKED_QUEUE() && EMPTY_SLEEPING_QUEUE()
212 #endif
213 ;
214 }
215
216 #endif /* !IN_STG_CODE */
217
218 #include "EndPrivate.h"
219
220 #endif /* SCHEDULE_H */
221