Windows: give a better error message when running out of memory
[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 "OSThreads.h"
14 #include "Capability.h"
15
16 /* initScheduler(), exitScheduler()
17 * Called from STG : no
18 * Locks assumed : none
19 */
20 void initScheduler (void);
21 void exitScheduler (rtsBool wait_foreign);
22 void freeScheduler (void);
23
24 // Place a new thread on the run queue of the current Capability
25 void scheduleThread (Capability *cap, StgTSO *tso);
26
27 // Place a new thread on the run queue of a specified Capability
28 // (cap is the currently owned Capability, cpu is the number of
29 // the desired Capability).
30 void scheduleThreadOn(Capability *cap, StgWord cpu, StgTSO *tso);
31
32 /* awakenBlockedQueue()
33 *
34 * Takes a pointer to the beginning of a blocked TSO queue, and
35 * wakes up the entire queue.
36 * Called from STG : yes
37 * Locks assumed : none
38 */
39 #if defined(GRAN)
40 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
41 #elif defined(PAR)
42 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
43 #else
44 void awakenBlockedQueue (Capability *cap, StgTSO *tso);
45 #endif
46
47 /* wakeUpRts()
48 *
49 * Causes an OS thread to wake up and run the scheduler, if necessary.
50 */
51 void wakeUpRts(void);
52
53 /* unblockOne()
54 *
55 * Put the specified thread on the run queue of the given Capability.
56 * Called from STG : yes
57 * Locks assumed : we own the Capability.
58 */
59 StgTSO * unblockOne (Capability *cap, StgTSO *tso);
60
61 /* raiseExceptionHelper */
62 StgWord raiseExceptionHelper (StgRegTable *reg, StgTSO *tso, StgClosure *exception);
63
64 /* findRetryFrameHelper */
65 StgWord findRetryFrameHelper (StgTSO *tso);
66
67 /* GetRoots(evac_fn f)
68 *
69 * Call f() for each root known to the scheduler.
70 *
71 * Called from STG : NO
72 * Locks assumed : ????
73 */
74 void GetRoots(evac_fn);
75
76 /* workerStart()
77 *
78 * Entry point for a new worker task.
79 * Called from STG : NO
80 * Locks assumed : none
81 */
82 void workerStart(Task *task);
83
84 #if defined(GRAN)
85 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
86 void unlink_from_bq(StgTSO* tso, StgClosure* node);
87 void initThread(StgTSO *tso, nat stack_size, StgInt pri);
88 #elif defined(PAR)
89 nat run_queue_len(void);
90 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
91 void initThread(StgTSO *tso, nat stack_size);
92 #else
93 char *info_type(StgClosure *closure); // dummy
94 char *info_type_by_ip(StgInfoTable *ip); // dummy
95 void awaken_blocked_queue(StgTSO *q);
96 void initThread(StgTSO *tso, nat stack_size);
97 #endif
98
99 /* Context switch flag.
100 * Locks required : none (conflicts are harmless)
101 */
102 extern int RTS_VAR(context_switch);
103
104 /* The state of the scheduler. This is used to control the sequence
105 * of events during shutdown, and when the runtime is interrupted
106 * using ^C.
107 */
108 #define SCHED_RUNNING 0 /* running as normal */
109 #define SCHED_INTERRUPTING 1 /* ^C detected, before threads are deleted */
110 #define SCHED_SHUTTING_DOWN 2 /* final shutdown */
111
112 extern rtsBool RTS_VAR(sched_state);
113
114 /*
115 * flag that tracks whether we have done any execution in this time slice.
116 */
117 #define ACTIVITY_YES 0 /* there has been activity in the current slice */
118 #define ACTIVITY_MAYBE_NO 1 /* no activity in the current slice */
119 #define ACTIVITY_INACTIVE 2 /* a complete slice has passed with no activity */
120 #define ACTIVITY_DONE_GC 3 /* like 2, but we've done a GC too */
121
122 /* Recent activity flag.
123 * Locks required : Transition from MAYBE_NO to INACTIVE
124 * happens in the timer signal, so it is atomic. Trnasition from
125 * INACTIVE to DONE_GC happens under sched_mutex. No lock required
126 * to set it to ACTIVITY_YES.
127 */
128 extern nat recent_activity;
129
130 /* Thread queues.
131 * Locks required : sched_mutex
132 *
133 * In GranSim we have one run/blocked_queue per PE.
134 */
135 #if defined(GRAN)
136 // run_queue_hds defined in GranSim.h
137 #else
138 extern StgTSO *RTS_VAR(blackhole_queue);
139 #if !defined(THREADED_RTS)
140 extern StgTSO *RTS_VAR(blocked_queue_hd), *RTS_VAR(blocked_queue_tl);
141 extern StgTSO *RTS_VAR(sleeping_queue);
142 #endif
143 #endif
144
145 /* Linked list of all threads.
146 * Locks required : sched_mutex
147 */
148 extern StgTSO *RTS_VAR(all_threads);
149
150 /* Set to rtsTrue if there are threads on the blackhole_queue, and
151 * it is possible that one or more of them may be available to run.
152 * This flag is set to rtsFalse after we've checked the queue, and
153 * set to rtsTrue just before we run some Haskell code. It is used
154 * to decide whether we should yield the Capability or not.
155 * Locks required : none (see scheduleCheckBlackHoles()).
156 */
157 extern rtsBool blackholes_need_checking;
158
159 #if defined(THREADED_RTS)
160 extern Mutex RTS_VAR(sched_mutex);
161 #endif
162
163 SchedulerStatus rts_mainLazyIO(HaskellObj p, /*out*/HaskellObj *ret);
164
165 /* Called by shutdown_handler(). */
166 void interruptStgRts (void);
167
168 nat run_queue_len (void);
169
170 void resurrectThreads (StgTSO *);
171
172 void printAllThreads(void);
173
174 /* debugging only
175 */
176 #ifdef DEBUG
177 void print_bq (StgClosure *node);
178 #endif
179 #if defined(PAR)
180 void print_bqe (StgBlockingQueueElement *bqe);
181 #endif
182
183 /* -----------------------------------------------------------------------------
184 * Some convenient macros/inline functions...
185 */
186
187 #if !IN_STG_CODE
188
189 /* END_TSO_QUEUE and friends now defined in includes/StgMiscClosures.h */
190
191 /* Add a thread to the end of the run queue.
192 * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
193 * ASSUMES: cap->running_task is the current task.
194 */
195 INLINE_HEADER void
196 appendToRunQueue (Capability *cap, StgTSO *tso)
197 {
198 ASSERT(tso->link == END_TSO_QUEUE);
199 if (cap->run_queue_hd == END_TSO_QUEUE) {
200 cap->run_queue_hd = tso;
201 } else {
202 cap->run_queue_tl->link = tso;
203 }
204 cap->run_queue_tl = tso;
205 }
206
207 /* Push a thread on the beginning of the run queue. Used for
208 * newly awakened threads, so they get run as soon as possible.
209 * ASSUMES: cap->running_task is the current task.
210 */
211 INLINE_HEADER void
212 pushOnRunQueue (Capability *cap, StgTSO *tso)
213 {
214 tso->link = cap->run_queue_hd;
215 cap->run_queue_hd = tso;
216 if (cap->run_queue_tl == END_TSO_QUEUE) {
217 cap->run_queue_tl = tso;
218 }
219 }
220
221 /* Pop the first thread off the runnable queue.
222 */
223 INLINE_HEADER StgTSO *
224 popRunQueue (Capability *cap)
225 {
226 StgTSO *t = cap->run_queue_hd;
227 ASSERT(t != END_TSO_QUEUE);
228 cap->run_queue_hd = t->link;
229 t->link = END_TSO_QUEUE;
230 if (cap->run_queue_hd == END_TSO_QUEUE) {
231 cap->run_queue_tl = END_TSO_QUEUE;
232 }
233 return t;
234 }
235
236 /* Add a thread to the end of the blocked queue.
237 */
238 #if !defined(THREADED_RTS)
239 INLINE_HEADER void
240 appendToBlockedQueue(StgTSO *tso)
241 {
242 ASSERT(tso->link == END_TSO_QUEUE);
243 if (blocked_queue_hd == END_TSO_QUEUE) {
244 blocked_queue_hd = tso;
245 } else {
246 blocked_queue_tl->link = tso;
247 }
248 blocked_queue_tl = tso;
249 }
250 #endif
251
252 #if defined(THREADED_RTS)
253 INLINE_HEADER void
254 appendToWakeupQueue (Capability *cap, StgTSO *tso)
255 {
256 ASSERT(tso->link == END_TSO_QUEUE);
257 if (cap->wakeup_queue_hd == END_TSO_QUEUE) {
258 cap->wakeup_queue_hd = tso;
259 } else {
260 cap->wakeup_queue_tl->link = tso;
261 }
262 cap->wakeup_queue_tl = tso;
263 }
264 #endif
265
266 /* Check whether various thread queues are empty
267 */
268 INLINE_HEADER rtsBool
269 emptyQueue (StgTSO *q)
270 {
271 return (q == END_TSO_QUEUE);
272 }
273
274 INLINE_HEADER rtsBool
275 emptyRunQueue(Capability *cap)
276 {
277 return emptyQueue(cap->run_queue_hd);
278 }
279
280 #if defined(THREADED_RTS)
281 INLINE_HEADER rtsBool
282 emptyWakeupQueue(Capability *cap)
283 {
284 return emptyQueue(cap->wakeup_queue_hd);
285 }
286 #endif
287
288 #if !defined(THREADED_RTS)
289 #define EMPTY_BLOCKED_QUEUE() (emptyQueue(blocked_queue_hd))
290 #define EMPTY_SLEEPING_QUEUE() (emptyQueue(sleeping_queue))
291 #endif
292
293 INLINE_HEADER rtsBool
294 emptyThreadQueues(Capability *cap)
295 {
296 return emptyRunQueue(cap)
297 #if !defined(THREADED_RTS)
298 && EMPTY_BLOCKED_QUEUE() && EMPTY_SLEEPING_QUEUE()
299 #endif
300 ;
301 }
302
303 #endif /* !IN_STG_CODE */
304
305 INLINE_HEADER void
306 dirtyTSO (StgTSO *tso)
307 {
308 tso->flags |= TSO_DIRTY;
309 }
310
311 #endif /* SCHEDULE_H */
312