1 /* -----------------------------------------------------------------------------
2 * $Id: Schedule.h,v 1.38 2003/03/17 14:47:48 simonmar Exp $
4 * (c) The GHC Team 1998-1999
6 * Prototypes for functions in Schedule.c
7 * (RTS internal scheduler interface)
9 * -------------------------------------------------------------------------*/
11 #ifndef __SCHEDULE_H__
12 #define __SCHEDULE_H__
13 #include "OSThreads.h"
15 /* initScheduler(), exitScheduler(), startTasks()
17 * Called from STG : no
18 * Locks assumed : none
20 extern void initScheduler ( void );
21 extern void exitScheduler ( void );
23 /* awakenBlockedQueue()
25 * Takes a pointer to the beginning of a blocked TSO queue, and
26 * wakes up the entire queue.
28 * Called from STG : yes
29 * Locks assumed : none
32 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
34 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
36 void awakenBlockedQueue(StgTSO *tso);
37 #if defined(RTS_SUPPORTS_THREADS)
38 void awakenBlockedQueueNoLock(StgTSO *tso);
44 * Takes a pointer to the beginning of a blocked TSO queue, and
45 * removes the first thread, placing it on the runnable queue.
47 * Called from STG : yes
48 * Locks assumed : none
50 #if defined(GRAN) || defined(PAR)
51 StgBlockingQueueElement *unblockOne(StgBlockingQueueElement *bqe, StgClosure *node);
53 StgTSO *unblockOne(StgTSO *tso);
58 * Raises an exception asynchronously in the specified thread.
60 * Called from STG : yes
61 * Locks assumed : none
63 void raiseAsync(StgTSO *tso, StgClosure *exception);
64 void raiseAsyncWithLock(StgTSO *tso, StgClosure *exception);
66 /* awaitEvent(rtsBool wait)
68 * Checks for blocked threads that need to be woken.
70 * Called from STG : NO
71 * Locks assumed : sched_mutex
73 void awaitEvent(rtsBool wait); /* In Select.c */
75 /* wakeUpSleepingThreads(nat ticks)
77 * Wakes up any sleeping threads whose timers have expired.
79 * Called from STG : NO
80 * Locks assumed : sched_mutex
82 rtsBool wakeUpSleepingThreads(nat); /* In Select.c */
84 /* wakeBlockedWorkerThread()
86 * If a worker thread is currently blocked in awaitEvent(), interrupt it.
88 * Called from STG : NO
89 * Locks assumed : sched_mutex
91 void wakeBlockedWorkerThread(void); /* In Select.c */
94 /* GetRoots(evac_fn f)
96 * Call f() for each root known to the scheduler.
98 * Called from STG : NO
99 * Locks assumed : ????
101 void GetRoots(evac_fn);
103 // ToDo: check whether all fcts below are used in the SMP version, too
105 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
106 void unlink_from_bq(StgTSO* tso, StgClosure* node);
107 void initThread(StgTSO *tso, nat stack_size, StgInt pri);
109 nat run_queue_len(void);
110 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
111 void initThread(StgTSO *tso, nat stack_size);
113 char *info_type(StgClosure *closure); // dummy
114 char *info_type_by_ip(StgInfoTable *ip); // dummy
115 void awaken_blocked_queue(StgTSO *q);
116 void initThread(StgTSO *tso, nat stack_size);
119 /* Context switch flag.
120 * Locks required : sched_mutex
122 extern nat context_switch;
123 extern rtsBool interrupted;
126 extern nat timestamp;
129 * Locks required : sched_mutex
131 * In GranSim we have one run/blocked_queue per PE.
134 // run_queue_hds defined in GranSim.h
136 extern StgTSO *run_queue_hd, *run_queue_tl;
137 extern StgTSO *blocked_queue_hd, *blocked_queue_tl;
138 extern StgTSO *sleeping_queue;
140 /* Linked list of all threads. */
141 extern StgTSO *all_threads;
143 #if defined(RTS_SUPPORTS_THREADS)
144 /* Schedule.c has detailed info on what these do */
145 extern Mutex sched_mutex;
146 extern Condition thread_ready_cond;
147 extern Condition returning_worker_cond;
148 extern nat rts_n_waiting_workers;
149 extern nat rts_n_waiting_tasks;
152 StgInt forkProcess(StgTSO *tso);
154 extern SchedulerStatus rts_mainLazyIO(HaskellObj p, /*out*/HaskellObj *ret);
157 /* Called by shutdown_handler(). */
158 void interruptStgRts ( void );
160 void raiseAsync(StgTSO *tso, StgClosure *exception);
161 nat run_queue_len(void);
163 void resurrectThreads( StgTSO * );
167 * These are the threads which clients have requested that we run.
169 * In a 'threaded' build, we might have several concurrent clients all
170 * waiting for results, and each one will wait on a condition variable
171 * until the result is available.
173 * In non-SMP, clients are strictly nested: the first client calls
174 * into the RTS, which might call out again to C with a _ccall_GC, and
175 * eventually re-enter the RTS.
177 * This is non-abstract at the moment because the garbage collector
178 * treats pointers to TSOs from the main thread list as "weak" - these
179 * pointers won't prevent a thread from receiving a BlockedOnDeadMVar
182 * Main threads information is kept in a linked list:
184 typedef struct StgMainThread_ {
186 SchedulerStatus stat;
188 #if defined(RTS_SUPPORTS_THREADS)
190 #if defined(THREADED_RTS)
191 rtsBool thread_bound;
192 Condition bound_thread_cond;
195 struct StgMainThread_ *link;
198 /* Main thread queue.
199 * Locks required: sched_mutex.
201 extern StgMainThread *main_threads;
203 void printAllThreads(void);
204 #ifdef COMPILING_SCHEDULER
205 static void printThreadBlockage(StgTSO *tso);
206 static void printThreadStatus(StgTSO *tso);
211 void print_bq (StgClosure *node);
214 void print_bqe (StgBlockingQueueElement *bqe);
217 void labelThread(StgPtr tso, char *label);
219 /* -----------------------------------------------------------------------------
220 * Some convenient macros...
223 /* END_TSO_QUEUE and friends now defined in includes/StgMiscClosures.h */
225 /* Add a thread to the end of the run queue.
226 * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
228 #define APPEND_TO_RUN_QUEUE(tso) \
229 ASSERT(tso->link == END_TSO_QUEUE); \
230 if (run_queue_hd == END_TSO_QUEUE) { \
231 run_queue_hd = tso; \
233 run_queue_tl->link = tso; \
237 /* Push a thread on the beginning of the run queue. Used for
238 * newly awakened threads, so they get run as soon as possible.
240 #define PUSH_ON_RUN_QUEUE(tso) \
241 tso->link = run_queue_hd; \
242 run_queue_hd = tso; \
243 if (run_queue_tl == END_TSO_QUEUE) { \
244 run_queue_tl = tso; \
247 /* Pop the first thread off the runnable queue.
249 #define POP_RUN_QUEUE() \
250 ({ StgTSO *t = run_queue_hd; \
251 if (t != END_TSO_QUEUE) { \
252 run_queue_hd = t->link; \
253 t->link = END_TSO_QUEUE; \
254 if (run_queue_hd == END_TSO_QUEUE) { \
255 run_queue_tl = END_TSO_QUEUE; \
261 /* Add a thread to the end of the blocked queue.
263 #define APPEND_TO_BLOCKED_QUEUE(tso) \
264 ASSERT(tso->link == END_TSO_QUEUE); \
265 if (blocked_queue_hd == END_TSO_QUEUE) { \
266 blocked_queue_hd = tso; \
268 blocked_queue_tl->link = tso; \
270 blocked_queue_tl = tso;
272 /* Signal that a runnable thread has become available, in
273 * case there are any waiting tasks to execute it.
275 #if defined(RTS_SUPPORTS_THREADS)
276 #define THREAD_RUNNABLE() \
277 wakeBlockedWorkerThread(); \
280 #define THREAD_RUNNABLE() /* nothing */
283 /* Check whether various thread queues are empty
285 #define EMPTY_QUEUE(q) (q == END_TSO_QUEUE)
287 #define EMPTY_RUN_QUEUE() (EMPTY_QUEUE(run_queue_hd))
288 #define EMPTY_BLOCKED_QUEUE() (EMPTY_QUEUE(blocked_queue_hd))
289 #define EMPTY_SLEEPING_QUEUE() (EMPTY_QUEUE(sleeping_queue))
291 #define EMPTY_THREAD_QUEUES() (EMPTY_RUN_QUEUE() && \
292 EMPTY_BLOCKED_QUEUE() && \
293 EMPTY_SLEEPING_QUEUE())
295 #if defined(RTS_SUPPORTS_THREADS)
296 /* If no task is waiting for a capability,
297 * spawn a new worker thread.
299 * (Used by the RtsAPI)
302 startSchedulerTask(void);
305 #endif /* __SCHEDULE_H__ */