[project @ 2000-04-14 15:18:05 by sewardj]
[ghc-hetmet.git] / ghc / rts / Schedule.h
1 /* -----------------------------------------------------------------------------
2  * $Id: Schedule.h,v 1.18 2000/04/14 15:18:07 sewardj 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 //@menu
12 //* Scheduler Functions::       
13 //* Scheduler Vars and Data Types::  
14 //* Some convenient macros::    
15 //* Index::                     
16 //@end menu
17
18 //@node Scheduler Functions, Scheduler Vars and Data Types
19 //@subsection Scheduler Functions
20
21 //@cindex initScheduler
22 //@cindex exitScheduler
23 //@cindex startTasks
24 /* initScheduler(), exitScheduler(), startTasks()
25  * 
26  * Called from STG :  no
27  * Locks assumed   :  none
28  */
29 void initScheduler( void );
30 void exitScheduler( void );
31 #ifdef SMP
32 void startTasks( void );
33 #endif
34
35
36 //@cindex awakenBlockedQueue
37 /* awakenBlockedQueue()
38  *
39  * Takes a pointer to the beginning of a blocked TSO queue, and
40  * wakes up the entire queue.
41  *
42  * Called from STG :  yes
43  * Locks assumed   :  none
44  */
45 #if defined(GRAN)
46 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
47 #elif defined(PAR)
48 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
49 #else
50 void awakenBlockedQueue(StgTSO *tso);
51 #endif
52
53 //@cindex unblockOne
54 /* unblockOne()
55  *
56  * Takes a pointer to the beginning of a blocked TSO queue, and
57  * removes the first thread, placing it on the runnable queue.
58  *
59  * Called from STG : yes
60  * Locks assumed   : none
61  */
62 #if defined(GRAN) || defined(PAR)
63 StgBlockingQueueElement *unblockOne(StgBlockingQueueElement *bqe, StgClosure *node);
64 #else
65 StgTSO *unblockOne(StgTSO *tso);
66 #endif
67
68 //@cindex raiseAsync
69 /* raiseAsync()
70  *
71  * Raises an exception asynchronously in the specified thread.
72  *
73  * Called from STG :  yes
74  * Locks assumed   :  none
75  */
76 void raiseAsync(StgTSO *tso, StgClosure *exception);
77
78 //@cindex awaitEvent
79 /* awaitEvent()
80  *
81  * Raises an exception asynchronously in the specified thread.
82  *
83  * Called from STG :  NO
84  * Locks assumed   :  sched_mutex
85  */
86 void awaitEvent(rtsBool wait);  /* In Select.c */
87
88 // ToDo: check whether all fcts below are used in the SMP version, too
89 //@cindex awaken_blocked_queue
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 //@node Scheduler Vars and Data Types, Some convenient macros, Scheduler Functions
106 //@subsection Scheduler Vars and Data Types
107
108 //@cindex context_switch
109 /* Context switch flag.
110  * Locks required  : sched_mutex
111  */
112 extern nat context_switch;
113 extern rtsBool interrupted;
114
115 extern  nat ticks_since_select;
116
117 //@cindex Capability
118 /* Capability type
119  */
120 typedef StgRegTable Capability;
121
122 /* Free capability list.
123  * Locks required: sched_mutex.
124  */
125 #ifdef SMP
126 extern Capability *free_capabilities;
127 extern nat n_free_capabilities;
128 #else
129 extern Capability MainRegTable;
130 #endif
131
132 /* Thread queues.
133  * Locks required  : sched_mutex
134  *
135  * In GranSim we have one run/blocked_queue per PE.
136  */
137 #if defined(GRAN)
138 // run_queue_hds defined in GranSim.h
139 #else
140 extern  StgTSO *run_queue_hd, *run_queue_tl;
141 extern  StgTSO *blocked_queue_hd, *blocked_queue_tl;
142 #endif
143 /* Linked list of all threads. */
144 extern  StgTSO *all_threads;
145
146 #ifdef SMP
147 //@cindex sched_mutex
148 //@cindex thread_ready_cond
149 //@cindex gc_pending_cond
150 extern pthread_mutex_t sched_mutex;
151 extern pthread_cond_t  thread_ready_cond;
152 extern pthread_cond_t  gc_pending_cond;
153 #endif
154
155 //@cindex task_info
156 #ifdef SMP
157 typedef struct {
158   pthread_t id;
159   double    elapsedtimestart;
160   double    mut_time;
161   double    mut_etime;
162   double    gc_time;
163   double    gc_etime;
164 } task_info;
165
166 extern task_info *task_ids;
167 #endif
168
169 /* Needed by Hugs.
170  */
171 void interruptStgRts ( void );
172
173 void raiseAsync(StgTSO *tso, StgClosure *exception);
174 nat  run_queue_len(void);
175
176 void resurrectThreads( StgTSO * );
177
178 //@node Some convenient macros, Index, Scheduler Vars and Data Types
179 //@subsection Some convenient macros
180
181 /* debugging only 
182  */
183 #ifdef DEBUG
184 void printThreadBlockage(StgTSO *tso);
185 void printThreadStatus(StgTSO *tso);
186 void printAllThreads(void);
187 #endif
188 void print_bq (StgClosure *node);
189
190 /* -----------------------------------------------------------------------------
191  * Some convenient macros...
192  */
193
194 /* this is the NIL ptr for a TSO queue (e.g. runnable queue) */
195 #define END_TSO_QUEUE  ((StgTSO *)(void*)&END_TSO_QUEUE_closure)
196 /* this is the NIL ptr for a list CAFs */
197 #define END_ECAF_LIST   ((StgCAF *)(void*)&END_TSO_QUEUE_closure)
198 #if defined(PAR) || defined(GRAN)
199 /* this is the NIL ptr for a blocking queue */
200 # define END_BQ_QUEUE  ((StgBlockingQueueElement *)(void*)&END_TSO_QUEUE_closure)
201 /* this is the NIL ptr for a blocked fetch queue (as in PendingFetches in GUM) */
202 # define END_BF_QUEUE  ((StgBlockedFetch *)(void*)&END_TSO_QUEUE_closure)
203 #endif
204
205 //@cindex APPEND_TO_RUN_QUEUE
206 /* Add a thread to the end of the run queue.
207  * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
208  */
209 #define APPEND_TO_RUN_QUEUE(tso)                \
210     ASSERT(tso->link == END_TSO_QUEUE);         \
211     if (run_queue_hd == END_TSO_QUEUE) {        \
212       run_queue_hd = tso;                       \
213     } else {                                    \
214       run_queue_tl->link = tso;                 \
215     }                                           \
216     run_queue_tl = tso;
217
218 //@cindex PUSH_ON_RUN_QUEUE
219 /* Push a thread on the beginning of the run queue.  Used for
220  * newly awakened threads, so they get run as soon as possible.
221  */
222 #define PUSH_ON_RUN_QUEUE(tso)                  \
223     tso->link = run_queue_hd;                   \
224       run_queue_hd = tso;                       \
225     if (run_queue_tl == END_TSO_QUEUE) {        \
226       run_queue_tl = tso;                       \
227     }
228
229 //@cindex POP_RUN_QUEUE
230 /* Pop the first thread off the runnable queue.
231  */
232 #define POP_RUN_QUEUE()                         \
233   ({ StgTSO *t = run_queue_hd;                  \
234     if (t != END_TSO_QUEUE) {                   \
235       run_queue_hd = t->link;                   \
236       t->link = END_TSO_QUEUE;                  \
237       if (run_queue_hd == END_TSO_QUEUE) {      \
238         run_queue_tl = END_TSO_QUEUE;           \
239       }                                         \
240     }                                           \
241     t;                                          \
242   })
243
244 //@cindex APPEND_TO_BLOCKED_QUEUE
245 /* Add a thread to the end of the blocked queue.
246  */
247 #define APPEND_TO_BLOCKED_QUEUE(tso)            \
248     ASSERT(tso->link == END_TSO_QUEUE);         \
249     if (blocked_queue_hd == END_TSO_QUEUE) {    \
250       blocked_queue_hd = tso;                   \
251     } else {                                    \
252       blocked_queue_tl->link = tso;             \
253     }                                           \
254     blocked_queue_tl = tso;
255
256 //@cindex THREAD_RUNNABLE
257 /* Signal that a runnable thread has become available, in
258  * case there are any waiting tasks to execute it.
259  */
260 #ifdef SMP
261 #define THREAD_RUNNABLE()                       \
262   if (free_capabilities != NULL) {              \
263      pthread_cond_signal(&thread_ready_cond);   \
264   }                                             \
265   context_switch = 1;
266 #else
267 #define THREAD_RUNNABLE()  /* nothing */
268 #endif
269
270 //@node Index,  , Some convenient macros
271 //@subsection Index
272
273 //@index
274 //* APPEND_TO_BLOCKED_QUEUE::  @cindex\s-+APPEND_TO_BLOCKED_QUEUE
275 //* APPEND_TO_RUN_QUEUE::  @cindex\s-+APPEND_TO_RUN_QUEUE
276 //* Capability::  @cindex\s-+Capability
277 //* POP_RUN_QUEUE    ::  @cindex\s-+POP_RUN_QUEUE    
278 //* PUSH_ON_RUN_QUEUE::  @cindex\s-+PUSH_ON_RUN_QUEUE
279 //* THREAD_RUNNABLE::  @cindex\s-+THREAD_RUNNABLE
280 //* awaitEvent::  @cindex\s-+awaitEvent
281 //* awakenBlockedQueue::  @cindex\s-+awakenBlockedQueue
282 //* awaken_blocked_queue::  @cindex\s-+awaken_blocked_queue
283 //* context_switch::  @cindex\s-+context_switch
284 //* exitScheduler::  @cindex\s-+exitScheduler
285 //* gc_pending_cond::  @cindex\s-+gc_pending_cond
286 //* initScheduler::  @cindex\s-+initScheduler
287 //* raiseAsync::  @cindex\s-+raiseAsync
288 //* sched_mutex::  @cindex\s-+sched_mutex
289 //* startTasks::  @cindex\s-+startTasks
290 //* task_info::  @cindex\s-+task_info
291 //* thread_ready_cond::  @cindex\s-+thread_ready_cond
292 //* unblockOne::  @cindex\s-+unblockOne
293 //@end index