00b4de17977f02ca76ad7617192e00d74e63493f
[ghc-hetmet.git] / ghc / rts / Schedule.h
1 /* -----------------------------------------------------------------------------
2  * $Id: Schedule.h,v 1.22 2001/03/22 03:51:10 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 /* wakeUpSleepingThreads(nat ticks)
88  *
89  * Wakes up any sleeping threads whose timers have expired.
90  *
91  * Called from STG :  NO
92  * Locks assumed   :  sched_mutex
93  */
94 rtsBool wakeUpSleepingThreads(nat);  /* In Select.c */
95
96 // ToDo: check whether all fcts below are used in the SMP version, too
97 //@cindex awaken_blocked_queue
98 #if defined(GRAN)
99 void    awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
100 void    unlink_from_bq(StgTSO* tso, StgClosure* node);
101 void    initThread(StgTSO *tso, nat stack_size, StgInt pri);
102 #elif defined(PAR)
103 nat     run_queue_len(void);
104 void    awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
105 void    initThread(StgTSO *tso, nat stack_size);
106 #else
107 char   *info_type(StgClosure *closure);    // dummy
108 char   *info_type_by_ip(StgInfoTable *ip); // dummy
109 void    awaken_blocked_queue(StgTSO *q);
110 void    initThread(StgTSO *tso, nat stack_size);
111 #endif
112
113 //@node Scheduler Vars and Data Types, Some convenient macros, Scheduler Functions
114 //@subsection Scheduler Vars and Data Types
115
116 //@cindex context_switch
117 /* Context switch flag.
118  * Locks required  : sched_mutex
119  */
120 extern nat context_switch;
121 extern rtsBool interrupted;
122
123 /* In Select.c */
124 extern nat timestamp;
125 extern nat ticks_since_timestamp;
126
127 //@cindex Capability
128 /* Capability type
129  */
130 typedef StgRegTable Capability;
131
132 /* Free capability list.
133  * Locks required: sched_mutex.
134  */
135 #ifdef SMP
136 extern Capability *free_capabilities;
137 extern nat n_free_capabilities;
138 #else
139 extern Capability MainRegTable;
140 #endif
141
142 /* Thread queues.
143  * Locks required  : sched_mutex
144  *
145  * In GranSim we have one run/blocked_queue per PE.
146  */
147 #if defined(GRAN)
148 // run_queue_hds defined in GranSim.h
149 #else
150 extern  StgTSO *run_queue_hd, *run_queue_tl;
151 extern  StgTSO *blocked_queue_hd, *blocked_queue_tl;
152 extern  StgTSO *sleeping_queue;
153 #endif
154 /* Linked list of all threads. */
155 extern  StgTSO *all_threads;
156
157 #ifdef SMP
158 //@cindex sched_mutex
159 //@cindex thread_ready_cond
160 //@cindex gc_pending_cond
161 extern pthread_mutex_t sched_mutex;
162 extern pthread_cond_t  thread_ready_cond;
163 extern pthread_cond_t  gc_pending_cond;
164 #endif
165
166 //@cindex task_info
167 #ifdef SMP
168 typedef struct {
169   pthread_t id;
170   double    elapsedtimestart;
171   double    mut_time;
172   double    mut_etime;
173   double    gc_time;
174   double    gc_etime;
175 } task_info;
176
177 extern task_info *task_ids;
178 #endif
179
180 /* Needed by Hugs.
181  */
182 void interruptStgRts ( void );
183
184 void raiseAsync(StgTSO *tso, StgClosure *exception);
185 nat  run_queue_len(void);
186
187 void resurrectThreads( StgTSO * );
188
189 //@node Some convenient macros, Index, Scheduler Vars and Data Types
190 //@subsection Some convenient macros
191
192 /* debugging only 
193  */
194 #ifdef DEBUG
195 void printThreadBlockage(StgTSO *tso);
196 void printThreadStatus(StgTSO *tso);
197 void printAllThreads(void);
198 #endif
199 void print_bq (StgClosure *node);
200 #if defined(PAR)
201 void print_bqe (StgBlockingQueueElement *bqe);
202 #endif
203
204 /* -----------------------------------------------------------------------------
205  * Some convenient macros...
206  */
207
208 /* END_TSO_QUEUE and friends now defined in includes/StgMiscClosures.h */
209
210 //@cindex APPEND_TO_RUN_QUEUE
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 //@cindex PUSH_ON_RUN_QUEUE
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 //@cindex POP_RUN_QUEUE
235 /* Pop the first thread off the runnable queue.
236  */
237 #define POP_RUN_QUEUE()                         \
238   ({ StgTSO *t = run_queue_hd;                  \
239     if (t != END_TSO_QUEUE) {                   \
240       run_queue_hd = t->link;                   \
241       t->link = END_TSO_QUEUE;                  \
242       if (run_queue_hd == END_TSO_QUEUE) {      \
243         run_queue_tl = END_TSO_QUEUE;           \
244       }                                         \
245     }                                           \
246     t;                                          \
247   })
248
249 //@cindex APPEND_TO_BLOCKED_QUEUE
250 /* Add a thread to the end of the blocked queue.
251  */
252 #define APPEND_TO_BLOCKED_QUEUE(tso)            \
253     ASSERT(tso->link == END_TSO_QUEUE);         \
254     if (blocked_queue_hd == END_TSO_QUEUE) {    \
255       blocked_queue_hd = tso;                   \
256     } else {                                    \
257       blocked_queue_tl->link = tso;             \
258     }                                           \
259     blocked_queue_tl = tso;
260
261 //@cindex THREAD_RUNNABLE
262 /* Signal that a runnable thread has become available, in
263  * case there are any waiting tasks to execute it.
264  */
265 #ifdef SMP
266 #define THREAD_RUNNABLE()                       \
267   if (free_capabilities != NULL) {              \
268      pthread_cond_signal(&thread_ready_cond);   \
269   }                                             \
270   context_switch = 1;
271 #else
272 #define THREAD_RUNNABLE()  /* nothing */
273 #endif
274
275 //@cindex EMPTY_RUN_QUEUE
276 /* Check whether the run queue is empty i.e. the PE is idle
277  */
278 #define EMPTY_RUN_QUEUE()     (run_queue_hd == END_TSO_QUEUE)
279
280 //@node Index,  , Some convenient macros
281 //@subsection Index
282
283 //@index
284 //* APPEND_TO_BLOCKED_QUEUE::  @cindex\s-+APPEND_TO_BLOCKED_QUEUE
285 //* APPEND_TO_RUN_QUEUE::  @cindex\s-+APPEND_TO_RUN_QUEUE
286 //* Capability::  @cindex\s-+Capability
287 //* POP_RUN_QUEUE    ::  @cindex\s-+POP_RUN_QUEUE    
288 //* PUSH_ON_RUN_QUEUE::  @cindex\s-+PUSH_ON_RUN_QUEUE
289 //* THREAD_RUNNABLE::  @cindex\s-+THREAD_RUNNABLE
290 //* awaitEvent::  @cindex\s-+awaitEvent
291 //* awakenBlockedQueue::  @cindex\s-+awakenBlockedQueue
292 //* awaken_blocked_queue::  @cindex\s-+awaken_blocked_queue
293 //* context_switch::  @cindex\s-+context_switch
294 //* exitScheduler::  @cindex\s-+exitScheduler
295 //* gc_pending_cond::  @cindex\s-+gc_pending_cond
296 //* initScheduler::  @cindex\s-+initScheduler
297 //* raiseAsync::  @cindex\s-+raiseAsync
298 //* sched_mutex::  @cindex\s-+sched_mutex
299 //* startTasks::  @cindex\s-+startTasks
300 //* task_info::  @cindex\s-+task_info
301 //* thread_ready_cond::  @cindex\s-+thread_ready_cond
302 //* unblockOne::  @cindex\s-+unblockOne
303 //@end index