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