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"
17 /* initScheduler(), exitScheduler()
18 * Called from STG : no
19 * Locks assumed : none
21 void initScheduler (void);
22 void exitScheduler (rtsBool wait_foreign);
23 void freeScheduler (void);
25 // Place a new thread on the run queue of the current Capability
26 void scheduleThread (Capability *cap, StgTSO *tso);
28 // Place a new thread on the run queue of a specified Capability
29 // (cap is the currently owned Capability, cpu is the number of
30 // the desired Capability).
31 void scheduleThreadOn(Capability *cap, StgWord cpu, StgTSO *tso);
33 /* awakenBlockedQueue()
35 * Takes a pointer to the beginning of a blocked TSO queue, and
36 * wakes up the entire queue.
37 * Called from STG : yes
38 * Locks assumed : none
41 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
43 void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
45 void awakenBlockedQueue (Capability *cap, StgTSO *tso);
50 * Causes an OS thread to wake up and run the scheduler, if necessary.
56 * Put the specified thread on the run queue of the given Capability.
57 * Called from STG : yes
58 * Locks assumed : we own the Capability.
60 StgTSO * unblockOne (Capability *cap, StgTSO *tso);
62 /* raiseExceptionHelper */
63 StgWord raiseExceptionHelper (StgRegTable *reg, StgTSO *tso, StgClosure *exception);
65 /* findRetryFrameHelper */
66 StgWord findRetryFrameHelper (StgTSO *tso);
70 * Entry point for a new worker task.
71 * Called from STG : NO
72 * Locks assumed : none
74 #if defined(THREADED_RTS)
75 void OSThreadProcAttr workerStart(Task *task);
79 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
80 void unlink_from_bq(StgTSO* tso, StgClosure* node);
81 void initThread(StgTSO *tso, nat stack_size, StgInt pri);
83 nat run_queue_len(void);
84 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
85 void initThread(StgTSO *tso, nat stack_size);
87 char *info_type(StgClosure *closure); // dummy
88 char *info_type_by_ip(StgInfoTable *ip); // dummy
89 void awaken_blocked_queue(StgTSO *q);
90 void initThread(StgTSO *tso, nat stack_size);
93 /* The state of the scheduler. This is used to control the sequence
94 * of events during shutdown, and when the runtime is interrupted
97 #define SCHED_RUNNING 0 /* running as normal */
98 #define SCHED_INTERRUPTING 1 /* ^C detected, before threads are deleted */
99 #define SCHED_SHUTTING_DOWN 2 /* final shutdown */
101 extern volatile StgWord RTS_VAR(sched_state);
104 * flag that tracks whether we have done any execution in this time slice.
106 #define ACTIVITY_YES 0 /* there has been activity in the current slice */
107 #define ACTIVITY_MAYBE_NO 1 /* no activity in the current slice */
108 #define ACTIVITY_INACTIVE 2 /* a complete slice has passed with no activity */
109 #define ACTIVITY_DONE_GC 3 /* like 2, but we've done a GC too */
111 /* Recent activity flag.
112 * Locks required : Transition from MAYBE_NO to INACTIVE
113 * happens in the timer signal, so it is atomic. Trnasition from
114 * INACTIVE to DONE_GC happens under sched_mutex. No lock required
115 * to set it to ACTIVITY_YES.
117 extern volatile StgWord recent_activity;
120 * Locks required : sched_mutex
122 * In GranSim we have one run/blocked_queue per PE.
125 // run_queue_hds defined in GranSim.h
127 extern StgTSO *RTS_VAR(blackhole_queue);
128 #if !defined(THREADED_RTS)
129 extern StgTSO *RTS_VAR(blocked_queue_hd), *RTS_VAR(blocked_queue_tl);
130 extern StgTSO *RTS_VAR(sleeping_queue);
134 /* Set to rtsTrue if there are threads on the blackhole_queue, and
135 * it is possible that one or more of them may be available to run.
136 * This flag is set to rtsFalse after we've checked the queue, and
137 * set to rtsTrue just before we run some Haskell code. It is used
138 * to decide whether we should yield the Capability or not.
139 * Locks required : none (see scheduleCheckBlackHoles()).
141 extern rtsBool blackholes_need_checking;
143 extern rtsBool heap_overflow;
145 #if defined(THREADED_RTS)
146 extern Mutex RTS_VAR(sched_mutex);
149 SchedulerStatus rts_mainLazyIO(HaskellObj p, /*out*/HaskellObj *ret);
151 /* Called by shutdown_handler(). */
152 void interruptStgRts (void);
154 nat run_queue_len (void);
156 void resurrectThreads (StgTSO *);
157 void performPendingThrowTos (StgTSO *);
159 void printAllThreads(void);
164 void print_bq (StgClosure *node);
167 void print_bqe (StgBlockingQueueElement *bqe);
170 /* -----------------------------------------------------------------------------
171 * Some convenient macros/inline functions...
176 /* END_TSO_QUEUE and friends now defined in includes/StgMiscClosures.h */
178 /* Add a thread to the end of the run queue.
179 * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
180 * ASSUMES: cap->running_task is the current task.
183 appendToRunQueue (Capability *cap, StgTSO *tso)
185 ASSERT(tso->_link == END_TSO_QUEUE);
186 if (cap->run_queue_hd == END_TSO_QUEUE) {
187 cap->run_queue_hd = tso;
189 setTSOLink(cap, cap->run_queue_tl, tso);
191 cap->run_queue_tl = tso;
192 postEvent (cap, EVENT_THREAD_RUNNABLE, tso->id, 0);
195 /* Push a thread on the beginning of the run queue.
196 * ASSUMES: cap->running_task is the current task.
199 pushOnRunQueue (Capability *cap, StgTSO *tso)
201 setTSOLink(cap, tso, cap->run_queue_hd);
202 cap->run_queue_hd = tso;
203 if (cap->run_queue_tl == END_TSO_QUEUE) {
204 cap->run_queue_tl = tso;
208 /* Pop the first thread off the runnable queue.
210 INLINE_HEADER StgTSO *
211 popRunQueue (Capability *cap)
213 StgTSO *t = cap->run_queue_hd;
214 ASSERT(t != END_TSO_QUEUE);
215 cap->run_queue_hd = t->_link;
216 t->_link = END_TSO_QUEUE; // no write barrier req'd
217 if (cap->run_queue_hd == END_TSO_QUEUE) {
218 cap->run_queue_tl = END_TSO_QUEUE;
223 /* Add a thread to the end of the blocked queue.
225 #if !defined(THREADED_RTS)
227 appendToBlockedQueue(StgTSO *tso)
229 ASSERT(tso->_link == END_TSO_QUEUE);
230 if (blocked_queue_hd == END_TSO_QUEUE) {
231 blocked_queue_hd = tso;
233 setTSOLink(&MainCapability, blocked_queue_tl, tso);
235 blocked_queue_tl = tso;
239 #if defined(THREADED_RTS)
240 // Assumes: my_cap is owned by the current Task. We hold
241 // other_cap->lock, but we do not necessarily own other_cap; another
242 // Task may be running on it.
244 appendToWakeupQueue (Capability *my_cap, Capability *other_cap, StgTSO *tso)
246 ASSERT(tso->_link == END_TSO_QUEUE);
247 if (other_cap->wakeup_queue_hd == END_TSO_QUEUE) {
248 other_cap->wakeup_queue_hd = tso;
250 // my_cap is passed to setTSOLink() because it may need to
251 // write to the mutable list.
252 setTSOLink(my_cap, other_cap->wakeup_queue_tl, tso);
254 other_cap->wakeup_queue_tl = tso;
258 /* Check whether various thread queues are empty
260 INLINE_HEADER rtsBool
261 emptyQueue (StgTSO *q)
263 return (q == END_TSO_QUEUE);
266 INLINE_HEADER rtsBool
267 emptyRunQueue(Capability *cap)
269 return emptyQueue(cap->run_queue_hd);
272 #if defined(THREADED_RTS)
273 INLINE_HEADER rtsBool
274 emptyWakeupQueue(Capability *cap)
276 return emptyQueue(cap->wakeup_queue_hd);
280 #if !defined(THREADED_RTS)
281 #define EMPTY_BLOCKED_QUEUE() (emptyQueue(blocked_queue_hd))
282 #define EMPTY_SLEEPING_QUEUE() (emptyQueue(sleeping_queue))
285 INLINE_HEADER rtsBool
286 emptyThreadQueues(Capability *cap)
288 return emptyRunQueue(cap)
289 #if !defined(THREADED_RTS)
290 && EMPTY_BLOCKED_QUEUE() && EMPTY_SLEEPING_QUEUE()
295 #endif /* !IN_STG_CODE */
297 #endif /* SCHEDULE_H */