1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-1999
5 * Prototypes for functions in Schedule.c
6 * (RTS internal scheduler interface)
8 * -------------------------------------------------------------------------*/
10 #ifndef __SCHEDULE_H__
11 #define __SCHEDULE_H__
12 #include "OSThreads.h"
14 /* initScheduler(), exitScheduler(), startTasks()
16 * Called from STG : no
17 * Locks assumed : none
19 extern void initScheduler ( void );
20 extern void exitScheduler ( void );
22 /* awakenBlockedQueue()
24 * Takes a pointer to the beginning of a blocked TSO queue, and
25 * wakes up the entire queue.
27 * Called from STG : yes
28 * Locks assumed : none
31 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
33 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
35 void awakenBlockedQueue (StgTSO *tso);
36 void awakenBlockedQueueNoLock (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);
63 /* raiseExceptionHelper */
64 StgWord raiseExceptionHelper (StgTSO *tso, StgClosure *exception);
66 /* findRetryFrameHelper */
67 StgWord findRetryFrameHelper (StgTSO *tso);
69 /* awaitEvent(rtsBool wait)
71 * Checks for blocked threads that need to be woken.
73 * Called from STG : NO
74 * Locks assumed : sched_mutex
76 void awaitEvent(rtsBool wait); /* In Select.c */
78 /* wakeUpSleepingThreads(nat ticks)
80 * Wakes up any sleeping threads whose timers have expired.
82 * Called from STG : NO
83 * Locks assumed : sched_mutex
85 rtsBool wakeUpSleepingThreads(nat); /* In Select.c */
87 /* wakeBlockedWorkerThread()
89 * If a worker thread is currently blocked in awaitEvent(), interrupt it.
91 * Called from STG : NO
92 * Locks assumed : sched_mutex
94 void wakeBlockedWorkerThread(void); /* In Select.c */
96 /* resetWorkerWakeupPipeAfterFork()
98 * Notify Select.c that a fork() has occured
100 * Called from STG : NO
101 * Locks assumed : don't care, but must be called right after fork()
103 void resetWorkerWakeupPipeAfterFork(void); /* In Select.c */
105 /* GetRoots(evac_fn f)
107 * Call f() for each root known to the scheduler.
109 * Called from STG : NO
110 * Locks assumed : ????
112 void GetRoots(evac_fn);
114 // ToDo: check whether all fcts below are used in the SMP version, too
116 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
117 void unlink_from_bq(StgTSO* tso, StgClosure* node);
118 void initThread(StgTSO *tso, nat stack_size, StgInt pri);
120 nat run_queue_len(void);
121 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
122 void initThread(StgTSO *tso, nat stack_size);
124 char *info_type(StgClosure *closure); // dummy
125 char *info_type_by_ip(StgInfoTable *ip); // dummy
126 void awaken_blocked_queue(StgTSO *q);
127 void initThread(StgTSO *tso, nat stack_size);
130 /* Context switch flag.
131 * Locks required : sched_mutex
133 extern int RTS_VAR(context_switch);
134 extern rtsBool RTS_VAR(interrupted);
137 extern nat RTS_VAR(timestamp);
140 * Locks required : sched_mutex
142 * In GranSim we have one run/blocked_queue per PE.
145 // run_queue_hds defined in GranSim.h
147 extern StgTSO *RTS_VAR(run_queue_hd), *RTS_VAR(run_queue_tl);
148 extern StgTSO *RTS_VAR(blocked_queue_hd), *RTS_VAR(blocked_queue_tl);
149 extern StgTSO *RTS_VAR(sleeping_queue);
151 /* Linked list of all threads. */
152 extern StgTSO *RTS_VAR(all_threads);
154 #if defined(RTS_SUPPORTS_THREADS)
155 /* Schedule.c has detailed info on what these do */
156 extern Mutex RTS_VAR(sched_mutex);
157 extern Condition RTS_VAR(returning_worker_cond);
158 extern nat RTS_VAR(rts_n_waiting_workers);
159 extern nat RTS_VAR(rts_n_waiting_tasks);
162 StgBool isThreadBound(StgTSO *tso);
164 extern SchedulerStatus rts_mainLazyIO(HaskellObj p, /*out*/HaskellObj *ret);
167 /* Called by shutdown_handler(). */
168 void interruptStgRts ( void );
170 void raiseAsync(StgTSO *tso, StgClosure *exception);
171 nat run_queue_len(void);
173 void resurrectThreads( StgTSO * );
177 * These are the threads which clients have requested that we run.
179 * In a 'threaded' build, each of these corresponds to one bound thread.
180 * The pointer to the StgMainThread is passed as a parameter to schedule;
181 * this invocation of schedule will always pass this main thread's
182 * bound_thread_cond to waitForkWorkCapability; OS-thread-switching
183 * takes place using passCapability.
185 * In non-threaded builds, clients are strictly nested: the first client calls
186 * into the RTS, which might call out again to C with a _ccall_GC, and
187 * eventually re-enter the RTS.
189 * This is non-abstract at the moment because the garbage collector
190 * treats pointers to TSOs from the main thread list as "weak" - these
191 * pointers won't prevent a thread from receiving a BlockedOnDeadMVar
194 * Main threads information is kept in a linked list:
196 typedef struct StgMainThread_ {
198 SchedulerStatus stat;
200 #if defined(RTS_SUPPORTS_THREADS)
201 #if defined(THREADED_RTS)
202 Condition bound_thread_cond;
207 struct StgMainThread_ *prev;
208 struct StgMainThread_ *link;
211 /* Main thread queue.
212 * Locks required: sched_mutex.
214 extern StgMainThread *main_threads;
216 void printAllThreads(void);
217 #ifdef COMPILING_SCHEDULER
218 static void printThreadBlockage(StgTSO *tso);
219 static void printThreadStatus(StgTSO *tso);
224 void print_bq (StgClosure *node);
227 void print_bqe (StgBlockingQueueElement *bqe);
230 void labelThread(StgPtr tso, char *label);
232 /* -----------------------------------------------------------------------------
233 * Some convenient macros...
236 /* END_TSO_QUEUE and friends now defined in includes/StgMiscClosures.h */
238 /* Add a thread to the end of the run queue.
239 * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
241 #define APPEND_TO_RUN_QUEUE(tso) \
242 ASSERT(tso->link == END_TSO_QUEUE); \
243 if (run_queue_hd == END_TSO_QUEUE) { \
244 run_queue_hd = tso; \
246 run_queue_tl->link = tso; \
250 /* Push a thread on the beginning of the run queue. Used for
251 * newly awakened threads, so they get run as soon as possible.
253 #define PUSH_ON_RUN_QUEUE(tso) \
254 tso->link = run_queue_hd; \
255 run_queue_hd = tso; \
256 if (run_queue_tl == END_TSO_QUEUE) { \
257 run_queue_tl = tso; \
260 /* Pop the first thread off the runnable queue.
262 #define POP_RUN_QUEUE(pt) \
263 do { StgTSO *__tmp_t = run_queue_hd; \
264 if (__tmp_t != END_TSO_QUEUE) { \
265 run_queue_hd = __tmp_t->link; \
266 __tmp_t->link = END_TSO_QUEUE; \
267 if (run_queue_hd == END_TSO_QUEUE) { \
268 run_queue_tl = END_TSO_QUEUE; \
274 /* Add a thread to the end of the blocked queue.
276 #define APPEND_TO_BLOCKED_QUEUE(tso) \
277 ASSERT(tso->link == END_TSO_QUEUE); \
278 if (blocked_queue_hd == END_TSO_QUEUE) { \
279 blocked_queue_hd = tso; \
281 blocked_queue_tl->link = tso; \
283 blocked_queue_tl = tso;
285 /* Check whether various thread queues are empty
287 #define EMPTY_QUEUE(q) (q == END_TSO_QUEUE)
289 #define EMPTY_RUN_QUEUE() (EMPTY_QUEUE(run_queue_hd))
290 #define EMPTY_BLOCKED_QUEUE() (EMPTY_QUEUE(blocked_queue_hd))
291 #define EMPTY_SLEEPING_QUEUE() (EMPTY_QUEUE(sleeping_queue))
293 #define EMPTY_THREAD_QUEUES() (EMPTY_RUN_QUEUE() && \
294 EMPTY_BLOCKED_QUEUE() && \
295 EMPTY_SLEEPING_QUEUE())
297 #if defined(RTS_SUPPORTS_THREADS)
298 /* If no task is waiting for a capability,
299 * and if there is work to be done
300 * or if we need to wait for IO or delay requests,
301 * spawn a new worker thread.
304 startSchedulerTaskIfNecessary(void);
308 extern void sched_belch(char *s, ...)
309 GNU_ATTRIBUTE(format (printf, 1, 2));
312 #endif /* __SCHEDULE_H__ */