1 /* -----------------------------------------------------------------------------
2 * $Id: Schedule.h,v 1.36 2002/10/22 11:01:20 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);
41 * Takes a pointer to the beginning of a blocked TSO queue, and
42 * removes the first thread, placing it on the runnable queue.
44 * Called from STG : yes
45 * Locks assumed : none
47 #if defined(GRAN) || defined(PAR)
48 StgBlockingQueueElement *unblockOne(StgBlockingQueueElement *bqe, StgClosure *node);
50 StgTSO *unblockOne(StgTSO *tso);
55 * Raises an exception asynchronously in the specified thread.
57 * Called from STG : yes
58 * Locks assumed : none
60 void raiseAsync(StgTSO *tso, StgClosure *exception);
61 void raiseAsyncWithLock(StgTSO *tso, StgClosure *exception);
65 * Raises an exception asynchronously in the specified thread.
67 * Called from STG : NO
68 * Locks assumed : sched_mutex
70 void awaitEvent(rtsBool wait); /* In Select.c */
72 /* wakeUpSleepingThreads(nat ticks)
74 * Wakes up any sleeping threads whose timers have expired.
76 * Called from STG : NO
77 * Locks assumed : sched_mutex
79 rtsBool wakeUpSleepingThreads(nat); /* In Select.c */
81 /* GetRoots(evac_fn f)
83 * Call f() for each root known to the scheduler.
85 * Called from STG : NO
86 * Locks assumed : ????
88 void GetRoots(evac_fn);
90 // ToDo: check whether all fcts below are used in the SMP version, too
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);
106 /* Context switch flag.
107 * Locks required : sched_mutex
109 extern nat context_switch;
110 extern rtsBool interrupted;
113 extern nat timestamp;
116 * Locks required : sched_mutex
118 * In GranSim we have one run/blocked_queue per PE.
121 // run_queue_hds defined in GranSim.h
123 extern StgTSO *run_queue_hd, *run_queue_tl;
124 extern StgTSO *blocked_queue_hd, *blocked_queue_tl;
125 extern StgTSO *sleeping_queue;
127 /* Linked list of all threads. */
128 extern StgTSO *all_threads;
130 #if defined(RTS_SUPPORTS_THREADS)
131 /* Schedule.c has detailed info on what these do */
132 extern Mutex sched_mutex;
133 extern Condition thread_ready_cond;
134 extern Condition returning_worker_cond;
135 extern nat rts_n_waiting_workers;
136 extern nat rts_n_waiting_tasks;
139 StgInt forkProcess(StgTSO *tso);
141 extern SchedulerStatus rts_mainEvalIO(HaskellObj p, /*out*/HaskellObj *ret);
144 /* Called by shutdown_handler(). */
145 void interruptStgRts ( void );
147 void raiseAsync(StgTSO *tso, StgClosure *exception);
148 nat run_queue_len(void);
150 void resurrectThreads( StgTSO * );
154 * These are the threads which clients have requested that we run.
156 * In a 'threaded' build, we might have several concurrent clients all
157 * waiting for results, and each one will wait on a condition variable
158 * until the result is available.
160 * In non-SMP, clients are strictly nested: the first client calls
161 * into the RTS, which might call out again to C with a _ccall_GC, and
162 * eventually re-enter the RTS.
164 * This is non-abstract at the moment because the garbage collector
165 * treats pointers to TSOs from the main thread list as "weak" - these
166 * pointers won't prevent a thread from receiving a BlockedOnDeadMVar
169 * Main threads information is kept in a linked list:
171 typedef struct StgMainThread_ {
173 SchedulerStatus stat;
175 #if defined(RTS_SUPPORTS_THREADS)
178 struct StgMainThread_ *link;
181 /* Main thread queue.
182 * Locks required: sched_mutex.
184 extern StgMainThread *main_threads;
186 void printAllThreads(void);
187 #ifdef COMPILING_SCHEDULER
188 static void printThreadBlockage(StgTSO *tso);
189 static void printThreadStatus(StgTSO *tso);
194 void print_bq (StgClosure *node);
197 void print_bqe (StgBlockingQueueElement *bqe);
200 void labelThread(StgPtr tso, char *label);
202 /* -----------------------------------------------------------------------------
203 * Some convenient macros...
206 /* END_TSO_QUEUE and friends now defined in includes/StgMiscClosures.h */
208 /* Add a thread to the end of the run queue.
209 * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
211 #define APPEND_TO_RUN_QUEUE(tso) \
212 ASSERT(tso->link == END_TSO_QUEUE); \
213 if (run_queue_hd == END_TSO_QUEUE) { \
214 run_queue_hd = tso; \
216 run_queue_tl->link = tso; \
220 /* Push a thread on the beginning of the run queue. Used for
221 * newly awakened threads, so they get run as soon as possible.
223 #define PUSH_ON_RUN_QUEUE(tso) \
224 tso->link = run_queue_hd; \
225 run_queue_hd = tso; \
226 if (run_queue_tl == END_TSO_QUEUE) { \
227 run_queue_tl = tso; \
230 /* Pop the first thread off the runnable queue.
232 #define POP_RUN_QUEUE() \
233 ({ StgTSO *t = run_queue_hd; \
234 if (t != END_TSO_QUEUE) { \
235 run_queue_hd = t->link; \
236 t->link = END_TSO_QUEUE; \
237 if (run_queue_hd == END_TSO_QUEUE) { \
238 run_queue_tl = END_TSO_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 /* Signal that a runnable thread has become available, in
256 * case there are any waiting tasks to execute it.
258 #if defined(RTS_SUPPORTS_THREADS)
259 #define THREAD_RUNNABLE() \
260 if ( !noCapabilities() ) { \
261 signalCondition(&thread_ready_cond); \
265 #define THREAD_RUNNABLE() /* nothing */
268 /* Check whether various thread queues are empty
270 #define EMPTY_QUEUE(q) (q == END_TSO_QUEUE)
272 #define EMPTY_RUN_QUEUE() (EMPTY_QUEUE(run_queue_hd))
273 #define EMPTY_BLOCKED_QUEUE() (EMPTY_QUEUE(blocked_queue_hd))
274 #define EMPTY_SLEEPING_QUEUE() (EMPTY_QUEUE(sleeping_queue))
276 #define EMPTY_THREAD_QUEUES() (EMPTY_RUN_QUEUE() && \
277 EMPTY_BLOCKED_QUEUE() && \
278 EMPTY_SLEEPING_QUEUE())
280 #endif /* __SCHEDULE_H__ */