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