1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 2010
5 * Inter-Capability message passing
7 * --------------------------------------------------------------------------*/
12 #include "Capability.h"
15 #include "RaiseAsync.h"
16 #include "sm/Storage.h"
18 /* ----------------------------------------------------------------------------
19 Send a message to another Capability
20 ------------------------------------------------------------------------- */
24 void sendMessage(Capability *from_cap, Capability *to_cap, Message *msg)
26 ACQUIRE_LOCK(&to_cap->lock);
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);
41 msg->link = to_cap->inbox;
44 recordClosureMutated(from_cap,(StgClosure*)msg);
46 if (to_cap->running_task == NULL) {
47 to_cap->running_task = myTask();
48 // precond for releaseCapability_()
49 releaseCapability_(to_cap,rtsFalse);
51 contextSwitchCapability(to_cap);
54 RELEASE_LOCK(&to_cap->lock);
57 #endif /* THREADED_RTS */
59 /* ----------------------------------------------------------------------------
61 ------------------------------------------------------------------------- */
66 executeMessage (Capability *cap, Message *m)
68 const StgInfoTable *i;
71 write_barrier(); // allow m->header to be modified by another thread
73 if (i == &stg_MSG_TRY_WAKEUP_info)
75 StgTSO *tso = ((MessageWakeup *)m)->tso;
76 debugTraceCap(DEBUG_sched, cap, "message: try wakeup thread %ld",
78 tryWakeupThread(cap, tso);
80 else if (i == &stg_MSG_THROWTO_info)
82 MessageThrowTo *t = (MessageThrowTo *)m;
84 const StgInfoTable *i;
86 i = lockClosure((StgClosure*)m);
87 if (i != &stg_MSG_THROWTO_info) {
88 unlockClosure((StgClosure*)m, i);
92 debugTraceCap(DEBUG_sched, cap, "message: throwTo %ld -> %ld",
93 (lnat)t->source->id, (lnat)t->target->id);
95 ASSERT(t->source->why_blocked == BlockedOnMsgThrowTo);
96 ASSERT(t->source->block_info.closure == (StgClosure *)m);
98 r = throwToMsg(cap, t);
101 case THROWTO_SUCCESS:
102 // this message is done
103 unlockClosure((StgClosure*)m, &stg_MSG_NULL_info);
104 tryWakeupThread(cap, t->source);
106 case THROWTO_BLOCKED:
107 // unlock the message
108 unlockClosure((StgClosure*)m, &stg_MSG_THROWTO_info);
112 else if (i == &stg_MSG_BLACKHOLE_info)
115 MessageBlackHole *b = (MessageBlackHole*)m;
117 r = messageBlackHole(cap, b);
119 tryWakeupThread(cap, b->tso);
123 else if (i == &stg_IND_info || i == &stg_MSG_NULL_info)
125 // message was revoked
128 else if (i == &stg_WHITEHOLE_info)
134 barf("executeMessage: %p", i);
140 /* ----------------------------------------------------------------------------
141 Handle a MSG_BLACKHOLE message
143 This is called from two places: either we just entered a BLACKHOLE
144 (stg_BLACKHOLE_info), or we received a MSG_BLACKHOLE in our
147 We need to establish whether the BLACKHOLE belongs to
149 - if so, arrange to block the current thread on it
150 - otherwise, forward the message to the right place
153 - 0 if the blocked thread can be woken up by the caller
154 - 1 if the thread is still blocked, and we promise to send a MSG_TRY_WAKEUP
155 at some point in the future.
157 ------------------------------------------------------------------------- */
159 nat messageBlackHole(Capability *cap, MessageBlackHole *msg)
161 const StgInfoTable *info;
163 StgBlockingQueue *bq;
164 StgClosure *bh = UNTAG_CLOSURE(msg->bh);
167 debugTraceCap(DEBUG_sched, cap, "message: thread %d blocking on blackhole %p",
168 (lnat)msg->tso->id, msg->bh);
170 info = bh->header.info;
172 // If we got this message in our inbox, it might be that the
173 // BLACKHOLE has already been updated, and GC has shorted out the
174 // indirection, so the pointer no longer points to a BLACKHOLE at
176 if (info != &stg_BLACKHOLE_info &&
177 info != &stg_CAF_BLACKHOLE_info &&
178 info != &stg_WHITEHOLE_info) {
179 // if it is a WHITEHOLE, then a thread is in the process of
180 // trying to BLACKHOLE it. But we know that it was once a
181 // BLACKHOLE, so there is at least a valid pointer in the
182 // payload, so we can carry on.
186 // The blackhole must indirect to a TSO, a BLOCKING_QUEUE, an IND,
189 // NB. VOLATILE_LOAD(), because otherwise gcc hoists the load
190 // and turns this into an infinite loop.
191 p = UNTAG_CLOSURE((StgClosure*)VOLATILE_LOAD(&((StgInd*)bh)->indirectee));
192 info = p->header.info;
194 if (info == &stg_IND_info)
196 // This could happen, if e.g. we got a BLOCKING_QUEUE that has
197 // just been replaced with an IND by another thread in
198 // updateThunk(). In which case, if we read the indirectee
199 // again we should get the value.
203 else if (info == &stg_TSO_info)
205 owner = deRefTSO((StgTSO *)p);
208 if (owner->cap != cap) {
209 sendMessage(cap, owner->cap, (Message*)msg);
210 debugTraceCap(DEBUG_sched, cap, "forwarding message to cap %d", owner->cap->no);
214 // owner is the owner of the BLACKHOLE, and resides on this
215 // Capability. msg->tso is the first thread to block on this
216 // BLACKHOLE, so we first create a BLOCKING_QUEUE object.
218 bq = (StgBlockingQueue*)allocate(cap, sizeofW(StgBlockingQueue));
220 // initialise the BLOCKING_QUEUE object
221 SET_HDR(bq, &stg_BLOCKING_QUEUE_DIRTY_info, CCS_SYSTEM);
226 msg->link = (MessageBlackHole*)END_TSO_QUEUE;
228 // All BLOCKING_QUEUES are linked in a list on owner->bq, so
229 // that we can search through them in the event that there is
230 // a collision to update a BLACKHOLE and a BLOCKING_QUEUE
231 // becomes orphaned (see updateThunk()).
232 bq->link = owner->bq;
234 dirty_TSO(cap, owner); // we modified owner->bq
236 // If the owner of the blackhole is currently runnable, then
237 // bump it to the front of the run queue. This gives the
238 // blocked-on thread a little boost which should help unblock
239 // this thread, and may avoid a pile-up of other threads
240 // becoming blocked on the same BLACKHOLE (#3838).
242 // NB. we check to make sure that the owner is not the same as
243 // the current thread, since in that case it will not be on
245 if (owner->why_blocked == NotBlocked && owner->id != msg->tso->id) {
246 removeFromRunQueue(cap, owner);
247 pushOnRunQueue(cap,owner);
250 // point to the BLOCKING_QUEUE from the BLACKHOLE
251 write_barrier(); // make the BQ visible
252 ((StgInd*)bh)->indirectee = (StgClosure *)bq;
253 recordClosureMutated(cap,bh); // bh was mutated
255 debugTraceCap(DEBUG_sched, cap, "thread %d blocked on thread %d",
256 (lnat)msg->tso->id, (lnat)owner->id);
260 else if (info == &stg_BLOCKING_QUEUE_CLEAN_info ||
261 info == &stg_BLOCKING_QUEUE_DIRTY_info)
263 StgBlockingQueue *bq = (StgBlockingQueue *)p;
265 ASSERT(bq->bh == bh);
267 owner = deRefTSO(bq->owner);
269 ASSERT(owner != END_TSO_QUEUE);
272 if (owner->cap != cap) {
273 sendMessage(cap, owner->cap, (Message*)msg);
274 debugTraceCap(DEBUG_sched, cap, "forwarding message to cap %d", owner->cap->no);
279 msg->link = bq->queue;
281 recordClosureMutated(cap,(StgClosure*)msg);
283 if (info == &stg_BLOCKING_QUEUE_CLEAN_info) {
284 bq->header.info = &stg_BLOCKING_QUEUE_DIRTY_info;
285 recordClosureMutated(cap,(StgClosure*)bq);
288 debugTraceCap(DEBUG_sched, cap, "thread %d blocked on thread %d",
289 (lnat)msg->tso->id, (lnat)owner->id);
292 if (owner->why_blocked == NotBlocked && owner->id != msg->tso->id) {
293 removeFromRunQueue(cap, owner);
294 pushOnRunQueue(cap,owner);
300 return 0; // not blocked