1 /* -----------------------------------------------------------------------------
2 * $Id: Schedule.h,v 1.24 2001/11/13 13:38:02 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
61 #if defined(GRAN) || defined(PAR)
62 StgBlockingQueueElement *unblockOne(StgBlockingQueueElement *bqe, StgClosure *node);
64 StgTSO *unblockOne(StgTSO *tso);
70 * Raises an exception asynchronously in the specified thread.
72 * Called from STG : yes
73 * Locks assumed : none
75 void raiseAsync(StgTSO *tso, StgClosure *exception);
80 * Raises an exception asynchronously in the specified thread.
82 * Called from STG : NO
83 * Locks assumed : sched_mutex
85 void awaitEvent(rtsBool wait); /* In Select.c */
87 /* wakeUpSleepingThreads(nat ticks)
89 * Wakes up any sleeping threads whose timers have expired.
91 * Called from STG : NO
92 * Locks assumed : sched_mutex
94 rtsBool wakeUpSleepingThreads(nat); /* In Select.c */
96 // ToDo: check whether all fcts below are used in the SMP version, too
97 //@cindex awaken_blocked_queue
99 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
100 void unlink_from_bq(StgTSO* tso, StgClosure* node);
101 void initThread(StgTSO *tso, nat stack_size, StgInt pri);
103 nat run_queue_len(void);
104 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
105 void initThread(StgTSO *tso, nat stack_size);
107 char *info_type(StgClosure *closure); // dummy
108 char *info_type_by_ip(StgInfoTable *ip); // dummy
109 void awaken_blocked_queue(StgTSO *q);
110 void initThread(StgTSO *tso, nat stack_size);
113 //@node Scheduler Vars and Data Types, Some convenient macros, Scheduler Functions
114 //@subsection Scheduler Vars and Data Types
116 //@cindex context_switch
117 /* Context switch flag.
118 * Locks required : sched_mutex
120 extern nat context_switch;
121 extern rtsBool interrupted;
124 extern nat timestamp;
126 /* Free capability list.
127 * Locks required: sched_mutex.
130 extern Capability *free_capabilities;
131 extern nat n_free_capabilities;
133 extern Capability MainCapability;
137 * Locks required : sched_mutex
139 * In GranSim we have one run/blocked_queue per PE.
142 // run_queue_hds defined in GranSim.h
144 extern StgTSO *run_queue_hd, *run_queue_tl;
145 extern StgTSO *blocked_queue_hd, *blocked_queue_tl;
146 extern StgTSO *sleeping_queue;
148 /* Linked list of all threads. */
149 extern StgTSO *all_threads;
152 //@cindex sched_mutex
153 //@cindex thread_ready_cond
154 //@cindex gc_pending_cond
155 extern pthread_mutex_t sched_mutex;
156 extern pthread_cond_t thread_ready_cond;
157 extern pthread_cond_t gc_pending_cond;
164 double elapsedtimestart;
171 extern task_info *task_ids;
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.
260 #define THREAD_RUNNABLE() \
261 if (free_capabilities != NULL) { \
262 pthread_cond_signal(&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)
274 //@node Index, , Some convenient macros
278 //* APPEND_TO_BLOCKED_QUEUE:: @cindex\s-+APPEND_TO_BLOCKED_QUEUE
279 //* APPEND_TO_RUN_QUEUE:: @cindex\s-+APPEND_TO_RUN_QUEUE
280 //* Capability:: @cindex\s-+Capability
281 //* POP_RUN_QUEUE :: @cindex\s-+POP_RUN_QUEUE
282 //* PUSH_ON_RUN_QUEUE:: @cindex\s-+PUSH_ON_RUN_QUEUE
283 //* THREAD_RUNNABLE:: @cindex\s-+THREAD_RUNNABLE
284 //* awaitEvent:: @cindex\s-+awaitEvent
285 //* awakenBlockedQueue:: @cindex\s-+awakenBlockedQueue
286 //* awaken_blocked_queue:: @cindex\s-+awaken_blocked_queue
287 //* context_switch:: @cindex\s-+context_switch
288 //* exitScheduler:: @cindex\s-+exitScheduler
289 //* gc_pending_cond:: @cindex\s-+gc_pending_cond
290 //* initScheduler:: @cindex\s-+initScheduler
291 //* raiseAsync:: @cindex\s-+raiseAsync
292 //* sched_mutex:: @cindex\s-+sched_mutex
293 //* startTasks:: @cindex\s-+startTasks
294 //* task_info:: @cindex\s-+task_info
295 //* thread_ready_cond:: @cindex\s-+thread_ready_cond
296 //* unblockOne:: @cindex\s-+unblockOne