[project @ 2004-02-12 02:04:59 by mthomas]
[ghc-hetmet.git] / ghc / rts / Schedule.h
1 /* -----------------------------------------------------------------------------
2  * $Id: Schedule.h,v 1.43 2003/12/12 16:35:21 simonmar Exp $
3  *
4  * (c) The GHC Team 1998-1999
5  *
6  * Prototypes for functions in Schedule.c 
7  * (RTS internal scheduler interface)
8  *
9  * -------------------------------------------------------------------------*/
10
11 #ifndef __SCHEDULE_H__
12 #define __SCHEDULE_H__
13 #include "OSThreads.h"
14
15 /* initScheduler(), exitScheduler(), startTasks()
16  * 
17  * Called from STG :  no
18  * Locks assumed   :  none
19  */
20 extern void initScheduler  ( void );
21 extern void exitScheduler  ( void );
22
23 /* awakenBlockedQueue()
24  *
25  * Takes a pointer to the beginning of a blocked TSO queue, and
26  * wakes up the entire queue.
27  *
28  * Called from STG :  yes
29  * Locks assumed   :  none
30  */
31 #if defined(GRAN)
32 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
33 #elif defined(PAR)
34 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
35 #else
36 void awakenBlockedQueue(StgTSO *tso);
37 #if defined(RTS_SUPPORTS_THREADS)
38 void awakenBlockedQueueNoLock(StgTSO *tso);
39 #endif
40 #endif
41
42 /* unblockOne()
43  *
44  * Takes a pointer to the beginning of a blocked TSO queue, and
45  * removes the first thread, placing it on the runnable queue.
46  *
47  * Called from STG : yes
48  * Locks assumed   : none
49  */
50 #if defined(GRAN) || defined(PAR)
51 StgBlockingQueueElement *unblockOne(StgBlockingQueueElement *bqe, StgClosure *node);
52 #else
53 StgTSO *unblockOne(StgTSO *tso);
54 #endif
55
56 /* raiseAsync()
57  *
58  * Raises an exception asynchronously in the specified thread.
59  *
60  * Called from STG :  yes
61  * Locks assumed   :  none
62  */
63 void raiseAsync(StgTSO *tso, StgClosure *exception);
64 void raiseAsyncWithLock(StgTSO *tso, StgClosure *exception);
65
66 /* awaitEvent(rtsBool wait)
67  *
68  * Checks for blocked threads that need to be woken.
69  *
70  * Called from STG :  NO
71  * Locks assumed   :  sched_mutex
72  */
73 void awaitEvent(rtsBool wait);  /* In Select.c */
74
75 /* wakeUpSleepingThreads(nat ticks)
76  *
77  * Wakes up any sleeping threads whose timers have expired.
78  *
79  * Called from STG :  NO
80  * Locks assumed   :  sched_mutex
81  */
82 rtsBool wakeUpSleepingThreads(nat);  /* In Select.c */
83
84 /* wakeBlockedWorkerThread()
85  *
86  * If a worker thread is currently blocked in awaitEvent(), interrupt it.
87  *
88  * Called from STG :  NO
89  * Locks assumed   :  sched_mutex
90  */
91 void wakeBlockedWorkerThread(void); /* In Select.c */
92
93 /* resetWorkerWakeupPipeAfterFork()
94  *
95  * Notify Select.c that a fork() has occured
96  *
97  * Called from STG :  NO
98  * Locks assumed   :  don't care, but must be called right after fork()
99  */
100 void resetWorkerWakeupPipeAfterFork(void); /* In Select.c */
101
102 /* GetRoots(evac_fn f)
103  *
104  * Call f() for each root known to the scheduler.
105  *
106  * Called from STG :  NO
107  * Locks assumed   :  ????
108  */
109 void GetRoots(evac_fn);
110
111 // ToDo: check whether all fcts below are used in the SMP version, too
112 #if defined(GRAN)
113 void    awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
114 void    unlink_from_bq(StgTSO* tso, StgClosure* node);
115 void    initThread(StgTSO *tso, nat stack_size, StgInt pri);
116 #elif defined(PAR)
117 nat     run_queue_len(void);
118 void    awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
119 void    initThread(StgTSO *tso, nat stack_size);
120 #else
121 char   *info_type(StgClosure *closure);    // dummy
122 char   *info_type_by_ip(StgInfoTable *ip); // dummy
123 void    awaken_blocked_queue(StgTSO *q);
124 void    initThread(StgTSO *tso, nat stack_size);
125 #endif
126
127 /* Context switch flag.
128  * Locks required  : sched_mutex
129  */
130 extern nat context_switch;
131 extern rtsBool interrupted;
132
133 /* In Select.c */
134 extern nat timestamp;
135
136 /* Thread queues.
137  * Locks required  : sched_mutex
138  *
139  * In GranSim we have one run/blocked_queue per PE.
140  */
141 #if defined(GRAN)
142 // run_queue_hds defined in GranSim.h
143 #else
144 extern  StgTSO *run_queue_hd, *run_queue_tl;
145 extern  StgTSO *blocked_queue_hd, *blocked_queue_tl;
146 extern  StgTSO *sleeping_queue;
147 #endif
148 /* Linked list of all threads. */
149 extern  StgTSO *all_threads;
150
151 #if defined(RTS_SUPPORTS_THREADS)
152 /* Schedule.c has detailed info on what these do */
153 extern Mutex       sched_mutex;
154 extern Condition   thread_ready_cond;
155 extern Condition   returning_worker_cond;
156 extern nat         rts_n_waiting_workers;
157 extern nat         rts_n_waiting_tasks;
158 #endif
159
160 StgBool rtsSupportsBoundThreads(void);
161 StgBool isThreadBound(StgTSO *tso);
162 StgInt forkProcess(HsStablePtr *entry);
163
164 extern SchedulerStatus rts_mainLazyIO(HaskellObj p, /*out*/HaskellObj *ret);
165
166
167 /* Called by shutdown_handler(). */
168 void interruptStgRts ( void );
169
170 void raiseAsync(StgTSO *tso, StgClosure *exception);
171 nat  run_queue_len(void);
172
173 void resurrectThreads( StgTSO * );
174
175 /* Main threads:
176  *
177  * These are the threads which clients have requested that we run.  
178  *
179  * In a 'threaded' build, each of these corresponds to one bound thread.
180  * The pointer to the StgMainThread is passed as a parameter to schedule;
181  * this invocation of schedule will always pass this main thread's
182  * bound_thread_cond to waitForkWorkCapability; OS-thread-switching
183  * takes place using passCapability.
184  *
185  * In non-threaded builds, clients are strictly nested: the first client calls
186  * into the RTS, which might call out again to C with a _ccall_GC, and
187  * eventually re-enter the RTS.
188  *
189  * This is non-abstract at the moment because the garbage collector
190  * treats pointers to TSOs from the main thread list as "weak" - these
191  * pointers won't prevent a thread from receiving a BlockedOnDeadMVar
192  * exception.
193  *
194  * Main threads information is kept in a linked list:
195  */
196 typedef struct StgMainThread_ {
197   StgTSO *         tso;
198   SchedulerStatus  stat;
199   StgClosure **    ret;
200 #if defined(RTS_SUPPORTS_THREADS)
201 #if defined(THREADED_RTS)
202   Condition        bound_thread_cond;
203 #else
204   Condition        wakeup;
205 #endif
206 #endif
207   struct StgMainThread_ *link;
208 } StgMainThread;
209
210 /* Main thread queue.
211  * Locks required: sched_mutex.
212  */
213 extern StgMainThread *main_threads;
214
215 void printAllThreads(void);
216 #ifdef COMPILING_SCHEDULER
217 static void printThreadBlockage(StgTSO *tso);
218 static void printThreadStatus(StgTSO *tso);
219 #endif
220 /* debugging only 
221  */
222 #ifdef DEBUG
223 void print_bq (StgClosure *node);
224 #endif
225 #if defined(PAR)
226 void print_bqe (StgBlockingQueueElement *bqe);
227 #endif
228
229 void labelThread(StgPtr tso, char *label);
230
231 /* -----------------------------------------------------------------------------
232  * Some convenient macros...
233  */
234
235 /* END_TSO_QUEUE and friends now defined in includes/StgMiscClosures.h */
236
237 /* Add a thread to the end of the run queue.
238  * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
239  */
240 #define APPEND_TO_RUN_QUEUE(tso)                \
241     ASSERT(tso->link == END_TSO_QUEUE);         \
242     if (run_queue_hd == END_TSO_QUEUE) {        \
243       run_queue_hd = tso;                       \
244     } else {                                    \
245       run_queue_tl->link = tso;                 \
246     }                                           \
247     run_queue_tl = tso;
248
249 /* Push a thread on the beginning of the run queue.  Used for
250  * newly awakened threads, so they get run as soon as possible.
251  */
252 #define PUSH_ON_RUN_QUEUE(tso)                  \
253     tso->link = run_queue_hd;                   \
254       run_queue_hd = tso;                       \
255     if (run_queue_tl == END_TSO_QUEUE) {        \
256       run_queue_tl = tso;                       \
257     }
258
259 /* Pop the first thread off the runnable queue.
260  */
261 #define POP_RUN_QUEUE(pt)                       \
262   do { StgTSO *__tmp_t = run_queue_hd;          \
263     if (__tmp_t != END_TSO_QUEUE) {             \
264       run_queue_hd = __tmp_t->link;             \
265       __tmp_t->link = END_TSO_QUEUE;            \
266       if (run_queue_hd == END_TSO_QUEUE) {      \
267         run_queue_tl = END_TSO_QUEUE;           \
268       }                                         \
269     }                                           \
270     pt = __tmp_t;                               \
271   } while(0)
272
273 /* Add a thread to the end of the blocked queue.
274  */
275 #define APPEND_TO_BLOCKED_QUEUE(tso)            \
276     ASSERT(tso->link == END_TSO_QUEUE);         \
277     if (blocked_queue_hd == END_TSO_QUEUE) {    \
278       blocked_queue_hd = tso;                   \
279     } else {                                    \
280       blocked_queue_tl->link = tso;             \
281     }                                           \
282     blocked_queue_tl = tso;
283
284 /* Signal that a runnable thread has become available, in
285  * case there are any waiting tasks to execute it.
286  */
287 #if defined(RTS_SUPPORTS_THREADS)
288 #define THREAD_RUNNABLE()                       \
289   wakeBlockedWorkerThread();                    \
290   context_switch = 1;
291 #else
292 #define THREAD_RUNNABLE()  /* nothing */
293 #endif
294
295 /* Check whether various thread queues are empty
296  */
297 #define EMPTY_QUEUE(q)         (q == END_TSO_QUEUE)
298
299 #define EMPTY_RUN_QUEUE()      (EMPTY_QUEUE(run_queue_hd))
300 #define EMPTY_BLOCKED_QUEUE()  (EMPTY_QUEUE(blocked_queue_hd))
301 #define EMPTY_SLEEPING_QUEUE() (EMPTY_QUEUE(sleeping_queue))
302
303 #define EMPTY_THREAD_QUEUES()  (EMPTY_RUN_QUEUE() && \
304                                 EMPTY_BLOCKED_QUEUE() && \
305                                 EMPTY_SLEEPING_QUEUE())
306
307 #if defined(RTS_SUPPORTS_THREADS)
308 /* If no task is waiting for a capability,
309  * and if there is work to be done
310  * or if we need to wait for IO or delay requests,
311  * spawn a new worker thread.
312  */
313 void
314 startSchedulerTaskIfNecessary(void);
315 #endif
316
317 #ifdef DEBUG
318 extern void sched_belch(char *s, ...);
319 #endif
320
321 #endif /* __SCHEDULE_H__ */