1 /* -----------------------------------------------------------------------------
2 * $Id: Schedule.h,v 1.13 2000/01/13 14:34:05 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
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
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 );
177 void raiseAsync(StgTSO *tso, StgClosure *exception);
179 //@node Some convenient macros, Index, Scheduler Vars and Data Types
180 //@subsection Some convenient macros
182 /* -----------------------------------------------------------------------------
183 * Some convenient macros...
186 #define END_TSO_QUEUE ((StgTSO *)(void*)&END_TSO_QUEUE_closure)
187 #define END_CAF_LIST ((StgCAF *)(void*)&END_TSO_QUEUE_closure)
189 //@cindex APPEND_TO_RUN_QUEUE
190 /* Add a thread to the end of the run queue.
191 * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
193 #define APPEND_TO_RUN_QUEUE(tso) \
194 ASSERT(tso->link == END_TSO_QUEUE); \
195 if (run_queue_hd == END_TSO_QUEUE) { \
196 run_queue_hd = tso; \
198 run_queue_tl->link = tso; \
202 //@cindex PUSH_ON_RUN_QUEUE
203 /* Push a thread on the beginning of the run queue. Used for
204 * newly awakened threads, so they get run as soon as possible.
206 #define PUSH_ON_RUN_QUEUE(tso) \
207 tso->link = run_queue_hd; \
208 run_queue_hd = tso; \
209 if (run_queue_tl == END_TSO_QUEUE) { \
210 run_queue_tl = tso; \
213 //@cindex POP_RUN_QUEUE
214 /* Pop the first thread off the runnable queue.
216 #define POP_RUN_QUEUE() \
217 ({ StgTSO *t = run_queue_hd; \
218 if (t != END_TSO_QUEUE) { \
219 run_queue_hd = t->link; \
220 t->link = END_TSO_QUEUE; \
221 if (run_queue_hd == END_TSO_QUEUE) { \
222 run_queue_tl = END_TSO_QUEUE; \
228 //@cindex APPEND_TO_BLOCKED_QUEUE
229 /* Add a thread to the end of the blocked queue.
231 #define APPEND_TO_BLOCKED_QUEUE(tso) \
232 ASSERT(tso->link == END_TSO_QUEUE); \
233 if (blocked_queue_hd == END_TSO_QUEUE) { \
234 blocked_queue_hd = tso; \
236 blocked_queue_tl->link = tso; \
238 blocked_queue_tl = tso;
240 //@cindex THREAD_RUNNABLE
241 /* Signal that a runnable thread has become available, in
242 * case there are any waiting tasks to execute it.
245 #define THREAD_RUNNABLE() \
246 if (free_capabilities != NULL) { \
247 pthread_cond_signal(&thread_ready_cond); \
251 #define THREAD_RUNNABLE() /* nothing */
254 //@node Index, , Some convenient macros
258 //* APPEND_TO_BLOCKED_QUEUE:: @cindex\s-+APPEND_TO_BLOCKED_QUEUE
259 //* APPEND_TO_RUN_QUEUE:: @cindex\s-+APPEND_TO_RUN_QUEUE
260 //* Capability:: @cindex\s-+Capability
261 //* POP_RUN_QUEUE :: @cindex\s-+POP_RUN_QUEUE
262 //* PUSH_ON_RUN_QUEUE:: @cindex\s-+PUSH_ON_RUN_QUEUE
263 //* THREAD_RUNNABLE:: @cindex\s-+THREAD_RUNNABLE
264 //* awaitEvent:: @cindex\s-+awaitEvent
265 //* awakenBlockedQueue:: @cindex\s-+awakenBlockedQueue
266 //* awaken_blocked_queue:: @cindex\s-+awaken_blocked_queue
267 //* context_switch:: @cindex\s-+context_switch
268 //* exitScheduler:: @cindex\s-+exitScheduler
269 //* gc_pending_cond:: @cindex\s-+gc_pending_cond
270 //* initScheduler:: @cindex\s-+initScheduler
271 //* raiseAsync:: @cindex\s-+raiseAsync
272 //* sched_mutex:: @cindex\s-+sched_mutex
273 //* startTasks:: @cindex\s-+startTasks
274 //* task_info:: @cindex\s-+task_info
275 //* thread_ready_cond:: @cindex\s-+thread_ready_cond
276 //* unblockOne:: @cindex\s-+unblockOne