hadrian: eliminate most of the remaining big rule enumerations
[ghc.git] / rts / Messages.c
1 /* ---------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 2010
4 *
5 * Inter-Capability message passing
6 *
7 * --------------------------------------------------------------------------*/
8
9 #include "Rts.h"
10 #include "Messages.h"
11 #include "Trace.h"
12 #include "Capability.h"
13 #include "Schedule.h"
14 #include "Threads.h"
15 #include "RaiseAsync.h"
16 #include "sm/Storage.h"
17
18 /* ----------------------------------------------------------------------------
19 Send a message to another Capability
20 ------------------------------------------------------------------------- */
21
22 #if defined(THREADED_RTS)
23
24 void sendMessage(Capability *from_cap, Capability *to_cap, Message *msg)
25 {
26 ACQUIRE_LOCK(&to_cap->lock);
27
28 #if defined(DEBUG)
29 {
30 const StgInfoTable *i = msg->header.info;
31 if (i != &stg_MSG_THROWTO_info &&
32 i != &stg_MSG_BLACKHOLE_info &&
33 i != &stg_MSG_TRY_WAKEUP_info &&
34 i != &stg_IND_info && // can happen if a MSG_BLACKHOLE is revoked
35 i != &stg_WHITEHOLE_info) {
36 barf("sendMessage: %p", i);
37 }
38 }
39 #endif
40
41 msg->link = to_cap->inbox;
42 to_cap->inbox = msg;
43
44 recordClosureMutated(from_cap,(StgClosure*)msg);
45
46 if (to_cap->running_task == NULL) {
47 to_cap->running_task = myTask();
48 // precond for releaseCapability_()
49 releaseCapability_(to_cap,false);
50 } else {
51 interruptCapability(to_cap);
52 }
53
54 RELEASE_LOCK(&to_cap->lock);
55 }
56
57 #endif /* THREADED_RTS */
58
59 /* ----------------------------------------------------------------------------
60 Handle a message
61 ------------------------------------------------------------------------- */
62
63 #if defined(THREADED_RTS)
64
65 void
66 executeMessage (Capability *cap, Message *m)
67 {
68 const StgInfoTable *i;
69
70 loop:
71 write_barrier(); // allow m->header to be modified by another thread
72 i = m->header.info;
73 if (i == &stg_MSG_TRY_WAKEUP_info)
74 {
75 StgTSO *tso = ((MessageWakeup *)m)->tso;
76 debugTraceCap(DEBUG_sched, cap, "message: try wakeup thread %ld",
77 (W_)tso->id);
78 tryWakeupThread(cap, tso);
79 }
80 else if (i == &stg_MSG_THROWTO_info)
81 {
82 MessageThrowTo *t = (MessageThrowTo *)m;
83 uint32_t r;
84 const StgInfoTable *i;
85
86 i = lockClosure((StgClosure*)m);
87 if (i != &stg_MSG_THROWTO_info) {
88 unlockClosure((StgClosure*)m, i);
89 goto loop;
90 }
91
92 debugTraceCap(DEBUG_sched, cap, "message: throwTo %ld -> %ld",
93 (W_)t->source->id, (W_)t->target->id);
94
95 ASSERT(t->source->why_blocked == BlockedOnMsgThrowTo);
96 ASSERT(t->source->block_info.closure == (StgClosure *)m);
97
98 r = throwToMsg(cap, t);
99
100 switch (r) {
101 case THROWTO_SUCCESS: {
102 // this message is done
103 StgTSO *source = t->source;
104 doneWithMsgThrowTo(t);
105 tryWakeupThread(cap, source);
106 break;
107 }
108 case THROWTO_BLOCKED:
109 // unlock the message
110 unlockClosure((StgClosure*)m, &stg_MSG_THROWTO_info);
111 break;
112 }
113 }
114 else if (i == &stg_MSG_BLACKHOLE_info)
115 {
116 uint32_t r;
117 MessageBlackHole *b = (MessageBlackHole*)m;
118
119 r = messageBlackHole(cap, b);
120 if (r == 0) {
121 tryWakeupThread(cap, b->tso);
122 }
123 return;
124 }
125 else if (i == &stg_IND_info || i == &stg_MSG_NULL_info)
126 {
127 // message was revoked
128 return;
129 }
130 else if (i == &stg_WHITEHOLE_info)
131 {
132 #if defined(PROF_SPIN)
133 ++whitehole_executeMessage_spin;
134 #endif
135 goto loop;
136 }
137 else
138 {
139 barf("executeMessage: %p", i);
140 }
141 }
142
143 #endif
144
145 /* ----------------------------------------------------------------------------
146 Handle a MSG_BLACKHOLE message
147
148 This is called from two places: either we just entered a BLACKHOLE
149 (stg_BLACKHOLE_info), or we received a MSG_BLACKHOLE in our
150 cap->inbox.
151
152 We need to establish whether the BLACKHOLE belongs to
153 this Capability, and
154 - if so, arrange to block the current thread on it
155 - otherwise, forward the message to the right place
156
157 Returns:
158 - 0 if the blocked thread can be woken up by the caller
159 - 1 if the thread is still blocked, and we promise to send a MSG_TRY_WAKEUP
160 at some point in the future.
161
162 ------------------------------------------------------------------------- */
163
164 uint32_t messageBlackHole(Capability *cap, MessageBlackHole *msg)
165 {
166 const StgInfoTable *info;
167 StgClosure *p;
168 StgBlockingQueue *bq;
169 StgClosure *bh = UNTAG_CLOSURE(msg->bh);
170 StgTSO *owner;
171
172 debugTraceCap(DEBUG_sched, cap, "message: thread %d blocking on "
173 "blackhole %p", (W_)msg->tso->id, msg->bh);
174
175 info = bh->header.info;
176
177 // If we got this message in our inbox, it might be that the
178 // BLACKHOLE has already been updated, and GC has shorted out the
179 // indirection, so the pointer no longer points to a BLACKHOLE at
180 // all.
181 if (info != &stg_BLACKHOLE_info &&
182 info != &stg_CAF_BLACKHOLE_info &&
183 info != &__stg_EAGER_BLACKHOLE_info &&
184 info != &stg_WHITEHOLE_info) {
185 // if it is a WHITEHOLE, then a thread is in the process of
186 // trying to BLACKHOLE it. But we know that it was once a
187 // BLACKHOLE, so there is at least a valid pointer in the
188 // payload, so we can carry on.
189 return 0;
190 }
191
192 // The blackhole must indirect to a TSO, a BLOCKING_QUEUE, an IND,
193 // or a value.
194 loop:
195 // NB. VOLATILE_LOAD(), because otherwise gcc hoists the load
196 // and turns this into an infinite loop.
197 p = UNTAG_CLOSURE((StgClosure*)VOLATILE_LOAD(&((StgInd*)bh)->indirectee));
198 info = p->header.info;
199
200 if (info == &stg_IND_info)
201 {
202 // This could happen, if e.g. we got a BLOCKING_QUEUE that has
203 // just been replaced with an IND by another thread in
204 // updateThunk(). In which case, if we read the indirectee
205 // again we should get the value.
206 // See Note [BLACKHOLE pointing to IND] in sm/Evac.c
207 goto loop;
208 }
209
210 else if (info == &stg_TSO_info)
211 {
212 owner = (StgTSO*)p;
213
214 #if defined(THREADED_RTS)
215 if (owner->cap != cap) {
216 sendMessage(cap, owner->cap, (Message*)msg);
217 debugTraceCap(DEBUG_sched, cap, "forwarding message to cap %d",
218 owner->cap->no);
219 return 1;
220 }
221 #endif
222 // owner is the owner of the BLACKHOLE, and resides on this
223 // Capability. msg->tso is the first thread to block on this
224 // BLACKHOLE, so we first create a BLOCKING_QUEUE object.
225
226 bq = (StgBlockingQueue*)allocate(cap, sizeofW(StgBlockingQueue));
227
228 // initialise the BLOCKING_QUEUE object
229 SET_HDR(bq, &stg_BLOCKING_QUEUE_DIRTY_info, CCS_SYSTEM);
230 bq->bh = bh;
231 bq->queue = msg;
232 bq->owner = owner;
233
234 msg->link = (MessageBlackHole*)END_TSO_QUEUE;
235
236 // All BLOCKING_QUEUES are linked in a list on owner->bq, so
237 // that we can search through them in the event that there is
238 // a collision to update a BLACKHOLE and a BLOCKING_QUEUE
239 // becomes orphaned (see updateThunk()).
240 bq->link = owner->bq;
241 owner->bq = bq;
242 dirty_TSO(cap, owner); // we modified owner->bq
243
244 // If the owner of the blackhole is currently runnable, then
245 // bump it to the front of the run queue. This gives the
246 // blocked-on thread a little boost which should help unblock
247 // this thread, and may avoid a pile-up of other threads
248 // becoming blocked on the same BLACKHOLE (#3838).
249 //
250 // NB. we check to make sure that the owner is not the same as
251 // the current thread, since in that case it will not be on
252 // the run queue.
253 if (owner->why_blocked == NotBlocked && owner->id != msg->tso->id) {
254 promoteInRunQueue(cap, owner);
255 }
256
257 // point to the BLOCKING_QUEUE from the BLACKHOLE
258 write_barrier(); // make the BQ visible
259 ((StgInd*)bh)->indirectee = (StgClosure *)bq;
260 recordClosureMutated(cap,bh); // bh was mutated
261
262 debugTraceCap(DEBUG_sched, cap, "thread %d blocked on thread %d",
263 (W_)msg->tso->id, (W_)owner->id);
264
265 return 1; // blocked
266 }
267 else if (info == &stg_BLOCKING_QUEUE_CLEAN_info ||
268 info == &stg_BLOCKING_QUEUE_DIRTY_info)
269 {
270 StgBlockingQueue *bq = (StgBlockingQueue *)p;
271
272 ASSERT(bq->bh == bh);
273
274 owner = bq->owner;
275
276 ASSERT(owner != END_TSO_QUEUE);
277
278 #if defined(THREADED_RTS)
279 if (owner->cap != cap) {
280 sendMessage(cap, owner->cap, (Message*)msg);
281 debugTraceCap(DEBUG_sched, cap, "forwarding message to cap %d",
282 owner->cap->no);
283 return 1;
284 }
285 #endif
286
287 msg->link = bq->queue;
288 bq->queue = msg;
289 recordClosureMutated(cap,(StgClosure*)msg);
290
291 if (info == &stg_BLOCKING_QUEUE_CLEAN_info) {
292 bq->header.info = &stg_BLOCKING_QUEUE_DIRTY_info;
293 recordClosureMutated(cap,(StgClosure*)bq);
294 }
295
296 debugTraceCap(DEBUG_sched, cap,
297 "thread %d blocked on existing BLOCKING_QUEUE "
298 "owned by thread %d",
299 (W_)msg->tso->id, (W_)owner->id);
300
301 // See above, #3838
302 if (owner->why_blocked == NotBlocked && owner->id != msg->tso->id) {
303 promoteInRunQueue(cap, owner);
304 }
305
306 return 1; // blocked
307 }
308
309 return 0; // not blocked
310 }
311
312 // A shorter version of messageBlackHole(), that just returns the
313 // owner (or NULL if the owner cannot be found, because the blackhole
314 // has been updated in the meantime).
315
316 StgTSO * blackHoleOwner (StgClosure *bh)
317 {
318 const StgInfoTable *info;
319 StgClosure *p;
320
321 info = bh->header.info;
322
323 if (info != &stg_BLACKHOLE_info &&
324 info != &stg_CAF_BLACKHOLE_info &&
325 info != &__stg_EAGER_BLACKHOLE_info &&
326 info != &stg_WHITEHOLE_info) {
327 return NULL;
328 }
329
330 // The blackhole must indirect to a TSO, a BLOCKING_QUEUE, an IND,
331 // or a value.
332 loop:
333 // NB. VOLATILE_LOAD(), because otherwise gcc hoists the load
334 // and turns this into an infinite loop.
335 p = UNTAG_CLOSURE((StgClosure*)VOLATILE_LOAD(&((StgInd*)bh)->indirectee));
336 info = p->header.info;
337
338 if (info == &stg_IND_info) goto loop;
339
340 else if (info == &stg_TSO_info)
341 {
342 return (StgTSO*)p;
343 }
344 else if (info == &stg_BLOCKING_QUEUE_CLEAN_info ||
345 info == &stg_BLOCKING_QUEUE_DIRTY_info)
346 {
347 StgBlockingQueue *bq = (StgBlockingQueue *)p;
348 return bq->owner;
349 }
350
351 return NULL; // not blocked
352 }