[project @ 2002-02-04 20:40:36 by sof]
[ghc-hetmet.git] / ghc / rts / Schedule.h
1 /* -----------------------------------------------------------------------------
2  * $Id: Schedule.h,v 1.26 2002/02/04 20:40:37 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 #ifndef __SCHEDULE_H__
11 #define __SCHEDULE_H__
12 #include "OSThreads.h"
13
14 //@menu
15 //* Scheduler Functions::       
16 //* Scheduler Vars and Data Types::  
17 //* Some convenient macros::    
18 //* Index::                     
19 //@end menu
20
21 //@node Scheduler Functions, Scheduler Vars and Data Types
22 //@subsection Scheduler Functions
23
24 //@cindex initScheduler
25 //@cindex exitScheduler
26 /* initScheduler(), exitScheduler(), startTasks()
27  * 
28  * Called from STG :  no
29  * Locks assumed   :  none
30  */
31 extern void initScheduler  ( void );
32 extern void exitScheduler  ( void );
33
34 //@cindex awakenBlockedQueue
35 /* awakenBlockedQueue()
36  *
37  * Takes a pointer to the beginning of a blocked TSO queue, and
38  * wakes up the entire queue.
39  *
40  * Called from STG :  yes
41  * Locks assumed   :  none
42  */
43 #if defined(GRAN)
44 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
45 #elif defined(PAR)
46 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
47 #else
48 void awakenBlockedQueue(StgTSO *tso);
49 #endif
50
51 //@cindex unblockOne
52 /* unblockOne()
53  *
54  * Takes a pointer to the beginning of a blocked TSO queue, and
55  * removes the first thread, placing it on the runnable queue.
56  *
57  * Called from STG : yes
58  * Locks assumed   : none
59  */
60 #if defined(GRAN) || defined(PAR)
61 StgBlockingQueueElement *unblockOne(StgBlockingQueueElement *bqe, StgClosure *node);
62 #else
63 StgTSO *unblockOne(StgTSO *tso);
64 #endif
65
66 //@cindex raiseAsync
67 /* raiseAsync()
68  *
69  * Raises an exception asynchronously in the specified thread.
70  *
71  * Called from STG :  yes
72  * Locks assumed   :  none
73  */
74 void raiseAsync(StgTSO *tso, StgClosure *exception);
75
76 //@cindex awaitEvent
77 /* awaitEvent()
78  *
79  * Raises an exception asynchronously in the specified thread.
80  *
81  * Called from STG :  NO
82  * Locks assumed   :  sched_mutex
83  */
84 void awaitEvent(rtsBool wait);  /* In Select.c */
85
86 /* wakeUpSleepingThreads(nat ticks)
87  *
88  * Wakes up any sleeping threads whose timers have expired.
89  *
90  * Called from STG :  NO
91  * Locks assumed   :  sched_mutex
92  */
93 rtsBool wakeUpSleepingThreads(nat);  /* In Select.c */
94
95 /* GetRoots(evac_fn f)
96  *
97  * Call f() for each root known to the scheduler.
98  *
99  * Called from STG :  NO
100  * Locks assumed   :  ????
101  */
102 void GetRoots(evac_fn);
103
104 // ToDo: check whether all fcts below are used in the SMP version, too
105 //@cindex awaken_blocked_queue
106 #if defined(GRAN)
107 void    awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
108 void    unlink_from_bq(StgTSO* tso, StgClosure* node);
109 void    initThread(StgTSO *tso, nat stack_size, StgInt pri);
110 #elif defined(PAR)
111 nat     run_queue_len(void);
112 void    awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
113 void    initThread(StgTSO *tso, nat stack_size);
114 #else
115 char   *info_type(StgClosure *closure);    // dummy
116 char   *info_type_by_ip(StgInfoTable *ip); // dummy
117 void    awaken_blocked_queue(StgTSO *q);
118 void    initThread(StgTSO *tso, nat stack_size);
119 #endif
120
121 //@node Scheduler Vars and Data Types, Some convenient macros, Scheduler Functions
122 //@subsection Scheduler Vars and Data Types
123
124 //@cindex context_switch
125 /* Context switch flag.
126  * Locks required  : sched_mutex
127  */
128 extern nat context_switch;
129 extern rtsBool interrupted;
130
131 /* In Select.c */
132 extern nat timestamp;
133
134 /* Free capability list.
135  * Locks required: sched_mutex.
136  */
137 #ifdef SMP
138 extern Capability *free_capabilities;
139 extern nat n_free_capabilities;
140 #else
141 extern Capability MainCapability;
142 #endif
143
144 /* Thread queues.
145  * Locks required  : sched_mutex
146  *
147  * In GranSim we have one run/blocked_queue per PE.
148  */
149 #if defined(GRAN)
150 // run_queue_hds defined in GranSim.h
151 #else
152 extern  StgTSO *run_queue_hd, *run_queue_tl;
153 extern  StgTSO *blocked_queue_hd, *blocked_queue_tl;
154 extern  StgTSO *sleeping_queue;
155 #endif
156 /* Linked list of all threads. */
157 extern  StgTSO *all_threads;
158
159 #if defined(RTS_SUPPORTS_THREADS)
160 extern Mutex       sched_mutex;
161 extern Condition   thread_ready_cond;
162 extern Condition   gc_pending_cond;
163 #endif
164
165 /* Called by shutdown_handler(). */
166 void interruptStgRts ( void );
167
168 void raiseAsync(StgTSO *tso, StgClosure *exception);
169 nat  run_queue_len(void);
170
171 void resurrectThreads( StgTSO * );
172
173 //@node Some convenient macros, Index, Scheduler Vars and Data Types
174 //@subsection Some convenient macros
175
176 /* debugging only 
177  */
178 #ifdef DEBUG
179 void printThreadBlockage(StgTSO *tso);
180 void printThreadStatus(StgTSO *tso);
181 void printAllThreads(void);
182 #endif
183 void print_bq (StgClosure *node);
184 #if defined(PAR)
185 void print_bqe (StgBlockingQueueElement *bqe);
186 #endif
187
188 /* -----------------------------------------------------------------------------
189  * Some convenient macros...
190  */
191
192 /* END_TSO_QUEUE and friends now defined in includes/StgMiscClosures.h */
193
194 //@cindex APPEND_TO_RUN_QUEUE
195 /* Add a thread to the end of the run queue.
196  * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
197  */
198 #define APPEND_TO_RUN_QUEUE(tso)                \
199     ASSERT(tso->link == END_TSO_QUEUE);         \
200     if (run_queue_hd == END_TSO_QUEUE) {        \
201       run_queue_hd = tso;                       \
202     } else {                                    \
203       run_queue_tl->link = tso;                 \
204     }                                           \
205     run_queue_tl = tso;
206
207 //@cindex PUSH_ON_RUN_QUEUE
208 /* Push a thread on the beginning of the run queue.  Used for
209  * newly awakened threads, so they get run as soon as possible.
210  */
211 #define PUSH_ON_RUN_QUEUE(tso)                  \
212     tso->link = run_queue_hd;                   \
213       run_queue_hd = tso;                       \
214     if (run_queue_tl == END_TSO_QUEUE) {        \
215       run_queue_tl = tso;                       \
216     }
217
218 //@cindex POP_RUN_QUEUE
219 /* Pop the first thread off the runnable queue.
220  */
221 #define POP_RUN_QUEUE()                         \
222   ({ StgTSO *t = run_queue_hd;                  \
223     if (t != END_TSO_QUEUE) {                   \
224       run_queue_hd = t->link;                   \
225       t->link = END_TSO_QUEUE;                  \
226       if (run_queue_hd == END_TSO_QUEUE) {      \
227         run_queue_tl = END_TSO_QUEUE;           \
228       }                                         \
229     }                                           \
230     t;                                          \
231   })
232
233 //@cindex APPEND_TO_BLOCKED_QUEUE
234 /* Add a thread to the end of the blocked queue.
235  */
236 #define APPEND_TO_BLOCKED_QUEUE(tso)            \
237     ASSERT(tso->link == END_TSO_QUEUE);         \
238     if (blocked_queue_hd == END_TSO_QUEUE) {    \
239       blocked_queue_hd = tso;                   \
240     } else {                                    \
241       blocked_queue_tl->link = tso;             \
242     }                                           \
243     blocked_queue_tl = tso;
244
245 //@cindex THREAD_RUNNABLE
246 /* Signal that a runnable thread has become available, in
247  * case there are any waiting tasks to execute it.
248  */
249 #ifdef SMP
250 #define THREAD_RUNNABLE()                       \
251   if (free_capabilities != NULL) {              \
252      signalCondition(&thread_ready_cond);       \
253   }                                             \
254   context_switch = 1;
255 #else
256 #define THREAD_RUNNABLE()  /* nothing */
257 #endif
258
259 //@cindex EMPTY_RUN_QUEUE
260 /* Check whether the run queue is empty i.e. the PE is idle
261  */
262 #define EMPTY_RUN_QUEUE()     (run_queue_hd == END_TSO_QUEUE)
263
264 #endif /* __SCHEDULE_H__ */
265
266 //@node Index,  , Some convenient macros
267 //@subsection Index
268
269 //@index
270 //* APPEND_TO_BLOCKED_QUEUE::  @cindex\s-+APPEND_TO_BLOCKED_QUEUE
271 //* APPEND_TO_RUN_QUEUE::  @cindex\s-+APPEND_TO_RUN_QUEUE
272 //* POP_RUN_QUEUE    ::  @cindex\s-+POP_RUN_QUEUE    
273 //* PUSH_ON_RUN_QUEUE::  @cindex\s-+PUSH_ON_RUN_QUEUE
274 //* awaitEvent::  @cindex\s-+awaitEvent
275 //* awakenBlockedQueue::  @cindex\s-+awakenBlockedQueue
276 //* awaken_blocked_queue::  @cindex\s-+awaken_blocked_queue
277 //* context_switch::  @cindex\s-+context_switch
278 //* exitScheduler::  @cindex\s-+exitScheduler
279 //* gc_pending_cond::  @cindex\s-+gc_pending_cond
280 //* initScheduler::  @cindex\s-+initScheduler
281 //* raiseAsync::  @cindex\s-+raiseAsync
282 //* startTasks::  @cindex\s-+startTasks
283 //* unblockOne::  @cindex\s-+unblockOne
284 //@end index