[project @ 2000-01-14 14:06:48 by hwloidl]
[ghc-hetmet.git] / ghc / rts / Schedule.h
1 /* -----------------------------------------------------------------------------
2  * $Id: Schedule.h,v 1.15 2000/01/14 14:06:48 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)
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 // 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
177 // ?? needed -- HWL
178 void raiseAsync(StgTSO *tso, StgClosure *exception);
179 nat  run_queue_len(void);
180
181 //@node Some convenient macros, Index, Scheduler Vars and Data Types
182 //@subsection Some convenient macros
183
184 /* -----------------------------------------------------------------------------
185  * Some convenient macros...
186  */
187
188 #define END_TSO_QUEUE  ((StgTSO *)(void*)&END_TSO_QUEUE_closure)
189 #define END_CAF_LIST   ((StgCAF *)(void*)&END_TSO_QUEUE_closure)
190
191 //@cindex APPEND_TO_RUN_QUEUE
192 /* Add a thread to the end of the run queue.
193  * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
194  */
195 #define APPEND_TO_RUN_QUEUE(tso)                \
196     ASSERT(tso->link == END_TSO_QUEUE);         \
197     if (run_queue_hd == END_TSO_QUEUE) {        \
198       run_queue_hd = tso;                       \
199     } else {                                    \
200       run_queue_tl->link = tso;                 \
201     }                                           \
202     run_queue_tl = tso;
203
204 //@cindex PUSH_ON_RUN_QUEUE
205 /* Push a thread on the beginning of the run queue.  Used for
206  * newly awakened threads, so they get run as soon as possible.
207  */
208 #define PUSH_ON_RUN_QUEUE(tso)                  \
209     tso->link = run_queue_hd;                   \
210       run_queue_hd = tso;                       \
211     if (run_queue_tl == END_TSO_QUEUE) {        \
212       run_queue_tl = tso;                       \
213     }
214
215 //@cindex POP_RUN_QUEUE    
216 /* Pop the first thread off the runnable queue.
217  */
218 #define POP_RUN_QUEUE()                         \
219   ({ StgTSO *t = run_queue_hd;                  \
220     if (t != END_TSO_QUEUE) {                   \
221       run_queue_hd = t->link;                   \
222       t->link = END_TSO_QUEUE;                  \
223       if (run_queue_hd == END_TSO_QUEUE) {      \
224         run_queue_tl = END_TSO_QUEUE;           \
225       }                                         \
226     }                                           \
227     t;                                          \
228   })
229
230 //@cindex APPEND_TO_BLOCKED_QUEUE
231 /* Add a thread to the end of the blocked queue.
232  */
233 #define APPEND_TO_BLOCKED_QUEUE(tso)            \
234     ASSERT(tso->link == END_TSO_QUEUE);         \
235     if (blocked_queue_hd == END_TSO_QUEUE) {    \
236       blocked_queue_hd = tso;                   \
237     } else {                                    \
238       blocked_queue_tl->link = tso;             \
239     }                                           \
240     blocked_queue_tl = tso;
241
242 //@cindex THREAD_RUNNABLE
243 /* Signal that a runnable thread has become available, in
244  * case there are any waiting tasks to execute it.
245  */
246 #ifdef SMP
247 #define THREAD_RUNNABLE()                       \
248   if (free_capabilities != NULL) {              \
249      pthread_cond_signal(&thread_ready_cond);   \
250   }                                             \
251   context_switch = 1;
252 #else
253 #define THREAD_RUNNABLE()  /* nothing */
254 #endif
255
256 //@node Index,  , Some convenient macros
257 //@subsection Index
258
259 //@index
260 //* APPEND_TO_BLOCKED_QUEUE::  @cindex\s-+APPEND_TO_BLOCKED_QUEUE
261 //* APPEND_TO_RUN_QUEUE::  @cindex\s-+APPEND_TO_RUN_QUEUE
262 //* Capability::  @cindex\s-+Capability
263 //* POP_RUN_QUEUE    ::  @cindex\s-+POP_RUN_QUEUE    
264 //* PUSH_ON_RUN_QUEUE::  @cindex\s-+PUSH_ON_RUN_QUEUE
265 //* THREAD_RUNNABLE::  @cindex\s-+THREAD_RUNNABLE
266 //* awaitEvent::  @cindex\s-+awaitEvent
267 //* awakenBlockedQueue::  @cindex\s-+awakenBlockedQueue
268 //* awaken_blocked_queue::  @cindex\s-+awaken_blocked_queue
269 //* context_switch::  @cindex\s-+context_switch
270 //* exitScheduler::  @cindex\s-+exitScheduler
271 //* gc_pending_cond::  @cindex\s-+gc_pending_cond
272 //* initScheduler::  @cindex\s-+initScheduler
273 //* raiseAsync::  @cindex\s-+raiseAsync
274 //* sched_mutex::  @cindex\s-+sched_mutex
275 //* startTasks::  @cindex\s-+startTasks
276 //* task_info::  @cindex\s-+task_info
277 //* thread_ready_cond::  @cindex\s-+thread_ready_cond
278 //* unblockOne::  @cindex\s-+unblockOne
279 //@end index