[project @ 2000-01-13 14:33:57 by hwloidl]
[ghc-hetmet.git] / ghc / rts / Schedule.h
1 /* -----------------------------------------------------------------------------
2  * $Id: Schedule.h,v 1.13 2000/01/13 14:34:05 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 # error FixME
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 # error FixME
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 // debugging only
107 #ifdef DEBUG
108 extern void printThreadBlockage(StgTSO *tso);
109 #endif
110 void    print_bq (StgClosure *node);
111
112 //@node Scheduler Vars and Data Types, Some convenient macros, Scheduler Functions
113 //@subsection Scheduler Vars and Data Types
114
115 //@cindex context_switch
116 /* Context switch flag.
117  * Locks required  : sched_mutex
118  */
119 extern nat context_switch;
120 extern rtsBool interrupted;
121
122 extern  nat ticks_since_select;
123
124 //@cindex Capability
125 /* Capability type
126  */
127 typedef StgRegTable Capability;
128
129 /* Free capability list.
130  * Locks required: sched_mutex.
131  */
132 #ifdef SMP
133 extern Capability *free_capabilities;
134 extern nat n_free_capabilities;
135 #else
136 extern Capability MainRegTable;
137 #endif
138
139 /* Thread queues.
140  * Locks required  : sched_mutex
141  */
142 extern  StgTSO *run_queue_hd, *run_queue_tl;
143 extern  StgTSO *blocked_queue_hd, *blocked_queue_tl;
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 #if !defined(GRAN)
169 extern  StgTSO *run_queue_hd, *run_queue_tl;
170 extern  StgTSO *blocked_queue_hd, *blocked_queue_tl;
171 #endif
172
173 /* Needed by Hugs.
174  */
175 void interruptStgRts ( void );
176 // ?? needed -- HWL
177 void raiseAsync(StgTSO *tso, StgClosure *exception);
178
179 //@node Some convenient macros, Index, Scheduler Vars and Data Types
180 //@subsection Some convenient macros
181
182 /* -----------------------------------------------------------------------------
183  * Some convenient macros...
184  */
185
186 #define END_TSO_QUEUE  ((StgTSO *)(void*)&END_TSO_QUEUE_closure)
187 #define END_CAF_LIST   ((StgCAF *)(void*)&END_TSO_QUEUE_closure)
188
189 //@cindex APPEND_TO_RUN_QUEUE
190 /* Add a thread to the end of the run queue.
191  * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
192  */
193 #define APPEND_TO_RUN_QUEUE(tso)                \
194     ASSERT(tso->link == END_TSO_QUEUE);         \
195     if (run_queue_hd == END_TSO_QUEUE) {        \
196       run_queue_hd = tso;                       \
197     } else {                                    \
198       run_queue_tl->link = tso;                 \
199     }                                           \
200     run_queue_tl = tso;
201
202 //@cindex PUSH_ON_RUN_QUEUE
203 /* Push a thread on the beginning of the run queue.  Used for
204  * newly awakened threads, so they get run as soon as possible.
205  */
206 #define PUSH_ON_RUN_QUEUE(tso)                  \
207     tso->link = run_queue_hd;                   \
208       run_queue_hd = tso;                       \
209     if (run_queue_tl == END_TSO_QUEUE) {        \
210       run_queue_tl = tso;                       \
211     }
212
213 //@cindex POP_RUN_QUEUE    
214 /* Pop the first thread off the runnable queue.
215  */
216 #define POP_RUN_QUEUE()                         \
217   ({ StgTSO *t = run_queue_hd;                  \
218     if (t != END_TSO_QUEUE) {                   \
219       run_queue_hd = t->link;                   \
220       t->link = END_TSO_QUEUE;                  \
221       if (run_queue_hd == END_TSO_QUEUE) {      \
222         run_queue_tl = END_TSO_QUEUE;           \
223       }                                         \
224     }                                           \
225     t;                                          \
226   })
227
228 //@cindex APPEND_TO_BLOCKED_QUEUE
229 /* Add a thread to the end of the blocked queue.
230  */
231 #define APPEND_TO_BLOCKED_QUEUE(tso)            \
232     ASSERT(tso->link == END_TSO_QUEUE);         \
233     if (blocked_queue_hd == END_TSO_QUEUE) {    \
234       blocked_queue_hd = tso;                   \
235     } else {                                    \
236       blocked_queue_tl->link = tso;             \
237     }                                           \
238     blocked_queue_tl = tso;
239
240 //@cindex THREAD_RUNNABLE
241 /* Signal that a runnable thread has become available, in
242  * case there are any waiting tasks to execute it.
243  */
244 #ifdef SMP
245 #define THREAD_RUNNABLE()                       \
246   if (free_capabilities != NULL) {              \
247      pthread_cond_signal(&thread_ready_cond);   \
248   }                                             \
249   context_switch = 1;
250 #else
251 #define THREAD_RUNNABLE()  /* nothing */
252 #endif
253
254 //@node Index,  , Some convenient macros
255 //@subsection Index
256
257 //@index
258 //* APPEND_TO_BLOCKED_QUEUE::  @cindex\s-+APPEND_TO_BLOCKED_QUEUE
259 //* APPEND_TO_RUN_QUEUE::  @cindex\s-+APPEND_TO_RUN_QUEUE
260 //* Capability::  @cindex\s-+Capability
261 //* POP_RUN_QUEUE    ::  @cindex\s-+POP_RUN_QUEUE    
262 //* PUSH_ON_RUN_QUEUE::  @cindex\s-+PUSH_ON_RUN_QUEUE
263 //* THREAD_RUNNABLE::  @cindex\s-+THREAD_RUNNABLE
264 //* awaitEvent::  @cindex\s-+awaitEvent
265 //* awakenBlockedQueue::  @cindex\s-+awakenBlockedQueue
266 //* awaken_blocked_queue::  @cindex\s-+awaken_blocked_queue
267 //* context_switch::  @cindex\s-+context_switch
268 //* exitScheduler::  @cindex\s-+exitScheduler
269 //* gc_pending_cond::  @cindex\s-+gc_pending_cond
270 //* initScheduler::  @cindex\s-+initScheduler
271 //* raiseAsync::  @cindex\s-+raiseAsync
272 //* sched_mutex::  @cindex\s-+sched_mutex
273 //* startTasks::  @cindex\s-+startTasks
274 //* task_info::  @cindex\s-+task_info
275 //* thread_ready_cond::  @cindex\s-+thread_ready_cond
276 //* unblockOne::  @cindex\s-+unblockOne
277 //@end index