1 /* -----------------------------------------------------------------------------
2 * $Id: Schedule.h,v 1.18 2000/04/14 15:18:07 sewardj Exp $
4 * (c) The GHC Team 1998-1999
6 * Prototypes for functions in Schedule.c
7 * (RTS internal scheduler interface)
9 * -------------------------------------------------------------------------*/
12 //* Scheduler Functions::
13 //* Scheduler Vars and Data Types::
14 //* Some convenient macros::
18 //@node Scheduler Functions, Scheduler Vars and Data Types
19 //@subsection Scheduler Functions
21 //@cindex initScheduler
22 //@cindex exitScheduler
24 /* initScheduler(), exitScheduler(), startTasks()
26 * Called from STG : no
27 * Locks assumed : none
29 void initScheduler( void );
30 void exitScheduler( void );
32 void startTasks( void );
36 //@cindex awakenBlockedQueue
37 /* awakenBlockedQueue()
39 * Takes a pointer to the beginning of a blocked TSO queue, and
40 * wakes up the entire queue.
42 * Called from STG : yes
43 * Locks assumed : none
46 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
48 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
50 void awakenBlockedQueue(StgTSO *tso);
56 * Takes a pointer to the beginning of a blocked TSO queue, and
57 * removes the first thread, placing it on the runnable queue.
59 * Called from STG : yes
60 * Locks assumed : none
62 #if defined(GRAN) || defined(PAR)
63 StgBlockingQueueElement *unblockOne(StgBlockingQueueElement *bqe, StgClosure *node);
65 StgTSO *unblockOne(StgTSO *tso);
71 * Raises an exception asynchronously in the specified thread.
73 * Called from STG : yes
74 * Locks assumed : none
76 void raiseAsync(StgTSO *tso, StgClosure *exception);
81 * Raises an exception asynchronously in the specified thread.
83 * Called from STG : NO
84 * Locks assumed : sched_mutex
86 void awaitEvent(rtsBool wait); /* In Select.c */
88 // ToDo: check whether all fcts below are used in the SMP version, too
89 //@cindex awaken_blocked_queue
91 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
92 void unlink_from_bq(StgTSO* tso, StgClosure* node);
93 void initThread(StgTSO *tso, nat stack_size, StgInt pri);
95 nat run_queue_len(void);
96 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
97 void initThread(StgTSO *tso, nat stack_size);
99 char *info_type(StgClosure *closure); // dummy
100 char *info_type_by_ip(StgInfoTable *ip); // dummy
101 void awaken_blocked_queue(StgTSO *q);
102 void initThread(StgTSO *tso, nat stack_size);
105 //@node Scheduler Vars and Data Types, Some convenient macros, Scheduler Functions
106 //@subsection Scheduler Vars and Data Types
108 //@cindex context_switch
109 /* Context switch flag.
110 * Locks required : sched_mutex
112 extern nat context_switch;
113 extern rtsBool interrupted;
115 extern nat ticks_since_select;
120 typedef StgRegTable Capability;
122 /* Free capability list.
123 * Locks required: sched_mutex.
126 extern Capability *free_capabilities;
127 extern nat n_free_capabilities;
129 extern Capability MainRegTable;
133 * Locks required : sched_mutex
135 * In GranSim we have one run/blocked_queue per PE.
138 // run_queue_hds defined in GranSim.h
140 extern StgTSO *run_queue_hd, *run_queue_tl;
141 extern StgTSO *blocked_queue_hd, *blocked_queue_tl;
143 /* Linked list of all threads. */
144 extern StgTSO *all_threads;
147 //@cindex sched_mutex
148 //@cindex thread_ready_cond
149 //@cindex gc_pending_cond
150 extern pthread_mutex_t sched_mutex;
151 extern pthread_cond_t thread_ready_cond;
152 extern pthread_cond_t gc_pending_cond;
159 double elapsedtimestart;
166 extern task_info *task_ids;
171 void interruptStgRts ( void );
173 void raiseAsync(StgTSO *tso, StgClosure *exception);
174 nat run_queue_len(void);
176 void resurrectThreads( StgTSO * );
178 //@node Some convenient macros, Index, Scheduler Vars and Data Types
179 //@subsection Some convenient macros
184 void printThreadBlockage(StgTSO *tso);
185 void printThreadStatus(StgTSO *tso);
186 void printAllThreads(void);
188 void print_bq (StgClosure *node);
190 /* -----------------------------------------------------------------------------
191 * Some convenient macros...
194 /* this is the NIL ptr for a TSO queue (e.g. runnable queue) */
195 #define END_TSO_QUEUE ((StgTSO *)(void*)&END_TSO_QUEUE_closure)
196 /* this is the NIL ptr for a list CAFs */
197 #define END_ECAF_LIST ((StgCAF *)(void*)&END_TSO_QUEUE_closure)
198 #if defined(PAR) || defined(GRAN)
199 /* this is the NIL ptr for a blocking queue */
200 # define END_BQ_QUEUE ((StgBlockingQueueElement *)(void*)&END_TSO_QUEUE_closure)
201 /* this is the NIL ptr for a blocked fetch queue (as in PendingFetches in GUM) */
202 # define END_BF_QUEUE ((StgBlockedFetch *)(void*)&END_TSO_QUEUE_closure)
205 //@cindex APPEND_TO_RUN_QUEUE
206 /* Add a thread to the end of the run queue.
207 * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
209 #define APPEND_TO_RUN_QUEUE(tso) \
210 ASSERT(tso->link == END_TSO_QUEUE); \
211 if (run_queue_hd == END_TSO_QUEUE) { \
212 run_queue_hd = tso; \
214 run_queue_tl->link = tso; \
218 //@cindex PUSH_ON_RUN_QUEUE
219 /* Push a thread on the beginning of the run queue. Used for
220 * newly awakened threads, so they get run as soon as possible.
222 #define PUSH_ON_RUN_QUEUE(tso) \
223 tso->link = run_queue_hd; \
224 run_queue_hd = tso; \
225 if (run_queue_tl == END_TSO_QUEUE) { \
226 run_queue_tl = tso; \
229 //@cindex POP_RUN_QUEUE
230 /* Pop the first thread off the runnable queue.
232 #define POP_RUN_QUEUE() \
233 ({ StgTSO *t = run_queue_hd; \
234 if (t != END_TSO_QUEUE) { \
235 run_queue_hd = t->link; \
236 t->link = END_TSO_QUEUE; \
237 if (run_queue_hd == END_TSO_QUEUE) { \
238 run_queue_tl = END_TSO_QUEUE; \
244 //@cindex APPEND_TO_BLOCKED_QUEUE
245 /* Add a thread to the end of the blocked queue.
247 #define APPEND_TO_BLOCKED_QUEUE(tso) \
248 ASSERT(tso->link == END_TSO_QUEUE); \
249 if (blocked_queue_hd == END_TSO_QUEUE) { \
250 blocked_queue_hd = tso; \
252 blocked_queue_tl->link = tso; \
254 blocked_queue_tl = tso;
256 //@cindex THREAD_RUNNABLE
257 /* Signal that a runnable thread has become available, in
258 * case there are any waiting tasks to execute it.
261 #define THREAD_RUNNABLE() \
262 if (free_capabilities != NULL) { \
263 pthread_cond_signal(&thread_ready_cond); \
267 #define THREAD_RUNNABLE() /* nothing */
270 //@node Index, , Some convenient macros
274 //* APPEND_TO_BLOCKED_QUEUE:: @cindex\s-+APPEND_TO_BLOCKED_QUEUE
275 //* APPEND_TO_RUN_QUEUE:: @cindex\s-+APPEND_TO_RUN_QUEUE
276 //* Capability:: @cindex\s-+Capability
277 //* POP_RUN_QUEUE :: @cindex\s-+POP_RUN_QUEUE
278 //* PUSH_ON_RUN_QUEUE:: @cindex\s-+PUSH_ON_RUN_QUEUE
279 //* THREAD_RUNNABLE:: @cindex\s-+THREAD_RUNNABLE
280 //* awaitEvent:: @cindex\s-+awaitEvent
281 //* awakenBlockedQueue:: @cindex\s-+awakenBlockedQueue
282 //* awaken_blocked_queue:: @cindex\s-+awaken_blocked_queue
283 //* context_switch:: @cindex\s-+context_switch
284 //* exitScheduler:: @cindex\s-+exitScheduler
285 //* gc_pending_cond:: @cindex\s-+gc_pending_cond
286 //* initScheduler:: @cindex\s-+initScheduler
287 //* raiseAsync:: @cindex\s-+raiseAsync
288 //* sched_mutex:: @cindex\s-+sched_mutex
289 //* startTasks:: @cindex\s-+startTasks
290 //* task_info:: @cindex\s-+task_info
291 //* thread_ready_cond:: @cindex\s-+thread_ready_cond
292 //* unblockOne:: @cindex\s-+unblockOne