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