1 /* -----------------------------------------------------------------------------
2 * $Id: Schedule.h,v 1.16 2000/03/16 17:27:13 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 );
35 //@cindex awakenBlockedQueue
36 /* awakenBlockedQueue()
38 * Takes a pointer to the beginning of a blocked TSO queue, and
39 * wakes up the entire queue.
41 * Called from STG : yes
42 * Locks assumed : none
45 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
47 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
49 void awakenBlockedQueue(StgTSO *tso);
55 * Takes a pointer to the beginning of a blocked TSO queue, and
56 * removes the first thread, placing it on the runnable queue.
58 * Called from STG : yes
59 * Locks assumed : none
62 StgTSO *unblockOne(StgTSO *tso, StgClosure *node);
64 StgTSO *unblockOne(StgTSO *tso, StgClosure *node);
66 StgTSO *unblockOne(StgTSO *tso);
72 * Raises an exception asynchronously in the specified thread.
74 * Called from STG : yes
75 * Locks assumed : none
77 void raiseAsync(StgTSO *tso, StgClosure *exception);
82 * Raises an exception asynchronously in the specified thread.
84 * Called from STG : NO
85 * Locks assumed : sched_mutex
87 void awaitEvent(rtsBool wait); /* In Select.c */
89 // ToDo: check whether all fcts below are used in the SMP version, too
90 //@cindex awaken_blocked_queue
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);
96 nat run_queue_len(void);
97 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
98 void initThread(StgTSO *tso, nat stack_size);
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);
106 //@node Scheduler Vars and Data Types, Some convenient macros, Scheduler Functions
107 //@subsection Scheduler Vars and Data Types
109 //@cindex context_switch
110 /* Context switch flag.
111 * Locks required : sched_mutex
113 extern nat context_switch;
114 extern rtsBool interrupted;
116 extern nat ticks_since_select;
121 typedef StgRegTable Capability;
123 /* Free capability list.
124 * Locks required: sched_mutex.
127 extern Capability *free_capabilities;
128 extern nat n_free_capabilities;
130 extern Capability MainRegTable;
134 * Locks required : sched_mutex
136 extern StgTSO *run_queue_hd, *run_queue_tl;
137 extern StgTSO *blocked_queue_hd, *blocked_queue_tl;
138 extern StgTSO *all_threads;
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;
153 double elapsedtimestart;
160 extern task_info *task_ids;
164 extern StgTSO *run_queue_hd, *run_queue_tl;
165 extern StgTSO *blocked_queue_hd, *blocked_queue_tl;
170 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 #define END_TSO_QUEUE ((StgTSO *)(void*)&END_TSO_QUEUE_closure)
195 #define END_CAF_LIST ((StgCAF *)(void*)&END_TSO_QUEUE_closure)
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.
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; \
206 run_queue_tl->link = tso; \
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.
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; \
221 //@cindex POP_RUN_QUEUE
222 /* Pop the first thread off the runnable queue.
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; \
236 //@cindex APPEND_TO_BLOCKED_QUEUE
237 /* Add a thread to the end of the blocked queue.
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; \
244 blocked_queue_tl->link = tso; \
246 blocked_queue_tl = tso;
248 //@cindex THREAD_RUNNABLE
249 /* Signal that a runnable thread has become available, in
250 * case there are any waiting tasks to execute it.
253 #define THREAD_RUNNABLE() \
254 if (free_capabilities != NULL) { \
255 pthread_cond_signal(&thread_ready_cond); \
259 #define THREAD_RUNNABLE() /* nothing */
262 //@node Index, , Some convenient macros
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