Move a thread to the front of the run queue when another thread blocks on it
[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 BEGIN_RTS_PRIVATE
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
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 (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, and when the runtime is interrupted
54 * using ^C.
55 */
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 */
59
60 extern volatile StgWord sched_state;
61
62 /*
63 * flag that tracks whether we have done any execution in this time slice.
64 */
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 */
69
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.
75 */
76 extern volatile StgWord recent_activity;
77
78 /* Thread queues.
79 * Locks required : sched_mutex
80 *
81 * In GranSim we have one run/blocked_queue per PE.
82 */
83 extern StgTSO *blackhole_queue;
84 #if !defined(THREADED_RTS)
85 extern StgTSO *blocked_queue_hd, *blocked_queue_tl;
86 extern StgTSO *sleeping_queue;
87 #endif
88
89 extern rtsBool heap_overflow;
90
91 #if defined(THREADED_RTS)
92 extern Mutex sched_mutex;
93 #endif
94
95 /* Called by shutdown_handler(). */
96 void interruptStgRts (void);
97
98 void resurrectThreads (StgTSO *);
99
100 /* -----------------------------------------------------------------------------
101 * Some convenient macros/inline functions...
102 */
103
104 #if !IN_STG_CODE
105
106 /* END_TSO_QUEUE and friends now defined in includes/StgMiscClosures.h */
107
108 /* Add a thread to the end of the run queue.
109 * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
110 * ASSUMES: cap->running_task is the current task.
111 */
112 EXTERN_INLINE void
113 appendToRunQueue (Capability *cap, StgTSO *tso);
114
115 EXTERN_INLINE void
116 appendToRunQueue (Capability *cap, StgTSO *tso)
117 {
118 ASSERT(tso->_link == END_TSO_QUEUE);
119 if (cap->run_queue_hd == END_TSO_QUEUE) {
120 cap->run_queue_hd = tso;
121 tso->block_info.prev = END_TSO_QUEUE;
122 } else {
123 setTSOLink(cap, cap->run_queue_tl, tso);
124 setTSOPrev(cap, tso, cap->run_queue_tl);
125 }
126 cap->run_queue_tl = tso;
127 traceEventThreadRunnable (cap, tso);
128 }
129
130 /* Push a thread on the beginning of the run queue.
131 * ASSUMES: cap->running_task is the current task.
132 */
133 EXTERN_INLINE void
134 pushOnRunQueue (Capability *cap, StgTSO *tso);
135
136 EXTERN_INLINE void
137 pushOnRunQueue (Capability *cap, StgTSO *tso)
138 {
139 setTSOLink(cap, tso, cap->run_queue_hd);
140 tso->block_info.prev = END_TSO_QUEUE;
141 if (cap->run_queue_hd != END_TSO_QUEUE) {
142 setTSOPrev(cap, cap->run_queue_hd, tso);
143 }
144 cap->run_queue_hd = tso;
145 if (cap->run_queue_tl == END_TSO_QUEUE) {
146 cap->run_queue_tl = tso;
147 }
148 }
149
150 /* Pop the first thread off the runnable queue.
151 */
152 INLINE_HEADER StgTSO *
153 popRunQueue (Capability *cap)
154 {
155 StgTSO *t = cap->run_queue_hd;
156 ASSERT(t != END_TSO_QUEUE);
157 cap->run_queue_hd = t->_link;
158 cap->run_queue_hd->block_info.prev = END_TSO_QUEUE;
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;
162 }
163 return t;
164 }
165
166 extern void removeFromRunQueue (Capability *cap, StgTSO *tso);
167
168 /* Add a thread to the end of the blocked queue.
169 */
170 #if !defined(THREADED_RTS)
171 INLINE_HEADER void
172 appendToBlockedQueue(StgTSO *tso)
173 {
174 ASSERT(tso->_link == END_TSO_QUEUE);
175 if (blocked_queue_hd == END_TSO_QUEUE) {
176 blocked_queue_hd = tso;
177 } else {
178 setTSOLink(&MainCapability, blocked_queue_tl, tso);
179 }
180 blocked_queue_tl = tso;
181 }
182 #endif
183
184 /* Check whether various thread queues are empty
185 */
186 INLINE_HEADER rtsBool
187 emptyQueue (StgTSO *q)
188 {
189 return (q == END_TSO_QUEUE);
190 }
191
192 INLINE_HEADER rtsBool
193 emptyRunQueue(Capability *cap)
194 {
195 return emptyQueue(cap->run_queue_hd);
196 }
197
198 #if !defined(THREADED_RTS)
199 #define EMPTY_BLOCKED_QUEUE() (emptyQueue(blocked_queue_hd))
200 #define EMPTY_SLEEPING_QUEUE() (emptyQueue(sleeping_queue))
201 #endif
202
203 INLINE_HEADER rtsBool
204 emptyThreadQueues(Capability *cap)
205 {
206 return emptyRunQueue(cap)
207 #if !defined(THREADED_RTS)
208 && EMPTY_BLOCKED_QUEUE() && EMPTY_SLEEPING_QUEUE()
209 #endif
210 ;
211 }
212
213 #endif /* !IN_STG_CODE */
214
215 END_RTS_PRIVATE
216
217 #endif /* SCHEDULE_H */
218