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