1 /* -----------------------------------------------------------------------------
2 * $Id: Schedule.h,v 1.19 2000/08/25 13:12:07 simonmar 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 /* wakeUpSleepingThreads(nat ticks)
90 * Wakes up any sleeping threads whose timers have expired.
92 * Called from STG : NO
93 * Locks assumed : sched_mutex
95 rtsBool wakeUpSleepingThreads(nat); /* In Select.c */
97 // ToDo: check whether all fcts below are used in the SMP version, too
98 //@cindex awaken_blocked_queue
100 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
101 void unlink_from_bq(StgTSO* tso, StgClosure* node);
102 void initThread(StgTSO *tso, nat stack_size, StgInt pri);
104 nat run_queue_len(void);
105 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
106 void initThread(StgTSO *tso, nat stack_size);
108 char *info_type(StgClosure *closure); // dummy
109 char *info_type_by_ip(StgInfoTable *ip); // dummy
110 void awaken_blocked_queue(StgTSO *q);
111 void initThread(StgTSO *tso, nat stack_size);
114 //@node Scheduler Vars and Data Types, Some convenient macros, Scheduler Functions
115 //@subsection Scheduler Vars and Data Types
117 //@cindex context_switch
118 /* Context switch flag.
119 * Locks required : sched_mutex
121 extern nat context_switch;
122 extern rtsBool interrupted;
125 extern nat timestamp;
126 extern nat ticks_since_timestamp;
131 typedef StgRegTable Capability;
133 /* Free capability list.
134 * Locks required: sched_mutex.
137 extern Capability *free_capabilities;
138 extern nat n_free_capabilities;
140 extern Capability MainRegTable;
144 * Locks required : sched_mutex
146 * In GranSim we have one run/blocked_queue per PE.
149 // run_queue_hds defined in GranSim.h
151 extern StgTSO *run_queue_hd, *run_queue_tl;
152 extern StgTSO *blocked_queue_hd, *blocked_queue_tl;
153 extern StgTSO *sleeping_queue;
155 /* Linked list of all threads. */
156 extern StgTSO *all_threads;
159 //@cindex sched_mutex
160 //@cindex thread_ready_cond
161 //@cindex gc_pending_cond
162 extern pthread_mutex_t sched_mutex;
163 extern pthread_cond_t thread_ready_cond;
164 extern pthread_cond_t gc_pending_cond;
171 double elapsedtimestart;
178 extern task_info *task_ids;
183 void interruptStgRts ( void );
185 void raiseAsync(StgTSO *tso, StgClosure *exception);
186 nat run_queue_len(void);
188 void resurrectThreads( StgTSO * );
190 //@node Some convenient macros, Index, Scheduler Vars and Data Types
191 //@subsection Some convenient macros
196 void printThreadBlockage(StgTSO *tso);
197 void printThreadStatus(StgTSO *tso);
198 void printAllThreads(void);
200 void print_bq (StgClosure *node);
202 /* -----------------------------------------------------------------------------
203 * Some convenient macros...
206 /* this is the NIL ptr for a TSO queue (e.g. runnable queue) */
207 #define END_TSO_QUEUE ((StgTSO *)(void*)&END_TSO_QUEUE_closure)
208 /* this is the NIL ptr for a list CAFs */
209 #define END_ECAF_LIST ((StgCAF *)(void*)&END_TSO_QUEUE_closure)
210 #if defined(PAR) || defined(GRAN)
211 /* this is the NIL ptr for a blocking queue */
212 # define END_BQ_QUEUE ((StgBlockingQueueElement *)(void*)&END_TSO_QUEUE_closure)
213 /* this is the NIL ptr for a blocked fetch queue (as in PendingFetches in GUM) */
214 # define END_BF_QUEUE ((StgBlockedFetch *)(void*)&END_TSO_QUEUE_closure)
217 //@cindex APPEND_TO_RUN_QUEUE
218 /* Add a thread to the end of the run queue.
219 * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
221 #define APPEND_TO_RUN_QUEUE(tso) \
222 ASSERT(tso->link == END_TSO_QUEUE); \
223 if (run_queue_hd == END_TSO_QUEUE) { \
224 run_queue_hd = tso; \
226 run_queue_tl->link = tso; \
230 //@cindex PUSH_ON_RUN_QUEUE
231 /* Push a thread on the beginning of the run queue. Used for
232 * newly awakened threads, so they get run as soon as possible.
234 #define PUSH_ON_RUN_QUEUE(tso) \
235 tso->link = run_queue_hd; \
236 run_queue_hd = tso; \
237 if (run_queue_tl == END_TSO_QUEUE) { \
238 run_queue_tl = tso; \
241 //@cindex POP_RUN_QUEUE
242 /* Pop the first thread off the runnable queue.
244 #define POP_RUN_QUEUE() \
245 ({ StgTSO *t = run_queue_hd; \
246 if (t != END_TSO_QUEUE) { \
247 run_queue_hd = t->link; \
248 t->link = END_TSO_QUEUE; \
249 if (run_queue_hd == END_TSO_QUEUE) { \
250 run_queue_tl = END_TSO_QUEUE; \
256 //@cindex APPEND_TO_BLOCKED_QUEUE
257 /* Add a thread to the end of the blocked queue.
259 #define APPEND_TO_BLOCKED_QUEUE(tso) \
260 ASSERT(tso->link == END_TSO_QUEUE); \
261 if (blocked_queue_hd == END_TSO_QUEUE) { \
262 blocked_queue_hd = tso; \
264 blocked_queue_tl->link = tso; \
266 blocked_queue_tl = tso;
268 //@cindex THREAD_RUNNABLE
269 /* Signal that a runnable thread has become available, in
270 * case there are any waiting tasks to execute it.
273 #define THREAD_RUNNABLE() \
274 if (free_capabilities != NULL) { \
275 pthread_cond_signal(&thread_ready_cond); \
279 #define THREAD_RUNNABLE() /* nothing */
282 //@node Index, , Some convenient macros
286 //* APPEND_TO_BLOCKED_QUEUE:: @cindex\s-+APPEND_TO_BLOCKED_QUEUE
287 //* APPEND_TO_RUN_QUEUE:: @cindex\s-+APPEND_TO_RUN_QUEUE
288 //* Capability:: @cindex\s-+Capability
289 //* POP_RUN_QUEUE :: @cindex\s-+POP_RUN_QUEUE
290 //* PUSH_ON_RUN_QUEUE:: @cindex\s-+PUSH_ON_RUN_QUEUE
291 //* THREAD_RUNNABLE:: @cindex\s-+THREAD_RUNNABLE
292 //* awaitEvent:: @cindex\s-+awaitEvent
293 //* awakenBlockedQueue:: @cindex\s-+awakenBlockedQueue
294 //* awaken_blocked_queue:: @cindex\s-+awaken_blocked_queue
295 //* context_switch:: @cindex\s-+context_switch
296 //* exitScheduler:: @cindex\s-+exitScheduler
297 //* gc_pending_cond:: @cindex\s-+gc_pending_cond
298 //* initScheduler:: @cindex\s-+initScheduler
299 //* raiseAsync:: @cindex\s-+raiseAsync
300 //* sched_mutex:: @cindex\s-+sched_mutex
301 //* startTasks:: @cindex\s-+startTasks
302 //* task_info:: @cindex\s-+task_info
303 //* thread_ready_cond:: @cindex\s-+thread_ready_cond
304 //* unblockOne:: @cindex\s-+unblockOne