[project @ 2003-09-21 22:20:51 by wolfgang]
[ghc-hetmet.git] / ghc / rts / Schedule.h
1 /* -----------------------------------------------------------------------------
2  * $Id: Schedule.h,v 1.39 2003/09/21 22:20:56 wolfgang 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
94 /* GetRoots(evac_fn f)
95  *
96  * Call f() for each root known to the scheduler.
97  *
98  * Called from STG :  NO
99  * Locks assumed   :  ????
100  */
101 void GetRoots(evac_fn);
102
103 // ToDo: check whether all fcts below are used in the SMP version, too
104 #if defined(GRAN)
105 void    awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
106 void    unlink_from_bq(StgTSO* tso, StgClosure* node);
107 void    initThread(StgTSO *tso, nat stack_size, StgInt pri);
108 #elif defined(PAR)
109 nat     run_queue_len(void);
110 void    awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
111 void    initThread(StgTSO *tso, nat stack_size);
112 #else
113 char   *info_type(StgClosure *closure);    // dummy
114 char   *info_type_by_ip(StgInfoTable *ip); // dummy
115 void    awaken_blocked_queue(StgTSO *q);
116 void    initThread(StgTSO *tso, nat stack_size);
117 #endif
118
119 /* Context switch flag.
120  * Locks required  : sched_mutex
121  */
122 extern nat context_switch;
123 extern rtsBool interrupted;
124
125 /* In Select.c */
126 extern nat timestamp;
127
128 /* Thread queues.
129  * Locks required  : sched_mutex
130  *
131  * In GranSim we have one run/blocked_queue per PE.
132  */
133 #if defined(GRAN)
134 // run_queue_hds defined in GranSim.h
135 #else
136 extern  StgTSO *run_queue_hd, *run_queue_tl;
137 extern  StgTSO *blocked_queue_hd, *blocked_queue_tl;
138 extern  StgTSO *sleeping_queue;
139 #endif
140 /* Linked list of all threads. */
141 extern  StgTSO *all_threads;
142
143 #if defined(RTS_SUPPORTS_THREADS)
144 /* Schedule.c has detailed info on what these do */
145 extern Mutex       sched_mutex;
146 extern Condition   thread_ready_cond;
147 extern Condition   returning_worker_cond;
148 extern nat         rts_n_waiting_workers;
149 extern nat         rts_n_waiting_tasks;
150 #endif
151
152 StgBool rtsSupportsBoundThreads(void);
153 StgBool isThreadBound(StgTSO *tso);
154 StgInt forkProcess(StgTSO *tso);
155
156 extern SchedulerStatus rts_mainLazyIO(HaskellObj p, /*out*/HaskellObj *ret);
157
158
159 /* Called by shutdown_handler(). */
160 void interruptStgRts ( void );
161
162 void raiseAsync(StgTSO *tso, StgClosure *exception);
163 nat  run_queue_len(void);
164
165 void resurrectThreads( StgTSO * );
166
167 /* Main threads:
168  *
169  * These are the threads which clients have requested that we run.  
170  *
171  * In a 'threaded' build, each of these corresponds to one bound thread.
172  * The pointer to the StgMainThread is passed as a parameter to schedule;
173  * this invocation of schedule will always pass this main thread's
174  * bound_thread_cond to waitForkWorkCapability; OS-thread-switching
175  * takes place using passCapability.
176  *
177  * In non-threaded builds, clients are strictly nested: the first client calls
178  * into the RTS, which might call out again to C with a _ccall_GC, and
179  * eventually re-enter the RTS.
180  *
181  * This is non-abstract at the moment because the garbage collector
182  * treats pointers to TSOs from the main thread list as "weak" - these
183  * pointers won't prevent a thread from receiving a BlockedOnDeadMVar
184  * exception.
185  *
186  * Main threads information is kept in a linked list:
187  */
188 typedef struct StgMainThread_ {
189   StgTSO *         tso;
190   SchedulerStatus  stat;
191   StgClosure **    ret;
192 #if defined(RTS_SUPPORTS_THREADS)
193   Condition        wakeup;
194 #if defined(THREADED_RTS)
195   Condition        bound_thread_cond;
196 #endif
197 #endif
198   struct StgMainThread_ *link;
199 } StgMainThread;
200
201 /* Main thread queue.
202  * Locks required: sched_mutex.
203  */
204 extern StgMainThread *main_threads;
205
206 void printAllThreads(void);
207 #ifdef COMPILING_SCHEDULER
208 static void printThreadBlockage(StgTSO *tso);
209 static void printThreadStatus(StgTSO *tso);
210 #endif
211 /* debugging only 
212  */
213 #ifdef DEBUG
214 void print_bq (StgClosure *node);
215 #endif
216 #if defined(PAR)
217 void print_bqe (StgBlockingQueueElement *bqe);
218 #endif
219
220 void labelThread(StgPtr tso, char *label);
221
222 /* -----------------------------------------------------------------------------
223  * Some convenient macros...
224  */
225
226 /* END_TSO_QUEUE and friends now defined in includes/StgMiscClosures.h */
227
228 /* Add a thread to the end of the run queue.
229  * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
230  */
231 #define APPEND_TO_RUN_QUEUE(tso)                \
232     ASSERT(tso->link == END_TSO_QUEUE);         \
233     if (run_queue_hd == END_TSO_QUEUE) {        \
234       run_queue_hd = tso;                       \
235     } else {                                    \
236       run_queue_tl->link = tso;                 \
237     }                                           \
238     run_queue_tl = tso;
239
240 /* Push a thread on the beginning of the run queue.  Used for
241  * newly awakened threads, so they get run as soon as possible.
242  */
243 #define PUSH_ON_RUN_QUEUE(tso)                  \
244     tso->link = run_queue_hd;                   \
245       run_queue_hd = tso;                       \
246     if (run_queue_tl == END_TSO_QUEUE) {        \
247       run_queue_tl = tso;                       \
248     }
249
250 /* Pop the first thread off the runnable queue.
251  */
252 #define POP_RUN_QUEUE()                         \
253   ({ StgTSO *t = run_queue_hd;                  \
254     if (t != END_TSO_QUEUE) {                   \
255       run_queue_hd = t->link;                   \
256       t->link = END_TSO_QUEUE;                  \
257       if (run_queue_hd == END_TSO_QUEUE) {      \
258         run_queue_tl = END_TSO_QUEUE;           \
259       }                                         \
260     }                                           \
261     t;                                          \
262   })
263
264 /* Add a thread to the end of the blocked queue.
265  */
266 #define APPEND_TO_BLOCKED_QUEUE(tso)            \
267     ASSERT(tso->link == END_TSO_QUEUE);         \
268     if (blocked_queue_hd == END_TSO_QUEUE) {    \
269       blocked_queue_hd = tso;                   \
270     } else {                                    \
271       blocked_queue_tl->link = tso;             \
272     }                                           \
273     blocked_queue_tl = tso;
274
275 /* Signal that a runnable thread has become available, in
276  * case there are any waiting tasks to execute it.
277  */
278 #if defined(RTS_SUPPORTS_THREADS)
279 #define THREAD_RUNNABLE()                       \
280   wakeBlockedWorkerThread();                    \
281   context_switch = 1;
282 #else
283 #define THREAD_RUNNABLE()  /* nothing */
284 #endif
285
286 /* Check whether various thread queues are empty
287  */
288 #define EMPTY_QUEUE(q)         (q == END_TSO_QUEUE)
289
290 #define EMPTY_RUN_QUEUE()      (EMPTY_QUEUE(run_queue_hd))
291 #define EMPTY_BLOCKED_QUEUE()  (EMPTY_QUEUE(blocked_queue_hd))
292 #define EMPTY_SLEEPING_QUEUE() (EMPTY_QUEUE(sleeping_queue))
293
294 #define EMPTY_THREAD_QUEUES()  (EMPTY_RUN_QUEUE() && \
295                                 EMPTY_BLOCKED_QUEUE() && \
296                                 EMPTY_SLEEPING_QUEUE())
297
298 #if defined(RTS_SUPPORTS_THREADS)
299 /* If no task is waiting for a capability,
300  * spawn a new worker thread.
301  *
302  * (Used by the RtsAPI)
303  */
304 void
305 startSchedulerTask(void);
306 #endif
307
308 #endif /* __SCHEDULE_H__ */