1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-2005
5 * Prototypes for functions in Schedule.c
6 * (RTS internal scheduler interface)
8 * -------------------------------------------------------------------------*/
13 #include "OSThreads.h"
14 #include "Capability.h"
16 /* initScheduler(), exitScheduler()
17 * Called from STG : no
18 * Locks assumed : none
20 void initScheduler (void);
21 void exitScheduler (void);
22 void freeScheduler (void);
24 // Place a new thread on the run queue of the current Capability
25 void scheduleThread (Capability *cap, StgTSO *tso);
27 // Place a new thread on the run queue of a specified Capability
28 // (cap is the currently owned Capability, cpu is the number of
29 // the desired Capability).
30 void scheduleThreadOn(Capability *cap, StgWord cpu, StgTSO *tso);
32 /* awakenBlockedQueue()
34 * Takes a pointer to the beginning of a blocked TSO queue, and
35 * wakes up the entire queue.
36 * Called from STG : yes
37 * Locks assumed : none
40 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
42 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
44 void awakenBlockedQueue (Capability *cap, StgTSO *tso);
49 * Causes an OS thread to wake up and run the scheduler, if necessary.
55 * Put the specified thread on the run queue of the given Capability.
56 * Called from STG : yes
57 * Locks assumed : we own the Capability.
59 StgTSO * unblockOne (Capability *cap, StgTSO *tso);
61 /* raiseExceptionHelper */
62 StgWord raiseExceptionHelper (StgRegTable *reg, StgTSO *tso, StgClosure *exception);
64 /* findRetryFrameHelper */
65 StgWord findRetryFrameHelper (StgTSO *tso);
67 /* GetRoots(evac_fn f)
69 * Call f() for each root known to the scheduler.
71 * Called from STG : NO
72 * Locks assumed : ????
74 void GetRoots(evac_fn);
78 * Entry point for a new worker task.
79 * Called from STG : NO
80 * Locks assumed : none
82 void workerStart(Task *task);
85 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
86 void unlink_from_bq(StgTSO* tso, StgClosure* node);
87 void initThread(StgTSO *tso, nat stack_size, StgInt pri);
89 nat run_queue_len(void);
90 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
91 void initThread(StgTSO *tso, nat stack_size);
93 char *info_type(StgClosure *closure); // dummy
94 char *info_type_by_ip(StgInfoTable *ip); // dummy
95 void awaken_blocked_queue(StgTSO *q);
96 void initThread(StgTSO *tso, nat stack_size);
99 /* Context switch flag.
100 * Locks required : none (conflicts are harmless)
102 extern int RTS_VAR(context_switch);
104 /* The state of the scheduler. This is used to control the sequence
105 * of events during shutdown, and when the runtime is interrupted
108 #define SCHED_RUNNING 0 /* running as normal */
109 #define SCHED_INTERRUPTING 1 /* ^C detected, before threads are deleted */
110 #define SCHED_SHUTTING_DOWN 2 /* final shutdown */
112 extern rtsBool RTS_VAR(sched_state);
115 * flag that tracks whether we have done any execution in this time slice.
117 #define ACTIVITY_YES 0 /* there has been activity in the current slice */
118 #define ACTIVITY_MAYBE_NO 1 /* no activity in the current slice */
119 #define ACTIVITY_INACTIVE 2 /* a complete slice has passed with no activity */
120 #define ACTIVITY_DONE_GC 3 /* like 2, but we've done a GC too */
122 /* Recent activity flag.
123 * Locks required : Transition from MAYBE_NO to INACTIVE
124 * happens in the timer signal, so it is atomic. Trnasition from
125 * INACTIVE to DONE_GC happens under sched_mutex. No lock required
126 * to set it to ACTIVITY_YES.
128 extern nat recent_activity;
131 * Locks required : sched_mutex
133 * In GranSim we have one run/blocked_queue per PE.
136 // run_queue_hds defined in GranSim.h
138 extern StgTSO *RTS_VAR(blackhole_queue);
139 #if !defined(THREADED_RTS)
140 extern StgTSO *RTS_VAR(blocked_queue_hd), *RTS_VAR(blocked_queue_tl);
141 extern StgTSO *RTS_VAR(sleeping_queue);
145 /* Linked list of all threads.
146 * Locks required : sched_mutex
148 extern StgTSO *RTS_VAR(all_threads);
150 /* Set to rtsTrue if there are threads on the blackhole_queue, and
151 * it is possible that one or more of them may be available to run.
152 * This flag is set to rtsFalse after we've checked the queue, and
153 * set to rtsTrue just before we run some Haskell code. It is used
154 * to decide whether we should yield the Capability or not.
155 * Locks required : none (see scheduleCheckBlackHoles()).
157 extern rtsBool blackholes_need_checking;
159 #if defined(THREADED_RTS)
160 extern Mutex RTS_VAR(sched_mutex);
163 SchedulerStatus rts_mainLazyIO(HaskellObj p, /*out*/HaskellObj *ret);
165 /* Called by shutdown_handler(). */
166 void interruptStgRts (void);
168 nat run_queue_len (void);
170 void resurrectThreads (StgTSO *);
172 void printAllThreads(void);
177 void print_bq (StgClosure *node);
180 void print_bqe (StgBlockingQueueElement *bqe);
183 /* -----------------------------------------------------------------------------
184 * Some convenient macros/inline functions...
189 /* END_TSO_QUEUE and friends now defined in includes/StgMiscClosures.h */
191 /* Add a thread to the end of the run queue.
192 * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
193 * ASSUMES: cap->running_task is the current task.
196 appendToRunQueue (Capability *cap, StgTSO *tso)
198 ASSERT(tso->link == END_TSO_QUEUE);
199 if (cap->run_queue_hd == END_TSO_QUEUE) {
200 cap->run_queue_hd = tso;
202 cap->run_queue_tl->link = tso;
204 cap->run_queue_tl = tso;
207 /* Push a thread on the beginning of the run queue. Used for
208 * newly awakened threads, so they get run as soon as possible.
209 * ASSUMES: cap->running_task is the current task.
212 pushOnRunQueue (Capability *cap, StgTSO *tso)
214 tso->link = cap->run_queue_hd;
215 cap->run_queue_hd = tso;
216 if (cap->run_queue_tl == END_TSO_QUEUE) {
217 cap->run_queue_tl = tso;
221 /* Pop the first thread off the runnable queue.
223 INLINE_HEADER StgTSO *
224 popRunQueue (Capability *cap)
226 StgTSO *t = cap->run_queue_hd;
227 ASSERT(t != END_TSO_QUEUE);
228 cap->run_queue_hd = t->link;
229 t->link = END_TSO_QUEUE;
230 if (cap->run_queue_hd == END_TSO_QUEUE) {
231 cap->run_queue_tl = END_TSO_QUEUE;
236 /* Add a thread to the end of the blocked queue.
238 #if !defined(THREADED_RTS)
240 appendToBlockedQueue(StgTSO *tso)
242 ASSERT(tso->link == END_TSO_QUEUE);
243 if (blocked_queue_hd == END_TSO_QUEUE) {
244 blocked_queue_hd = tso;
246 blocked_queue_tl->link = tso;
248 blocked_queue_tl = tso;
252 #if defined(THREADED_RTS)
254 appendToWakeupQueue (Capability *cap, StgTSO *tso)
256 ASSERT(tso->link == END_TSO_QUEUE);
257 if (cap->wakeup_queue_hd == END_TSO_QUEUE) {
258 cap->wakeup_queue_hd = tso;
260 cap->wakeup_queue_tl->link = tso;
262 cap->wakeup_queue_tl = tso;
266 /* Check whether various thread queues are empty
268 INLINE_HEADER rtsBool
269 emptyQueue (StgTSO *q)
271 return (q == END_TSO_QUEUE);
274 INLINE_HEADER rtsBool
275 emptyRunQueue(Capability *cap)
277 return emptyQueue(cap->run_queue_hd);
280 #if defined(THREADED_RTS)
281 INLINE_HEADER rtsBool
282 emptyWakeupQueue(Capability *cap)
284 return emptyQueue(cap->wakeup_queue_hd);
288 #if !defined(THREADED_RTS)
289 #define EMPTY_BLOCKED_QUEUE() (emptyQueue(blocked_queue_hd))
290 #define EMPTY_SLEEPING_QUEUE() (emptyQueue(sleeping_queue))
293 INLINE_HEADER rtsBool
294 emptyThreadQueues(Capability *cap)
296 return emptyRunQueue(cap)
297 #if !defined(THREADED_RTS)
298 && EMPTY_BLOCKED_QUEUE() && EMPTY_SLEEPING_QUEUE()
303 #endif /* !IN_STG_CODE */
306 dirtyTSO (StgTSO *tso)
308 tso->flags |= TSO_DIRTY;
311 #endif /* SCHEDULE_H */