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