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_WAKEUP_info &&
32 i != &stg_MSG_THROWTO_info &&
33 i != &stg_MSG_BLACKHOLE_info &&
34 i != &stg_MSG_TRY_WAKEUP_info &&
35 i != &stg_IND_info && // can happen if a MSG_BLACKHOLE is revoked
36 i != &stg_WHITEHOLE_info) {
37 barf("sendMessage: %p", i);
42 msg->link = to_cap->inbox;
45 recordClosureMutated(from_cap,(StgClosure*)msg);
47 if (to_cap->running_task == NULL) {
48 to_cap->running_task = myTask();
49 // precond for releaseCapability_()
50 releaseCapability_(to_cap,rtsFalse);
52 contextSwitchCapability(to_cap);
55 RELEASE_LOCK(&to_cap->lock);
58 #endif /* THREADED_RTS */
60 /* ----------------------------------------------------------------------------
62 ------------------------------------------------------------------------- */
67 executeMessage (Capability *cap, Message *m)
69 const StgInfoTable *i;
72 write_barrier(); // allow m->header to be modified by another thread
74 if (i == &stg_MSG_WAKEUP_info)
76 // the plan is to eventually get rid of these and use
77 // TRY_WAKEUP instead.
78 MessageWakeup *w = (MessageWakeup *)m;
80 debugTraceCap(DEBUG_sched, cap, "message: wakeup thread %ld",
82 ASSERT(tso->cap == cap);
83 ASSERT(tso->why_blocked == BlockedOnMsgWakeup);
84 ASSERT(tso->block_info.closure == (StgClosure *)m);
85 tso->why_blocked = NotBlocked;
86 appendToRunQueue(cap, tso);
88 else if (i == &stg_MSG_TRY_WAKEUP_info)
90 StgTSO *tso = ((MessageWakeup *)m)->tso;
91 debugTraceCap(DEBUG_sched, cap, "message: try wakeup thread %ld",
93 tryWakeupThread(cap, tso);
95 else if (i == &stg_MSG_THROWTO_info)
97 MessageThrowTo *t = (MessageThrowTo *)m;
99 const StgInfoTable *i;
101 i = lockClosure((StgClosure*)m);
102 if (i != &stg_MSG_THROWTO_info) {
103 unlockClosure((StgClosure*)m, i);
107 debugTraceCap(DEBUG_sched, cap, "message: throwTo %ld -> %ld",
108 (lnat)t->source->id, (lnat)t->target->id);
110 ASSERT(t->source->why_blocked == BlockedOnMsgThrowTo);
111 ASSERT(t->source->block_info.closure == (StgClosure *)m);
113 r = throwToMsg(cap, t);
116 case THROWTO_SUCCESS:
117 ASSERT(t->source->sp[0] == (StgWord)&stg_block_throwto_info);
119 unblockOne(cap, t->source);
120 // this message is done
121 unlockClosure((StgClosure*)m, &stg_IND_info);
123 case THROWTO_BLOCKED:
124 // unlock the message
125 unlockClosure((StgClosure*)m, &stg_MSG_THROWTO_info);
129 else if (i == &stg_MSG_BLACKHOLE_info)
132 MessageBlackHole *b = (MessageBlackHole*)m;
134 r = messageBlackHole(cap, b);
136 tryWakeupThread(cap, b->tso);
140 else if (i == &stg_IND_info)
142 // message was revoked
145 else if (i == &stg_WHITEHOLE_info)
151 barf("executeMessage: %p", i);
157 /* ----------------------------------------------------------------------------
158 Handle a MSG_BLACKHOLE message
160 This is called from two places: either we just entered a BLACKHOLE
161 (stg_BLACKHOLE_info), or we received a MSG_BLACKHOLE in our
164 We need to establish whether the BLACKHOLE belongs to
166 - if so, arrange to block the current thread on it
167 - otherwise, forward the message to the right place
170 - 0 if the blocked thread can be woken up by the caller
171 - 1 if the thread is still blocked, and we promise to send a MSG_TRY_WAKEUP
172 at some point in the future.
174 ------------------------------------------------------------------------- */
176 nat messageBlackHole(Capability *cap, MessageBlackHole *msg)
178 const StgInfoTable *info;
180 StgBlockingQueue *bq;
181 StgClosure *bh = msg->bh;
184 debugTraceCap(DEBUG_sched, cap, "message: thread %d blocking on blackhole %p",
185 (lnat)msg->tso->id, msg->bh);
187 info = bh->header.info;
189 // If we got this message in our inbox, it might be that the
190 // BLACKHOLE has already been updated, and GC has shorted out the
191 // indirection, so the pointer no longer points to a BLACKHOLE at
193 if (info != &stg_BLACKHOLE_info &&
194 info != &stg_CAF_BLACKHOLE_info &&
195 info != &stg_WHITEHOLE_info) {
196 // if it is a WHITEHOLE, then a thread is in the process of
197 // trying to BLACKHOLE it. But we know that it was once a
198 // BLACKHOLE, so there is at least a valid pointer in the
199 // payload, so we can carry on.
203 // we know at this point that the closure
205 p = ((StgInd*)bh)->indirectee;
206 info = p->header.info;
208 if (info == &stg_IND_info)
210 // This could happen, if e.g. we got a BLOCKING_QUEUE that has
211 // just been replaced with an IND by another thread in
212 // updateThunk(). In which case, if we read the indirectee
213 // again we should get the value.
217 else if (info == &stg_TSO_info)
219 owner = deRefTSO((StgTSO *)p);
222 if (owner->cap != cap) {
223 sendMessage(cap, owner->cap, (Message*)msg);
224 debugTraceCap(DEBUG_sched, cap, "forwarding message to cap %d", owner->cap->no);
228 // owner is the owner of the BLACKHOLE, and resides on this
229 // Capability. msg->tso is the first thread to block on this
230 // BLACKHOLE, so we first create a BLOCKING_QUEUE object.
232 bq = (StgBlockingQueue*)allocate(cap, sizeofW(StgBlockingQueue));
234 // initialise the BLOCKING_QUEUE object
235 SET_HDR(bq, &stg_BLOCKING_QUEUE_DIRTY_info, CCS_SYSTEM);
240 msg->link = (MessageBlackHole*)END_TSO_QUEUE;
242 // All BLOCKING_QUEUES are linked in a list on owner->bq, so
243 // that we can search through them in the event that there is
244 // a collision to update a BLACKHOLE and a BLOCKING_QUEUE
245 // becomes orphaned (see updateThunk()).
246 bq->link = owner->bq;
248 dirty_TSO(cap, owner); // we modified owner->bq
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,bq);
288 debugTraceCap(DEBUG_sched, cap, "thread %d blocked on thread %d",
289 (lnat)msg->tso->id, (lnat)owner->id);
294 return 0; // not blocked