1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-2005
5 * Prototypes for functions in Schedule.c
6 * (RTS internal scheduler interface)
8 * -------------------------------------------------------------------------*/
13 #include "rts/OSThreads.h"
14 #include "Capability.h"
19 /* initScheduler(), exitScheduler()
20 * Called from STG : no
21 * Locks assumed : none
23 void initScheduler (void);
24 void exitScheduler (rtsBool wait_foreign);
25 void freeScheduler (void);
27 // Place a new thread on the run queue of the current Capability
28 void scheduleThread (Capability *cap, StgTSO *tso);
30 // Place a new thread on the run queue of a specified Capability
31 // (cap is the currently owned Capability, cpu is the number of
32 // the desired Capability).
33 void scheduleThreadOn(Capability *cap, StgWord cpu, StgTSO *tso);
37 * Causes an OS thread to wake up and run the scheduler, if necessary.
39 #if defined(THREADED_RTS)
43 /* raiseExceptionHelper */
44 StgWord raiseExceptionHelper (StgRegTable *reg, StgTSO *tso, StgClosure *exception);
46 /* findRetryFrameHelper */
47 StgWord findRetryFrameHelper (StgTSO *tso);
49 /* Entry point for a new worker */
50 void scheduleWorker (Capability *cap, Task *task);
52 /* The state of the scheduler. This is used to control the sequence
53 * of events during shutdown, and when the runtime is interrupted
56 #define SCHED_RUNNING 0 /* running as normal */
57 #define SCHED_INTERRUPTING 1 /* ^C detected, before threads are deleted */
58 #define SCHED_SHUTTING_DOWN 2 /* final shutdown */
60 extern volatile StgWord sched_state;
63 * flag that tracks whether we have done any execution in this time slice.
65 #define ACTIVITY_YES 0 /* there has been activity in the current slice */
66 #define ACTIVITY_MAYBE_NO 1 /* no activity in the current slice */
67 #define ACTIVITY_INACTIVE 2 /* a complete slice has passed with no activity */
68 #define ACTIVITY_DONE_GC 3 /* like 2, but we've done a GC too */
70 /* Recent activity flag.
71 * Locks required : Transition from MAYBE_NO to INACTIVE
72 * happens in the timer signal, so it is atomic. Trnasition from
73 * INACTIVE to DONE_GC happens under sched_mutex. No lock required
74 * to set it to ACTIVITY_YES.
76 extern volatile StgWord recent_activity;
79 * Locks required : sched_mutex
81 * In GranSim we have one run/blocked_queue per PE.
83 extern StgTSO *blackhole_queue;
84 #if !defined(THREADED_RTS)
85 extern StgTSO *blocked_queue_hd, *blocked_queue_tl;
86 extern StgTSO *sleeping_queue;
89 /* Set to rtsTrue if there are threads on the blackhole_queue, and
90 * it is possible that one or more of them may be available to run.
91 * This flag is set to rtsFalse after we've checked the queue, and
92 * set to rtsTrue just before we run some Haskell code. It is used
93 * to decide whether we should yield the Capability or not.
94 * Locks required : none (see scheduleCheckBlackHoles()).
96 extern rtsBool blackholes_need_checking;
98 extern rtsBool heap_overflow;
100 #if defined(THREADED_RTS)
101 extern Mutex sched_mutex;
104 /* Called by shutdown_handler(). */
105 void interruptStgRts (void);
107 void resurrectThreads (StgTSO *);
108 void performPendingThrowTos (StgTSO *);
110 /* -----------------------------------------------------------------------------
111 * Some convenient macros/inline functions...
116 /* END_TSO_QUEUE and friends now defined in includes/StgMiscClosures.h */
118 /* Add a thread to the end of the run queue.
119 * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
120 * ASSUMES: cap->running_task is the current task.
123 appendToRunQueue (Capability *cap, StgTSO *tso);
126 appendToRunQueue (Capability *cap, StgTSO *tso)
128 ASSERT(tso->_link == END_TSO_QUEUE);
129 if (cap->run_queue_hd == END_TSO_QUEUE) {
130 cap->run_queue_hd = tso;
132 setTSOLink(cap, cap->run_queue_tl, tso);
134 cap->run_queue_tl = tso;
135 traceEventThreadRunnable (cap, tso);
138 /* Push a thread on the beginning of the run queue.
139 * ASSUMES: cap->running_task is the current task.
142 pushOnRunQueue (Capability *cap, StgTSO *tso)
144 setTSOLink(cap, tso, cap->run_queue_hd);
145 cap->run_queue_hd = tso;
146 if (cap->run_queue_tl == END_TSO_QUEUE) {
147 cap->run_queue_tl = tso;
151 /* Pop the first thread off the runnable queue.
153 INLINE_HEADER StgTSO *
154 popRunQueue (Capability *cap)
156 StgTSO *t = cap->run_queue_hd;
157 ASSERT(t != END_TSO_QUEUE);
158 cap->run_queue_hd = t->_link;
159 t->_link = END_TSO_QUEUE; // no write barrier req'd
160 if (cap->run_queue_hd == END_TSO_QUEUE) {
161 cap->run_queue_tl = END_TSO_QUEUE;
166 /* Add a thread to the end of the blocked queue.
168 #if !defined(THREADED_RTS)
170 appendToBlockedQueue(StgTSO *tso)
172 ASSERT(tso->_link == END_TSO_QUEUE);
173 if (blocked_queue_hd == END_TSO_QUEUE) {
174 blocked_queue_hd = tso;
176 setTSOLink(&MainCapability, blocked_queue_tl, tso);
178 blocked_queue_tl = tso;
182 #if defined(THREADED_RTS)
183 // Assumes: my_cap is owned by the current Task. We hold
184 // other_cap->lock, but we do not necessarily own other_cap; another
185 // Task may be running on it.
187 appendToWakeupQueue (Capability *my_cap, Capability *other_cap, StgTSO *tso)
189 ASSERT(tso->_link == END_TSO_QUEUE);
190 if (other_cap->wakeup_queue_hd == END_TSO_QUEUE) {
191 other_cap->wakeup_queue_hd = tso;
193 // my_cap is passed to setTSOLink() because it may need to
194 // write to the mutable list.
195 setTSOLink(my_cap, other_cap->wakeup_queue_tl, tso);
197 other_cap->wakeup_queue_tl = tso;
201 /* Check whether various thread queues are empty
203 INLINE_HEADER rtsBool
204 emptyQueue (StgTSO *q)
206 return (q == END_TSO_QUEUE);
209 INLINE_HEADER rtsBool
210 emptyRunQueue(Capability *cap)
212 return emptyQueue(cap->run_queue_hd);
215 #if defined(THREADED_RTS)
216 INLINE_HEADER rtsBool
217 emptyWakeupQueue(Capability *cap)
219 return emptyQueue(cap->wakeup_queue_hd);
223 #if !defined(THREADED_RTS)
224 #define EMPTY_BLOCKED_QUEUE() (emptyQueue(blocked_queue_hd))
225 #define EMPTY_SLEEPING_QUEUE() (emptyQueue(sleeping_queue))
228 INLINE_HEADER rtsBool
229 emptyThreadQueues(Capability *cap)
231 return emptyRunQueue(cap)
232 #if !defined(THREADED_RTS)
233 && EMPTY_BLOCKED_QUEUE() && EMPTY_SLEEPING_QUEUE()
238 #endif /* !IN_STG_CODE */
242 #endif /* SCHEDULE_H */