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 (rtsBool wait_foreign);
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);
69 * Entry point for a new worker task.
70 * Called from STG : NO
71 * Locks assumed : none
73 void workerStart(Task *task);
76 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
77 void unlink_from_bq(StgTSO* tso, StgClosure* node);
78 void initThread(StgTSO *tso, nat stack_size, StgInt pri);
80 nat run_queue_len(void);
81 void awaken_blocked_queue(StgBlockingQueueElement *q, StgClosure *node);
82 void initThread(StgTSO *tso, nat stack_size);
84 char *info_type(StgClosure *closure); // dummy
85 char *info_type_by_ip(StgInfoTable *ip); // dummy
86 void awaken_blocked_queue(StgTSO *q);
87 void initThread(StgTSO *tso, nat stack_size);
90 /* Context switch flag.
91 * Locks required : none (conflicts are harmless)
93 extern int RTS_VAR(context_switch);
95 /* The state of the scheduler. This is used to control the sequence
96 * of events during shutdown, and when the runtime is interrupted
99 #define SCHED_RUNNING 0 /* running as normal */
100 #define SCHED_INTERRUPTING 1 /* ^C detected, before threads are deleted */
101 #define SCHED_SHUTTING_DOWN 2 /* final shutdown */
103 extern rtsBool RTS_VAR(sched_state);
106 * flag that tracks whether we have done any execution in this time slice.
108 #define ACTIVITY_YES 0 /* there has been activity in the current slice */
109 #define ACTIVITY_MAYBE_NO 1 /* no activity in the current slice */
110 #define ACTIVITY_INACTIVE 2 /* a complete slice has passed with no activity */
111 #define ACTIVITY_DONE_GC 3 /* like 2, but we've done a GC too */
113 /* Recent activity flag.
114 * Locks required : Transition from MAYBE_NO to INACTIVE
115 * happens in the timer signal, so it is atomic. Trnasition from
116 * INACTIVE to DONE_GC happens under sched_mutex. No lock required
117 * to set it to ACTIVITY_YES.
119 extern nat recent_activity;
122 * Locks required : sched_mutex
124 * In GranSim we have one run/blocked_queue per PE.
127 // run_queue_hds defined in GranSim.h
129 extern StgTSO *RTS_VAR(blackhole_queue);
130 #if !defined(THREADED_RTS)
131 extern StgTSO *RTS_VAR(blocked_queue_hd), *RTS_VAR(blocked_queue_tl);
132 extern StgTSO *RTS_VAR(sleeping_queue);
136 /* Linked list of all threads.
137 * Locks required : sched_mutex
139 extern StgTSO *RTS_VAR(all_threads);
141 /* Set to rtsTrue if there are threads on the blackhole_queue, and
142 * it is possible that one or more of them may be available to run.
143 * This flag is set to rtsFalse after we've checked the queue, and
144 * set to rtsTrue just before we run some Haskell code. It is used
145 * to decide whether we should yield the Capability or not.
146 * Locks required : none (see scheduleCheckBlackHoles()).
148 extern rtsBool blackholes_need_checking;
150 #if defined(THREADED_RTS)
151 extern Mutex RTS_VAR(sched_mutex);
154 SchedulerStatus rts_mainLazyIO(HaskellObj p, /*out*/HaskellObj *ret);
156 /* Called by shutdown_handler(). */
157 void interruptStgRts (void);
159 nat run_queue_len (void);
161 void resurrectThreads (StgTSO *);
163 void printAllThreads(void);
168 void print_bq (StgClosure *node);
171 void print_bqe (StgBlockingQueueElement *bqe);
174 /* -----------------------------------------------------------------------------
175 * Some convenient macros/inline functions...
180 /* END_TSO_QUEUE and friends now defined in includes/StgMiscClosures.h */
182 /* Add a thread to the end of the run queue.
183 * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
184 * ASSUMES: cap->running_task is the current task.
187 appendToRunQueue (Capability *cap, StgTSO *tso)
189 ASSERT(tso->_link == END_TSO_QUEUE);
190 if (cap->run_queue_hd == END_TSO_QUEUE) {
191 cap->run_queue_hd = tso;
193 setTSOLink(cap, cap->run_queue_tl, tso);
195 cap->run_queue_tl = tso;
198 /* Push a thread on the beginning of the run queue. Used for
199 * newly awakened threads, so they get run as soon as possible.
200 * ASSUMES: cap->running_task is the current task.
203 pushOnRunQueue (Capability *cap, StgTSO *tso)
205 setTSOLink(cap, tso, cap->run_queue_hd);
206 cap->run_queue_hd = tso;
207 if (cap->run_queue_tl == END_TSO_QUEUE) {
208 cap->run_queue_tl = tso;
212 /* Pop the first thread off the runnable queue.
214 INLINE_HEADER StgTSO *
215 popRunQueue (Capability *cap)
217 StgTSO *t = cap->run_queue_hd;
218 ASSERT(t != END_TSO_QUEUE);
219 cap->run_queue_hd = t->_link;
220 t->_link = END_TSO_QUEUE; // no write barrier req'd
221 if (cap->run_queue_hd == END_TSO_QUEUE) {
222 cap->run_queue_tl = END_TSO_QUEUE;
227 /* Add a thread to the end of the blocked queue.
229 #if !defined(THREADED_RTS)
231 appendToBlockedQueue(StgTSO *tso)
233 ASSERT(tso->_link == END_TSO_QUEUE);
234 if (blocked_queue_hd == END_TSO_QUEUE) {
235 blocked_queue_hd = tso;
237 setTSOLink(&MainCapability, blocked_queue_tl, tso);
239 blocked_queue_tl = tso;
243 #if defined(THREADED_RTS)
245 appendToWakeupQueue (Capability *cap, StgTSO *tso)
247 ASSERT(tso->_link == END_TSO_QUEUE);
248 if (cap->wakeup_queue_hd == END_TSO_QUEUE) {
249 cap->wakeup_queue_hd = tso;
251 setTSOLink(cap, cap->wakeup_queue_tl, tso);
253 cap->wakeup_queue_tl = tso;
257 /* Check whether various thread queues are empty
259 INLINE_HEADER rtsBool
260 emptyQueue (StgTSO *q)
262 return (q == END_TSO_QUEUE);
265 INLINE_HEADER rtsBool
266 emptyRunQueue(Capability *cap)
268 return emptyQueue(cap->run_queue_hd);
271 #if defined(THREADED_RTS)
272 INLINE_HEADER rtsBool
273 emptyWakeupQueue(Capability *cap)
275 return emptyQueue(cap->wakeup_queue_hd);
279 #if !defined(THREADED_RTS)
280 #define EMPTY_BLOCKED_QUEUE() (emptyQueue(blocked_queue_hd))
281 #define EMPTY_SLEEPING_QUEUE() (emptyQueue(sleeping_queue))
284 INLINE_HEADER rtsBool
285 emptyThreadQueues(Capability *cap)
287 return emptyRunQueue(cap)
288 #if !defined(THREADED_RTS)
289 && EMPTY_BLOCKED_QUEUE() && EMPTY_SLEEPING_QUEUE()
294 #endif /* !IN_STG_CODE */
296 #endif /* SCHEDULE_H */