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);
23 // Place a new thread on the run queue of the current Capability
24 void scheduleThread (Capability *cap, StgTSO *tso);
26 // Place a new thread on the run queue of a specified Capability
27 // (cap is the currently owned Capability, cpu is the number of
28 // the desired Capability).
29 void scheduleThreadOn(Capability *cap, StgWord cpu, StgTSO *tso);
31 /* awakenBlockedQueue()
33 * Takes a pointer to the beginning of a blocked TSO queue, and
34 * wakes up the entire queue.
35 * Called from STG : yes
36 * Locks assumed : none
39 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
41 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
43 void awakenBlockedQueue (Capability *cap, StgTSO *tso);
48 * Causes an OS thread to wake up and run the scheduler, if necessary.
54 * Put the specified thread on the run queue of the given Capability.
55 * Called from STG : yes
56 * Locks assumed : we own the Capability.
58 StgTSO * unblockOne (Capability *cap, StgTSO *tso);
60 /* raiseExceptionHelper */
61 StgWord raiseExceptionHelper (StgRegTable *reg, StgTSO *tso, StgClosure *exception);
63 /* findRetryFrameHelper */
64 StgWord findRetryFrameHelper (StgTSO *tso);
66 /* GetRoots(evac_fn f)
68 * Call f() for each root known to the scheduler.
70 * Called from STG : NO
71 * Locks assumed : ????
73 void GetRoots(evac_fn);
77 * Entry point for a new worker task.
78 * Called from STG : NO
79 * Locks assumed : none
81 void workerStart(Task *task);
84 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
85 void unlink_from_bq(StgTSO* tso, StgClosure* node);
86 void initThread(StgTSO *tso, nat stack_size, StgInt pri);
88 nat run_queue_len(void);
89 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
90 void initThread(StgTSO *tso, nat stack_size);
92 char *info_type(StgClosure *closure); // dummy
93 char *info_type_by_ip(StgInfoTable *ip); // dummy
94 void awaken_blocked_queue(StgTSO *q);
95 void initThread(StgTSO *tso, nat stack_size);
98 /* Context switch flag.
99 * Locks required : none (conflicts are harmless)
101 extern int RTS_VAR(context_switch);
103 /* The state of the scheduler. This is used to control the sequence
104 * of events during shutdown, and when the runtime is interrupted
107 #define SCHED_RUNNING 0 /* running as normal */
108 #define SCHED_INTERRUPTING 1 /* ^C detected, before threads are deleted */
109 #define SCHED_SHUTTING_DOWN 2 /* final shutdown */
111 extern rtsBool RTS_VAR(sched_state);
114 * flag that tracks whether we have done any execution in this time slice.
116 #define ACTIVITY_YES 0 /* there has been activity in the current slice */
117 #define ACTIVITY_MAYBE_NO 1 /* no activity in the current slice */
118 #define ACTIVITY_INACTIVE 2 /* a complete slice has passed with no activity */
119 #define ACTIVITY_DONE_GC 3 /* like 2, but we've done a GC too */
121 /* Recent activity flag.
122 * Locks required : Transition from MAYBE_NO to INACTIVE
123 * happens in the timer signal, so it is atomic. Trnasition from
124 * INACTIVE to DONE_GC happens under sched_mutex. No lock required
125 * to set it to ACTIVITY_YES.
127 extern nat recent_activity;
130 * Locks required : sched_mutex
132 * In GranSim we have one run/blocked_queue per PE.
135 // run_queue_hds defined in GranSim.h
137 extern StgTSO *RTS_VAR(blackhole_queue);
138 #if !defined(THREADED_RTS)
139 extern StgTSO *RTS_VAR(blocked_queue_hd), *RTS_VAR(blocked_queue_tl);
140 extern StgTSO *RTS_VAR(sleeping_queue);
144 /* Linked list of all threads.
145 * Locks required : sched_mutex
147 extern StgTSO *RTS_VAR(all_threads);
149 /* Set to rtsTrue if there are threads on the blackhole_queue, and
150 * it is possible that one or more of them may be available to run.
151 * This flag is set to rtsFalse after we've checked the queue, and
152 * set to rtsTrue just before we run some Haskell code. It is used
153 * to decide whether we should yield the Capability or not.
154 * Locks required : none (see scheduleCheckBlackHoles()).
156 extern rtsBool blackholes_need_checking;
158 #if defined(THREADED_RTS)
159 extern Mutex RTS_VAR(sched_mutex);
162 SchedulerStatus rts_mainLazyIO(HaskellObj p, /*out*/HaskellObj *ret);
164 /* Called by shutdown_handler(). */
165 void interruptStgRts (void);
167 nat run_queue_len (void);
169 void resurrectThreads (StgTSO *);
171 void printAllThreads(void);
176 void print_bq (StgClosure *node);
179 void print_bqe (StgBlockingQueueElement *bqe);
182 /* -----------------------------------------------------------------------------
183 * Some convenient macros/inline functions...
188 /* END_TSO_QUEUE and friends now defined in includes/StgMiscClosures.h */
190 /* Add a thread to the end of the run queue.
191 * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
192 * ASSUMES: cap->running_task is the current task.
195 appendToRunQueue (Capability *cap, StgTSO *tso)
197 ASSERT(tso->link == END_TSO_QUEUE);
198 if (cap->run_queue_hd == END_TSO_QUEUE) {
199 cap->run_queue_hd = tso;
201 cap->run_queue_tl->link = tso;
203 cap->run_queue_tl = tso;
206 /* Push a thread on the beginning of the run queue. Used for
207 * newly awakened threads, so they get run as soon as possible.
208 * ASSUMES: cap->running_task is the current task.
211 pushOnRunQueue (Capability *cap, StgTSO *tso)
213 tso->link = cap->run_queue_hd;
214 cap->run_queue_hd = tso;
215 if (cap->run_queue_tl == END_TSO_QUEUE) {
216 cap->run_queue_tl = tso;
220 /* Pop the first thread off the runnable queue.
222 STATIC_INLINE StgTSO *
223 popRunQueue (Capability *cap)
225 StgTSO *t = cap->run_queue_hd;
226 ASSERT(t != END_TSO_QUEUE);
227 cap->run_queue_hd = t->link;
228 t->link = END_TSO_QUEUE;
229 if (cap->run_queue_hd == END_TSO_QUEUE) {
230 cap->run_queue_tl = END_TSO_QUEUE;
235 /* Add a thread to the end of the blocked queue.
237 #if !defined(THREADED_RTS)
239 appendToBlockedQueue(StgTSO *tso)
241 ASSERT(tso->link == END_TSO_QUEUE);
242 if (blocked_queue_hd == END_TSO_QUEUE) {
243 blocked_queue_hd = tso;
245 blocked_queue_tl->link = tso;
247 blocked_queue_tl = tso;
251 #if defined(THREADED_RTS)
253 appendToWakeupQueue (Capability *cap, StgTSO *tso)
255 ASSERT(tso->link == END_TSO_QUEUE);
256 if (cap->wakeup_queue_hd == END_TSO_QUEUE) {
257 cap->wakeup_queue_hd = tso;
259 cap->wakeup_queue_tl->link = tso;
261 cap->wakeup_queue_tl = tso;
265 /* Check whether various thread queues are empty
267 STATIC_INLINE rtsBool
268 emptyQueue (StgTSO *q)
270 return (q == END_TSO_QUEUE);
273 STATIC_INLINE rtsBool
274 emptyRunQueue(Capability *cap)
276 return emptyQueue(cap->run_queue_hd);
279 #if defined(THREADED_RTS)
280 STATIC_INLINE rtsBool
281 emptyWakeupQueue(Capability *cap)
283 return emptyQueue(cap->wakeup_queue_hd);
287 #if !defined(THREADED_RTS)
288 #define EMPTY_BLOCKED_QUEUE() (emptyQueue(blocked_queue_hd))
289 #define EMPTY_SLEEPING_QUEUE() (emptyQueue(sleeping_queue))
292 STATIC_INLINE rtsBool
293 emptyThreadQueues(Capability *cap)
295 return emptyRunQueue(cap)
296 #if !defined(THREADED_RTS)
297 && EMPTY_BLOCKED_QUEUE() && EMPTY_SLEEPING_QUEUE()
302 #endif /* !IN_STG_CODE */
305 dirtyTSO (StgTSO *tso)
307 tso->flags |= TSO_DIRTY;
310 #endif /* SCHEDULE_H */