rts/Linker.c: declare 'deRefStablePtr' as an exported 'rts' symbol
[ghc.git] / rts / Schedule.h
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team 1998-2005
4 *
5 * Prototypes for functions in Schedule.c
6 * (RTS internal scheduler interface)
7 *
8 * -------------------------------------------------------------------------*/
9
10 #ifndef SCHEDULE_H
11 #define SCHEDULE_H
12
13 #include "rts/OSThreads.h"
14 #include "Capability.h"
15 #include "Trace.h"
16
17 #include "BeginPrivate.h"
18
19 /* initScheduler(), exitScheduler()
20 * Called from STG : no
21 * Locks assumed : none
22 */
23 void initScheduler (void);
24 void exitScheduler (rtsBool wait_foreign);
25 void freeScheduler (void);
26 void markScheduler (evac_fn evac, void *user);
27
28 // Place a new thread on the run queue of the current Capability
29 void scheduleThread (Capability *cap, StgTSO *tso);
30
31 // Place a new thread on the run queue of a specified Capability
32 // (cap is the currently owned Capability, cpu is the number of
33 // the desired Capability).
34 void scheduleThreadOn(Capability *cap, StgWord cpu, StgTSO *tso);
35
36 /* wakeUpRts()
37 *
38 * Causes an OS thread to wake up and run the scheduler, if necessary.
39 */
40 #if defined(THREADED_RTS)
41 void wakeUpRts(void);
42 #endif
43
44 /* raiseExceptionHelper */
45 StgWord raiseExceptionHelper (StgRegTable *reg, StgTSO *tso, StgClosure *exception);
46
47 /* findRetryFrameHelper */
48 StgWord findRetryFrameHelper (Capability *cap, StgTSO *tso);
49
50 /* Entry point for a new worker */
51 void scheduleWorker (Capability *cap, Task *task);
52
53 /* The state of the scheduler. This is used to control the sequence
54 * of events during shutdown, and when the runtime is interrupted
55 * using ^C.
56 */
57 #define SCHED_RUNNING 0 /* running as normal */
58 #define SCHED_INTERRUPTING 1 /* ^C detected, before threads are deleted */
59 #define SCHED_SHUTTING_DOWN 2 /* final shutdown */
60
61 extern volatile StgWord sched_state;
62
63 /*
64 * flag that tracks whether we have done any execution in this time
65 * slice, and controls the disabling of the interval timer.
66 *
67 * The timer interrupt transitions ACTIVITY_YES into
68 * ACTIVITY_MAYBE_NO, waits for RtsFlags.GcFlags.idleGCDelayTime,
69 * and then:
70 * - if idle GC is no, set ACTIVITY_INACTIVE and wakeUpRts()
71 * - if idle GC is off, set ACTIVITY_DONE_GC and stopTimer()
72 *
73 * If the scheduler finds ACTIVITY_INACTIVE, then it sets
74 * ACTIVITY_DONE_GC, performs the GC and calls stopTimer().
75 *
76 * If the scheduler finds ACTIVITY_DONE_GC and it has a thread to run,
77 * it enables the timer again with startTimer().
78 */
79 #define ACTIVITY_YES 0
80 // the RTS is active
81 #define ACTIVITY_MAYBE_NO 1
82 // no activity since the last timer signal
83 #define ACTIVITY_INACTIVE 2
84 // RtsFlags.GcFlags.idleGCDelayTime has passed with no activity
85 #define ACTIVITY_DONE_GC 3
86 // like ACTIVITY_INACTIVE, but we've done a GC too (if idle GC is
87 // enabled) and the interval timer is now turned off.
88
89 /* Recent activity flag.
90 * Locks required : Transition from MAYBE_NO to INACTIVE
91 * happens in the timer signal, so it is atomic. Trnasition from
92 * INACTIVE to DONE_GC happens under sched_mutex. No lock required
93 * to set it to ACTIVITY_YES.
94 */
95 extern volatile StgWord recent_activity;
96
97 /* Thread queues.
98 * Locks required : sched_mutex
99 *
100 * In GranSim we have one run/blocked_queue per PE.
101 */
102 extern StgTSO *blackhole_queue;
103 #if !defined(THREADED_RTS)
104 extern StgTSO *blocked_queue_hd, *blocked_queue_tl;
105 extern StgTSO *sleeping_queue;
106 #endif
107
108 extern rtsBool heap_overflow;
109
110 #if defined(THREADED_RTS)
111 extern Mutex sched_mutex;
112 #endif
113
114 /* Called by shutdown_handler(). */
115 void interruptStgRts (void);
116
117 void resurrectThreads (StgTSO *);
118
119 /* -----------------------------------------------------------------------------
120 * Some convenient macros/inline functions...
121 */
122
123 #if !IN_STG_CODE
124
125 /* END_TSO_QUEUE and friends now defined in includes/stg/MiscClosures.h */
126
127 /* Add a thread to the end of the run queue.
128 * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
129 * ASSUMES: cap->running_task is the current task.
130 */
131 EXTERN_INLINE void
132 appendToRunQueue (Capability *cap, StgTSO *tso);
133
134 EXTERN_INLINE void
135 appendToRunQueue (Capability *cap, StgTSO *tso)
136 {
137 ASSERT(tso->_link == END_TSO_QUEUE);
138 if (cap->run_queue_hd == END_TSO_QUEUE) {
139 cap->run_queue_hd = tso;
140 tso->block_info.prev = END_TSO_QUEUE;
141 } else {
142 setTSOLink(cap, cap->run_queue_tl, tso);
143 setTSOPrev(cap, tso, cap->run_queue_tl);
144 }
145 cap->run_queue_tl = tso;
146 }
147
148 /* Push a thread on the beginning of the run queue.
149 * ASSUMES: cap->running_task is the current task.
150 */
151 EXTERN_INLINE void
152 pushOnRunQueue (Capability *cap, StgTSO *tso);
153
154 EXTERN_INLINE void
155 pushOnRunQueue (Capability *cap, StgTSO *tso)
156 {
157 setTSOLink(cap, tso, cap->run_queue_hd);
158 tso->block_info.prev = END_TSO_QUEUE;
159 if (cap->run_queue_hd != END_TSO_QUEUE) {
160 setTSOPrev(cap, cap->run_queue_hd, tso);
161 }
162 cap->run_queue_hd = tso;
163 if (cap->run_queue_tl == END_TSO_QUEUE) {
164 cap->run_queue_tl = tso;
165 }
166 }
167
168 /* Pop the first thread off the runnable queue.
169 */
170 INLINE_HEADER StgTSO *
171 popRunQueue (Capability *cap)
172 {
173 StgTSO *t = cap->run_queue_hd;
174 ASSERT(t != END_TSO_QUEUE);
175 cap->run_queue_hd = t->_link;
176 if (t->_link != END_TSO_QUEUE) {
177 t->_link->block_info.prev = END_TSO_QUEUE;
178 }
179 t->_link = END_TSO_QUEUE; // no write barrier req'd
180 if (cap->run_queue_hd == END_TSO_QUEUE) {
181 cap->run_queue_tl = END_TSO_QUEUE;
182 }
183 return t;
184 }
185
186 INLINE_HEADER StgTSO *
187 peekRunQueue (Capability *cap)
188 {
189 return cap->run_queue_hd;
190 }
191
192 void removeFromRunQueue (Capability *cap, StgTSO *tso);
193 extern void promoteInRunQueue (Capability *cap, StgTSO *tso);
194
195 /* Add a thread to the end of the blocked queue.
196 */
197 #if !defined(THREADED_RTS)
198 INLINE_HEADER void
199 appendToBlockedQueue(StgTSO *tso)
200 {
201 ASSERT(tso->_link == END_TSO_QUEUE);
202 if (blocked_queue_hd == END_TSO_QUEUE) {
203 blocked_queue_hd = tso;
204 } else {
205 setTSOLink(&MainCapability, blocked_queue_tl, tso);
206 }
207 blocked_queue_tl = tso;
208 }
209 #endif
210
211 /* Check whether various thread queues are empty
212 */
213 INLINE_HEADER rtsBool
214 emptyQueue (StgTSO *q)
215 {
216 return (q == END_TSO_QUEUE);
217 }
218
219 INLINE_HEADER rtsBool
220 emptyRunQueue(Capability *cap)
221 {
222 return emptyQueue(cap->run_queue_hd);
223 }
224
225 /* assumes that the queue is not empty; so combine this with
226 * an emptyRunQueue check! */
227 INLINE_HEADER rtsBool
228 singletonRunQueue(Capability *cap)
229 {
230 ASSERT(!emptyRunQueue(cap));
231 return cap->run_queue_hd->_link == END_TSO_QUEUE;
232 }
233
234 INLINE_HEADER void
235 truncateRunQueue(Capability *cap)
236 {
237 cap->run_queue_hd = END_TSO_QUEUE;
238 cap->run_queue_tl = END_TSO_QUEUE;
239 }
240
241 #if !defined(THREADED_RTS)
242 #define EMPTY_BLOCKED_QUEUE() (emptyQueue(blocked_queue_hd))
243 #define EMPTY_SLEEPING_QUEUE() (emptyQueue(sleeping_queue))
244 #endif
245
246 INLINE_HEADER rtsBool
247 emptyThreadQueues(Capability *cap)
248 {
249 return emptyRunQueue(cap)
250 #if !defined(THREADED_RTS)
251 && EMPTY_BLOCKED_QUEUE() && EMPTY_SLEEPING_QUEUE()
252 #endif
253 ;
254 }
255
256 #endif /* !IN_STG_CODE */
257
258 #include "EndPrivate.h"
259
260 #endif /* SCHEDULE_H */
261
262
263 // Local Variables:
264 // mode: C
265 // fill-column: 80
266 // indent-tabs-mode: nil
267 // c-basic-offset: 4
268 // buffer-file-coding-system: utf-8-unix
269 // End: