[project @ 2002-04-10 11:43:43 by stolz]
[ghc-hetmet.git] / ghc / rts / Schedule.h
1 /* -----------------------------------------------------------------------------
2  * $Id: Schedule.h,v 1.32 2002/04/10 11:43:46 stolz 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 #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
62 /* awaitEvent()
63  *
64  * Raises an exception asynchronously in the specified thread.
65  *
66  * Called from STG :  NO
67  * Locks assumed   :  sched_mutex
68  */
69 void awaitEvent(rtsBool wait);  /* In Select.c */
70
71 /* wakeUpSleepingThreads(nat ticks)
72  *
73  * Wakes up any sleeping threads whose timers have expired.
74  *
75  * Called from STG :  NO
76  * Locks assumed   :  sched_mutex
77  */
78 rtsBool wakeUpSleepingThreads(nat);  /* In Select.c */
79
80 /* GetRoots(evac_fn f)
81  *
82  * Call f() for each root known to the scheduler.
83  *
84  * Called from STG :  NO
85  * Locks assumed   :  ????
86  */
87 void GetRoots(evac_fn);
88
89 // ToDo: check whether all fcts below are used in the SMP version, too
90 #if defined(GRAN)
91 void    awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
92 void    unlink_from_bq(StgTSO* tso, StgClosure* node);
93 void    initThread(StgTSO *tso, nat stack_size, StgInt pri);
94 #elif defined(PAR)
95 nat     run_queue_len(void);
96 void    awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
97 void    initThread(StgTSO *tso, nat stack_size);
98 #else
99 char   *info_type(StgClosure *closure);    // dummy
100 char   *info_type_by_ip(StgInfoTable *ip); // dummy
101 void    awaken_blocked_queue(StgTSO *q);
102 void    initThread(StgTSO *tso, nat stack_size);
103 #endif
104
105 /* Context switch flag.
106  * Locks required  : sched_mutex
107  */
108 extern nat context_switch;
109 extern rtsBool interrupted;
110
111 /* In Select.c */
112 extern nat timestamp;
113
114 /* Thread queues.
115  * Locks required  : sched_mutex
116  *
117  * In GranSim we have one run/blocked_queue per PE.
118  */
119 #if defined(GRAN)
120 // run_queue_hds defined in GranSim.h
121 #else
122 extern  StgTSO *run_queue_hd, *run_queue_tl;
123 extern  StgTSO *blocked_queue_hd, *blocked_queue_tl;
124 extern  StgTSO *sleeping_queue;
125 #endif
126 /* Linked list of all threads. */
127 extern  StgTSO *all_threads;
128
129 #if defined(RTS_SUPPORTS_THREADS)
130 /* Schedule.c has detailed info on what these do */
131 extern Mutex       sched_mutex;
132 extern Condition   thread_ready_cond;
133 extern Condition   returning_worker_cond;
134 extern nat         rts_n_waiting_workers;
135 extern nat         rts_n_waiting_tasks;
136 #endif
137
138 StgInt forkProcess(StgTSO *tso);
139
140 /* Sigh, RTS-internal versions of waitThread(), scheduleThread(), and
141    rts_evalIO() for the use by main() only. ToDo: better. */
142 extern SchedulerStatus waitThread_(StgTSO *tso,
143                                    /*out*/StgClosure **ret
144 #if defined(THREADED_RTS)
145                                    , rtsBool blockWaiting
146 #endif
147                                    );
148 extern SchedulerStatus rts_mainEvalIO(HaskellObj p, /*out*/HaskellObj *ret);
149
150
151 /* Called by shutdown_handler(). */
152 void interruptStgRts ( void );
153
154 void raiseAsync(StgTSO *tso, StgClosure *exception);
155 nat  run_queue_len(void);
156
157 void resurrectThreads( StgTSO * );
158
159 /* Main threads:
160  *
161  * These are the threads which clients have requested that we run.  
162  *
163  * In a 'threaded' build, we might have several concurrent clients all
164  * waiting for results, and each one will wait on a condition variable
165  * until the result is available.
166  *
167  * In non-SMP, clients are strictly nested: the first client calls
168  * into the RTS, which might call out again to C with a _ccall_GC, and
169  * eventually re-enter the RTS.
170  *
171  * This is non-abstract at the moment because the garbage collector
172  * treats pointers to TSOs from the main thread list as "weak" - these
173  * pointers won't prevent a thread from receiving a BlockedOnDeadMVar
174  * exception.
175  *
176  * Main threads information is kept in a linked list:
177  */
178 typedef struct StgMainThread_ {
179   StgTSO *         tso;
180   SchedulerStatus  stat;
181   StgClosure **    ret;
182 #if defined(RTS_SUPPORTS_THREADS)
183   Condition        wakeup;
184 #endif
185   struct StgMainThread_ *link;
186 } StgMainThread;
187
188 /* Main thread queue.
189  * Locks required: sched_mutex.
190  */
191 extern StgMainThread *main_threads;
192
193 /* debugging only 
194  */
195 #ifdef DEBUG
196 void printThreadBlockage(StgTSO *tso);
197 void printThreadStatus(StgTSO *tso);
198 void printAllThreads(void);
199 #endif
200 void print_bq (StgClosure *node);
201 #if defined(PAR)
202 void print_bqe (StgBlockingQueueElement *bqe);
203 #endif
204
205 /* -----------------------------------------------------------------------------
206  * Some convenient macros...
207  */
208
209 /* END_TSO_QUEUE and friends now defined in includes/StgMiscClosures.h */
210
211 /* Add a thread to the end of the run queue.
212  * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
213  */
214 #define APPEND_TO_RUN_QUEUE(tso)                \
215     ASSERT(tso->link == END_TSO_QUEUE);         \
216     if (run_queue_hd == END_TSO_QUEUE) {        \
217       run_queue_hd = tso;                       \
218     } else {                                    \
219       run_queue_tl->link = tso;                 \
220     }                                           \
221     run_queue_tl = tso;
222
223 /* Push a thread on the beginning of the run queue.  Used for
224  * newly awakened threads, so they get run as soon as possible.
225  */
226 #define PUSH_ON_RUN_QUEUE(tso)                  \
227     tso->link = run_queue_hd;                   \
228       run_queue_hd = tso;                       \
229     if (run_queue_tl == END_TSO_QUEUE) {        \
230       run_queue_tl = tso;                       \
231     }
232
233 /* Pop the first thread off the runnable queue.
234  */
235 #define POP_RUN_QUEUE()                         \
236   ({ StgTSO *t = run_queue_hd;                  \
237     if (t != END_TSO_QUEUE) {                   \
238       run_queue_hd = t->link;                   \
239       t->link = END_TSO_QUEUE;                  \
240       if (run_queue_hd == END_TSO_QUEUE) {      \
241         run_queue_tl = END_TSO_QUEUE;           \
242       }                                         \
243     }                                           \
244     t;                                          \
245   })
246
247 /* Add a thread to the end of the blocked queue.
248  */
249 #define APPEND_TO_BLOCKED_QUEUE(tso)            \
250     ASSERT(tso->link == END_TSO_QUEUE);         \
251     if (blocked_queue_hd == END_TSO_QUEUE) {    \
252       blocked_queue_hd = tso;                   \
253     } else {                                    \
254       blocked_queue_tl->link = tso;             \
255     }                                           \
256     blocked_queue_tl = tso;
257
258 /* Signal that a runnable thread has become available, in
259  * case there are any waiting tasks to execute it.
260  */
261 #if defined(RTS_SUPPORTS_THREADS)
262 #define THREAD_RUNNABLE()                       \
263   if ( !noCapabilities() ) {                    \
264      signalCondition(&thread_ready_cond);       \
265   }                                             \
266   context_switch = 1;
267 #else
268 #define THREAD_RUNNABLE()  /* nothing */
269 #endif
270
271 /* Check whether various thread queues are empty
272  */
273 #define EMPTY_QUEUE(q)         (q == END_TSO_QUEUE)
274
275 #define EMPTY_RUN_QUEUE()      (EMPTY_QUEUE(run_queue_hd))
276 #define EMPTY_BLOCKED_QUEUE()  (EMPTY_QUEUE(blocked_queue_hd))
277 #define EMPTY_SLEEPING_QUEUE() (EMPTY_QUEUE(sleeping_queue))
278
279 #define EMPTY_THREAD_QUEUES()  (EMPTY_RUN_QUEUE() && \
280                                 EMPTY_BLOCKED_QUEUE() && \
281                                 EMPTY_SLEEPING_QUEUE())
282
283 #endif /* __SCHEDULE_H__ */