[project @ 2000-03-16 17:27:12 by simonmar]
[ghc-hetmet.git] / ghc / rts / Schedule.h
1 /* -----------------------------------------------------------------------------
2  * $Id: Schedule.h,v 1.16 2000/03/16 17:27:13 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)
62 StgTSO *unblockOne(StgTSO *tso, StgClosure *node);
63 #elif defined(PAR)
64 StgTSO *unblockOne(StgTSO *tso, StgClosure *node);
65 #else
66 StgTSO *unblockOne(StgTSO *tso);
67 #endif
68
69 //@cindex raiseAsync
70 /* raiseAsync()
71  *
72  * Raises an exception asynchronously in the specified thread.
73  *
74  * Called from STG :  yes
75  * Locks assumed   :  none
76  */
77 void raiseAsync(StgTSO *tso, StgClosure *exception);
78
79 //@cindex awaitEvent
80 /* awaitEvent()
81  *
82  * Raises an exception asynchronously in the specified thread.
83  *
84  * Called from STG :  NO
85  * Locks assumed   :  sched_mutex
86  */
87 void awaitEvent(rtsBool wait);  /* In Select.c */
88
89 // ToDo: check whether all fcts below are used in the SMP version, too
90 //@cindex awaken_blocked_queue
91 #if defined(GRAN)
92 void    awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
93 void    unlink_from_bq(StgTSO* tso, StgClosure* node);
94 void    initThread(StgTSO *tso, nat stack_size, StgInt pri);
95 #elif defined(PAR)
96 nat     run_queue_len(void);
97 void    awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
98 void    initThread(StgTSO *tso, nat stack_size);
99 #else
100 char   *info_type(StgClosure *closure);    // dummy
101 char   *info_type_by_ip(StgInfoTable *ip); // dummy
102 void    awaken_blocked_queue(StgTSO *q);
103 void    initThread(StgTSO *tso, nat stack_size);
104 #endif
105
106 //@node Scheduler Vars and Data Types, Some convenient macros, Scheduler Functions
107 //@subsection Scheduler Vars and Data Types
108
109 //@cindex context_switch
110 /* Context switch flag.
111  * Locks required  : sched_mutex
112  */
113 extern nat context_switch;
114 extern rtsBool interrupted;
115
116 extern  nat ticks_since_select;
117
118 //@cindex Capability
119 /* Capability type
120  */
121 typedef StgRegTable Capability;
122
123 /* Free capability list.
124  * Locks required: sched_mutex.
125  */
126 #ifdef SMP
127 extern Capability *free_capabilities;
128 extern nat n_free_capabilities;
129 #else
130 extern Capability MainRegTable;
131 #endif
132
133 /* Thread queues.
134  * Locks required  : sched_mutex
135  */
136 extern  StgTSO *run_queue_hd, *run_queue_tl;
137 extern  StgTSO *blocked_queue_hd, *blocked_queue_tl;
138 extern  StgTSO *all_threads;
139
140 #ifdef SMP
141 //@cindex sched_mutex
142 //@cindex thread_ready_cond
143 //@cindex gc_pending_cond
144 extern pthread_mutex_t sched_mutex;
145 extern pthread_cond_t  thread_ready_cond;
146 extern pthread_cond_t  gc_pending_cond;
147 #endif
148
149 //@cindex task_info
150 #ifdef SMP
151 typedef struct {
152   pthread_t id;
153   double    elapsedtimestart;
154   double    mut_time;
155   double    mut_etime;
156   double    gc_time;
157   double    gc_etime;
158 } task_info;
159
160 extern task_info *task_ids;
161 #endif
162
163 #if !defined(GRAN)
164 extern  StgTSO *run_queue_hd, *run_queue_tl;
165 extern  StgTSO *blocked_queue_hd, *blocked_queue_tl;
166 #endif
167
168 /* Needed by Hugs.
169  */
170 void interruptStgRts ( void );
171
172 // ?? needed -- HWL
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 #define END_TSO_QUEUE  ((StgTSO *)(void*)&END_TSO_QUEUE_closure)
195 #define END_CAF_LIST   ((StgCAF *)(void*)&END_TSO_QUEUE_closure)
196
197 //@cindex APPEND_TO_RUN_QUEUE
198 /* Add a thread to the end of the run queue.
199  * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
200  */
201 #define APPEND_TO_RUN_QUEUE(tso)                \
202     ASSERT(tso->link == END_TSO_QUEUE);         \
203     if (run_queue_hd == END_TSO_QUEUE) {        \
204       run_queue_hd = tso;                       \
205     } else {                                    \
206       run_queue_tl->link = tso;                 \
207     }                                           \
208     run_queue_tl = tso;
209
210 //@cindex PUSH_ON_RUN_QUEUE
211 /* Push a thread on the beginning of the run queue.  Used for
212  * newly awakened threads, so they get run as soon as possible.
213  */
214 #define PUSH_ON_RUN_QUEUE(tso)                  \
215     tso->link = run_queue_hd;                   \
216       run_queue_hd = tso;                       \
217     if (run_queue_tl == END_TSO_QUEUE) {        \
218       run_queue_tl = tso;                       \
219     }
220
221 //@cindex POP_RUN_QUEUE    
222 /* Pop the first thread off the runnable queue.
223  */
224 #define POP_RUN_QUEUE()                         \
225   ({ StgTSO *t = run_queue_hd;                  \
226     if (t != END_TSO_QUEUE) {                   \
227       run_queue_hd = t->link;                   \
228       t->link = END_TSO_QUEUE;                  \
229       if (run_queue_hd == END_TSO_QUEUE) {      \
230         run_queue_tl = END_TSO_QUEUE;           \
231       }                                         \
232     }                                           \
233     t;                                          \
234   })
235
236 //@cindex APPEND_TO_BLOCKED_QUEUE
237 /* Add a thread to the end of the blocked queue.
238  */
239 #define APPEND_TO_BLOCKED_QUEUE(tso)            \
240     ASSERT(tso->link == END_TSO_QUEUE);         \
241     if (blocked_queue_hd == END_TSO_QUEUE) {    \
242       blocked_queue_hd = tso;                   \
243     } else {                                    \
244       blocked_queue_tl->link = tso;             \
245     }                                           \
246     blocked_queue_tl = tso;
247
248 //@cindex THREAD_RUNNABLE
249 /* Signal that a runnable thread has become available, in
250  * case there are any waiting tasks to execute it.
251  */
252 #ifdef SMP
253 #define THREAD_RUNNABLE()                       \
254   if (free_capabilities != NULL) {              \
255      pthread_cond_signal(&thread_ready_cond);   \
256   }                                             \
257   context_switch = 1;
258 #else
259 #define THREAD_RUNNABLE()  /* nothing */
260 #endif
261
262 //@node Index,  , Some convenient macros
263 //@subsection Index
264
265 //@index
266 //* APPEND_TO_BLOCKED_QUEUE::  @cindex\s-+APPEND_TO_BLOCKED_QUEUE
267 //* APPEND_TO_RUN_QUEUE::  @cindex\s-+APPEND_TO_RUN_QUEUE
268 //* Capability::  @cindex\s-+Capability
269 //* POP_RUN_QUEUE    ::  @cindex\s-+POP_RUN_QUEUE    
270 //* PUSH_ON_RUN_QUEUE::  @cindex\s-+PUSH_ON_RUN_QUEUE
271 //* THREAD_RUNNABLE::  @cindex\s-+THREAD_RUNNABLE
272 //* awaitEvent::  @cindex\s-+awaitEvent
273 //* awakenBlockedQueue::  @cindex\s-+awakenBlockedQueue
274 //* awaken_blocked_queue::  @cindex\s-+awaken_blocked_queue
275 //* context_switch::  @cindex\s-+context_switch
276 //* exitScheduler::  @cindex\s-+exitScheduler
277 //* gc_pending_cond::  @cindex\s-+gc_pending_cond
278 //* initScheduler::  @cindex\s-+initScheduler
279 //* raiseAsync::  @cindex\s-+raiseAsync
280 //* sched_mutex::  @cindex\s-+sched_mutex
281 //* startTasks::  @cindex\s-+startTasks
282 //* task_info::  @cindex\s-+task_info
283 //* thread_ready_cond::  @cindex\s-+thread_ready_cond
284 //* unblockOne::  @cindex\s-+unblockOne
285 //@end index