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 * Put the specified thread on the run queue of the given Capability.
49 * Called from STG : yes
50 * Locks assumed : we own the Capability.
52 StgTSO * unblockOne(Capability *cap, StgTSO *tso);
56 * Raises an exception asynchronously in the specified thread.
58 * Called from STG : yes
59 * Locks assumed : none
61 void raiseAsync(Capability *cap, StgTSO *tso, StgClosure *exception);
63 /* suspendComputation()
65 * A variant of raiseAsync(), this strips the stack of the specified
66 * thread down to the stop_here point, leaving a current closure on
67 * top of the stack at [stop_here - 1].
69 void suspendComputation(Capability *cap, StgTSO *tso, StgPtr stop_here);
71 /* raiseExceptionHelper */
72 StgWord raiseExceptionHelper (StgRegTable *reg, StgTSO *tso, StgClosure *exception);
74 /* findRetryFrameHelper */
75 StgWord findRetryFrameHelper (StgTSO *tso);
77 /* GetRoots(evac_fn f)
79 * Call f() for each root known to the scheduler.
81 * Called from STG : NO
82 * Locks assumed : ????
84 void GetRoots(evac_fn);
88 * Entry point for a new worker task.
89 * Called from STG : NO
90 * Locks assumed : none
92 void workerStart(Task *task);
95 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
96 void unlink_from_bq(StgTSO* tso, StgClosure* node);
97 void initThread(StgTSO *tso, nat stack_size, StgInt pri);
99 nat run_queue_len(void);
100 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
101 void initThread(StgTSO *tso, nat stack_size);
103 char *info_type(StgClosure *closure); // dummy
104 char *info_type_by_ip(StgInfoTable *ip); // dummy
105 void awaken_blocked_queue(StgTSO *q);
106 void initThread(StgTSO *tso, nat stack_size);
109 /* Context switch flag.
110 * Locks required : none (conflicts are harmless)
112 extern int RTS_VAR(context_switch);
114 /* The state of the scheduler. This is used to control the sequence
115 * of events during shutdown, and when the runtime is interrupted
118 #define SCHED_RUNNING 0 /* running as normal */
119 #define SCHED_INTERRUPTING 1 /* ^C detected, before threads are deleted */
120 #define SCHED_INTERRUPTED 2 /* ^C detected, after threads deleted */
121 #define SCHED_SHUTTING_DOWN 3 /* final shutdown */
123 extern rtsBool RTS_VAR(sched_state);
126 * flag that tracks whether we have done any execution in this time slice.
128 #define ACTIVITY_YES 0 /* there has been activity in the current slice */
129 #define ACTIVITY_MAYBE_NO 1 /* no activity in the current slice */
130 #define ACTIVITY_INACTIVE 2 /* a complete slice has passed with no activity */
131 #define ACTIVITY_DONE_GC 3 /* like 2, but we've done a GC too */
133 /* Recent activity flag.
134 * Locks required : Transition from MAYBE_NO to INACTIVE
135 * happens in the timer signal, so it is atomic. Trnasition from
136 * INACTIVE to DONE_GC happens under sched_mutex. No lock required
137 * to set it to ACTIVITY_YES.
139 extern nat recent_activity;
142 * Locks required : sched_mutex
144 * In GranSim we have one run/blocked_queue per PE.
147 // run_queue_hds defined in GranSim.h
149 extern StgTSO *RTS_VAR(blackhole_queue);
150 #if !defined(THREADED_RTS)
151 extern StgTSO *RTS_VAR(blocked_queue_hd), *RTS_VAR(blocked_queue_tl);
152 extern StgTSO *RTS_VAR(sleeping_queue);
156 /* Linked list of all threads.
157 * Locks required : sched_mutex
159 extern StgTSO *RTS_VAR(all_threads);
161 /* Set to rtsTrue if there are threads on the blackhole_queue, and
162 * it is possible that one or more of them may be available to run.
163 * This flag is set to rtsFalse after we've checked the queue, and
164 * set to rtsTrue just before we run some Haskell code. It is used
165 * to decide whether we should yield the Capability or not.
166 * Locks required : none (see scheduleCheckBlackHoles()).
168 extern rtsBool blackholes_need_checking;
170 #if defined(THREADED_RTS)
171 extern Mutex RTS_VAR(sched_mutex);
174 StgBool isThreadBound(StgTSO *tso);
176 SchedulerStatus rts_mainLazyIO(HaskellObj p, /*out*/HaskellObj *ret);
178 /* Called by shutdown_handler(). */
179 void interruptStgRts (void);
181 nat run_queue_len (void);
183 void resurrectThreads (StgTSO *);
185 void printAllThreads(void);
190 void print_bq (StgClosure *node);
193 void print_bqe (StgBlockingQueueElement *bqe);
196 void labelThread(StgPtr tso, char *label);
198 /* -----------------------------------------------------------------------------
199 * Some convenient macros/inline functions...
204 /* END_TSO_QUEUE and friends now defined in includes/StgMiscClosures.h */
206 /* Add a thread to the end of the run queue.
207 * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
208 * ASSUMES: cap->running_task is the current task.
211 appendToRunQueue (Capability *cap, StgTSO *tso)
213 ASSERT(tso->link == END_TSO_QUEUE);
214 if (cap->run_queue_hd == END_TSO_QUEUE) {
215 cap->run_queue_hd = tso;
217 cap->run_queue_tl->link = tso;
219 cap->run_queue_tl = tso;
222 /* Push a thread on the beginning of the run queue. Used for
223 * newly awakened threads, so they get run as soon as possible.
224 * ASSUMES: cap->running_task is the current task.
227 pushOnRunQueue (Capability *cap, StgTSO *tso)
229 tso->link = cap->run_queue_hd;
230 cap->run_queue_hd = tso;
231 if (cap->run_queue_tl == END_TSO_QUEUE) {
232 cap->run_queue_tl = tso;
236 /* Pop the first thread off the runnable queue.
238 STATIC_INLINE StgTSO *
239 popRunQueue (Capability *cap)
241 StgTSO *t = cap->run_queue_hd;
242 ASSERT(t != END_TSO_QUEUE);
243 cap->run_queue_hd = t->link;
244 t->link = END_TSO_QUEUE;
245 if (cap->run_queue_hd == END_TSO_QUEUE) {
246 cap->run_queue_tl = END_TSO_QUEUE;
251 /* Add a thread to the end of the blocked queue.
253 #if !defined(THREADED_RTS)
255 appendToBlockedQueue(StgTSO *tso)
257 ASSERT(tso->link == END_TSO_QUEUE);
258 if (blocked_queue_hd == END_TSO_QUEUE) {
259 blocked_queue_hd = tso;
261 blocked_queue_tl->link = tso;
263 blocked_queue_tl = tso;
267 #if defined(THREADED_RTS)
269 appendToWakeupQueue (Capability *cap, StgTSO *tso)
271 ASSERT(tso->link == END_TSO_QUEUE);
272 if (cap->wakeup_queue_hd == END_TSO_QUEUE) {
273 cap->wakeup_queue_hd = tso;
275 cap->wakeup_queue_tl->link = tso;
277 cap->wakeup_queue_tl = tso;
281 /* Check whether various thread queues are empty
283 STATIC_INLINE rtsBool
284 emptyQueue (StgTSO *q)
286 return (q == END_TSO_QUEUE);
289 STATIC_INLINE rtsBool
290 emptyRunQueue(Capability *cap)
292 return emptyQueue(cap->run_queue_hd);
295 #if defined(THREADED_RTS)
296 STATIC_INLINE rtsBool
297 emptyWakeupQueue(Capability *cap)
299 return emptyQueue(cap->wakeup_queue_hd);
303 #if !defined(THREADED_RTS)
304 #define EMPTY_BLOCKED_QUEUE() (emptyQueue(blocked_queue_hd))
305 #define EMPTY_SLEEPING_QUEUE() (emptyQueue(sleeping_queue))
308 STATIC_INLINE rtsBool
309 emptyThreadQueues(Capability *cap)
311 return emptyRunQueue(cap)
312 #if !defined(THREADED_RTS)
313 && EMPTY_BLOCKED_QUEUE() && EMPTY_SLEEPING_QUEUE()
319 void sched_belch(char *s, ...)
320 GNU_ATTRIBUTE(format (printf, 1, 2));
323 #endif /* !IN_STG_CODE */
326 dirtyTSO (StgTSO *tso)
328 tso->flags |= TSO_DIRTY;
331 #endif /* SCHEDULE_H */