1 /* -----------------------------------------------------------------------------
2 * $Id: Schedule.h,v 1.28 2002/02/13 08:48:07 sof Exp $
4 * (c) The GHC Team 1998-1999
6 * Prototypes for functions in Schedule.c
7 * (RTS internal scheduler interface)
9 * -------------------------------------------------------------------------*/
10 #ifndef __SCHEDULE_H__
11 #define __SCHEDULE_H__
12 #include "OSThreads.h"
15 //* Scheduler Functions::
16 //* Scheduler Vars and Data Types::
17 //* Some convenient macros::
21 //@node Scheduler Functions, Scheduler Vars and Data Types
22 //@subsection Scheduler Functions
24 //@cindex initScheduler
25 //@cindex exitScheduler
26 /* initScheduler(), exitScheduler(), startTasks()
28 * Called from STG : no
29 * Locks assumed : none
31 extern void initScheduler ( void );
32 extern void exitScheduler ( void );
34 //@cindex awakenBlockedQueue
35 /* awakenBlockedQueue()
37 * Takes a pointer to the beginning of a blocked TSO queue, and
38 * wakes up the entire queue.
40 * Called from STG : yes
41 * Locks assumed : none
44 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
46 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
48 void awakenBlockedQueue(StgTSO *tso);
54 * Takes a pointer to the beginning of a blocked TSO queue, and
55 * removes the first thread, placing it on the runnable queue.
57 * Called from STG : yes
58 * Locks assumed : none
60 #if defined(GRAN) || defined(PAR)
61 StgBlockingQueueElement *unblockOne(StgBlockingQueueElement *bqe, StgClosure *node);
63 StgTSO *unblockOne(StgTSO *tso);
69 * Raises an exception asynchronously in the specified thread.
71 * Called from STG : yes
72 * Locks assumed : none
74 void raiseAsync(StgTSO *tso, StgClosure *exception);
79 * Raises an exception asynchronously in the specified thread.
81 * Called from STG : NO
82 * Locks assumed : sched_mutex
84 void awaitEvent(rtsBool wait); /* In Select.c */
86 /* wakeUpSleepingThreads(nat ticks)
88 * Wakes up any sleeping threads whose timers have expired.
90 * Called from STG : NO
91 * Locks assumed : sched_mutex
93 rtsBool wakeUpSleepingThreads(nat); /* In Select.c */
95 /* GetRoots(evac_fn f)
97 * Call f() for each root known to the scheduler.
99 * Called from STG : NO
100 * Locks assumed : ????
102 void GetRoots(evac_fn);
104 // ToDo: check whether all fcts below are used in the SMP version, too
105 //@cindex awaken_blocked_queue
107 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
108 void unlink_from_bq(StgTSO* tso, StgClosure* node);
109 void initThread(StgTSO *tso, nat stack_size, StgInt pri);
111 nat run_queue_len(void);
112 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
113 void initThread(StgTSO *tso, nat stack_size);
115 char *info_type(StgClosure *closure); // dummy
116 char *info_type_by_ip(StgInfoTable *ip); // dummy
117 void awaken_blocked_queue(StgTSO *q);
118 void initThread(StgTSO *tso, nat stack_size);
121 //@node Scheduler Vars and Data Types, Some convenient macros, Scheduler Functions
122 //@subsection Scheduler Vars and Data Types
124 //@cindex context_switch
125 /* Context switch flag.
126 * Locks required : sched_mutex
128 extern nat context_switch;
129 extern rtsBool interrupted;
132 extern nat timestamp;
135 * Locks required : sched_mutex
137 * In GranSim we have one run/blocked_queue per PE.
140 // run_queue_hds defined in GranSim.h
142 extern StgTSO *run_queue_hd, *run_queue_tl;
143 extern StgTSO *blocked_queue_hd, *blocked_queue_tl;
144 extern StgTSO *sleeping_queue;
146 /* Linked list of all threads. */
147 extern StgTSO *all_threads;
149 #if defined(RTS_SUPPORTS_THREADS)
150 /* Schedule.c has detailed info on what these do */
151 extern Mutex sched_mutex;
152 extern Condition thread_ready_cond;
153 extern Condition returning_worker_cond;
154 extern nat rts_n_waiting_workers;
155 extern nat rts_n_waiting_tasks;
159 /* Sigh, RTS-internal versions of waitThread(), scheduleThread(), and
160 rts_evalIO() for the use by main() only. ToDo: better. */
161 extern SchedulerStatus waitThread_(StgTSO *tso,
162 /*out*/StgClosure **ret
163 #if defined(THREADED_RTS)
164 , rtsBool blockWaiting
167 extern void scheduleThread_(StgTSO *tso
168 #if defined(THREADED_RTS)
172 extern SchedulerStatus rts_mainEvalIO(HaskellObj p, /*out*/HaskellObj *ret);
175 /* Called by shutdown_handler(). */
176 void interruptStgRts ( void );
178 void raiseAsync(StgTSO *tso, StgClosure *exception);
179 nat run_queue_len(void);
181 void resurrectThreads( StgTSO * );
183 //@node Some convenient macros, Index, Scheduler Vars and Data Types
184 //@subsection Some convenient macros
189 void printThreadBlockage(StgTSO *tso);
190 void printThreadStatus(StgTSO *tso);
191 void printAllThreads(void);
193 void print_bq (StgClosure *node);
195 void print_bqe (StgBlockingQueueElement *bqe);
198 /* -----------------------------------------------------------------------------
199 * Some convenient macros...
202 /* END_TSO_QUEUE and friends now defined in includes/StgMiscClosures.h */
204 //@cindex APPEND_TO_RUN_QUEUE
205 /* Add a thread to the end of the run queue.
206 * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
208 #define APPEND_TO_RUN_QUEUE(tso) \
209 ASSERT(tso->link == END_TSO_QUEUE); \
210 if (run_queue_hd == END_TSO_QUEUE) { \
211 run_queue_hd = tso; \
213 run_queue_tl->link = tso; \
217 //@cindex PUSH_ON_RUN_QUEUE
218 /* Push a thread on the beginning of the run queue. Used for
219 * newly awakened threads, so they get run as soon as possible.
221 #define PUSH_ON_RUN_QUEUE(tso) \
222 tso->link = run_queue_hd; \
223 run_queue_hd = tso; \
224 if (run_queue_tl == END_TSO_QUEUE) { \
225 run_queue_tl = tso; \
228 //@cindex POP_RUN_QUEUE
229 /* Pop the first thread off the runnable queue.
231 #define POP_RUN_QUEUE() \
232 ({ StgTSO *t = run_queue_hd; \
233 if (t != END_TSO_QUEUE) { \
234 run_queue_hd = t->link; \
235 t->link = END_TSO_QUEUE; \
236 if (run_queue_hd == END_TSO_QUEUE) { \
237 run_queue_tl = END_TSO_QUEUE; \
243 //@cindex APPEND_TO_BLOCKED_QUEUE
244 /* Add a thread to the end of the blocked queue.
246 #define APPEND_TO_BLOCKED_QUEUE(tso) \
247 ASSERT(tso->link == END_TSO_QUEUE); \
248 if (blocked_queue_hd == END_TSO_QUEUE) { \
249 blocked_queue_hd = tso; \
251 blocked_queue_tl->link = tso; \
253 blocked_queue_tl = tso;
255 //@cindex THREAD_RUNNABLE
256 /* Signal that a runnable thread has become available, in
257 * case there are any waiting tasks to execute it.
259 #if defined(RTS_SUPPORTS_THREADS)
260 #define THREAD_RUNNABLE() \
261 if ( !noCapabilities() ) { \
262 signalCondition(&thread_ready_cond); \
266 #define THREAD_RUNNABLE() /* nothing */
269 //@cindex EMPTY_RUN_QUEUE
270 /* Check whether the run queue is empty i.e. the PE is idle
272 #define EMPTY_RUN_QUEUE() (run_queue_hd == END_TSO_QUEUE)
273 #define EMPTY_QUEUE(q) (q == END_TSO_QUEUE)
275 #endif /* __SCHEDULE_H__ */
277 //@node Index, , Some convenient macros
281 //* APPEND_TO_BLOCKED_QUEUE:: @cindex\s-+APPEND_TO_BLOCKED_QUEUE
282 //* APPEND_TO_RUN_QUEUE:: @cindex\s-+APPEND_TO_RUN_QUEUE
283 //* POP_RUN_QUEUE :: @cindex\s-+POP_RUN_QUEUE
284 //* PUSH_ON_RUN_QUEUE:: @cindex\s-+PUSH_ON_RUN_QUEUE
285 //* awaitEvent:: @cindex\s-+awaitEvent
286 //* awakenBlockedQueue:: @cindex\s-+awakenBlockedQueue
287 //* awaken_blocked_queue:: @cindex\s-+awaken_blocked_queue
288 //* context_switch:: @cindex\s-+context_switch
289 //* exitScheduler:: @cindex\s-+exitScheduler
290 //* gc_pending_cond:: @cindex\s-+gc_pending_cond
291 //* initScheduler:: @cindex\s-+initScheduler
292 //* raiseAsync:: @cindex\s-+raiseAsync
293 //* startTasks:: @cindex\s-+startTasks
294 //* unblockOne:: @cindex\s-+unblockOne