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