1 /* -----------------------------------------------------------------------------
2 * $Id: Schedule.h,v 1.15 2000/01/14 14:06:48 hwloidl 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);
108 extern void printThreadBlockage(StgTSO *tso);
110 void print_bq (StgClosure *node);
112 //@node Scheduler Vars and Data Types, Some convenient macros, Scheduler Functions
113 //@subsection Scheduler Vars and Data Types
115 //@cindex context_switch
116 /* Context switch flag.
117 * Locks required : sched_mutex
119 extern nat context_switch;
120 extern rtsBool interrupted;
122 extern nat ticks_since_select;
127 typedef StgRegTable Capability;
129 /* Free capability list.
130 * Locks required: sched_mutex.
133 extern Capability *free_capabilities;
134 extern nat n_free_capabilities;
136 extern Capability MainRegTable;
140 * Locks required : sched_mutex
142 extern StgTSO *run_queue_hd, *run_queue_tl;
143 extern StgTSO *blocked_queue_hd, *blocked_queue_tl;
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;
158 double elapsedtimestart;
165 extern task_info *task_ids;
169 extern StgTSO *run_queue_hd, *run_queue_tl;
170 extern StgTSO *blocked_queue_hd, *blocked_queue_tl;
175 void interruptStgRts ( void );
178 void raiseAsync(StgTSO *tso, StgClosure *exception);
179 nat run_queue_len(void);
181 //@node Some convenient macros, Index, Scheduler Vars and Data Types
182 //@subsection Some convenient macros
184 /* -----------------------------------------------------------------------------
185 * Some convenient macros...
188 #define END_TSO_QUEUE ((StgTSO *)(void*)&END_TSO_QUEUE_closure)
189 #define END_CAF_LIST ((StgCAF *)(void*)&END_TSO_QUEUE_closure)
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.
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; \
200 run_queue_tl->link = tso; \
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.
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; \
215 //@cindex POP_RUN_QUEUE
216 /* Pop the first thread off the runnable queue.
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; \
230 //@cindex APPEND_TO_BLOCKED_QUEUE
231 /* Add a thread to the end of the blocked queue.
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; \
238 blocked_queue_tl->link = tso; \
240 blocked_queue_tl = tso;
242 //@cindex THREAD_RUNNABLE
243 /* Signal that a runnable thread has become available, in
244 * case there are any waiting tasks to execute it.
247 #define THREAD_RUNNABLE() \
248 if (free_capabilities != NULL) { \
249 pthread_cond_signal(&thread_ready_cond); \
253 #define THREAD_RUNNABLE() /* nothing */
256 //@node Index, , Some convenient macros
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