1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2006
5 * Asynchronous exceptions
7 * --------------------------------------------------------------------------*/
9 #include "PosixSource.h"
12 #include "sm/Storage.h"
15 #include "RaiseAsync.h"
19 #include "sm/Sanity.h"
20 #include "Profiling.h"
22 #if defined(mingw32_HOST_OS)
23 #include "win32/IOManager.h"
26 static void raiseAsync (Capability *cap,
28 StgClosure *exception,
29 rtsBool stop_at_atomically,
30 StgUpdateFrame *stop_here);
32 static void removeFromQueues(Capability *cap, StgTSO *tso);
34 static void blockedThrowTo (Capability *cap,
35 StgTSO *target, MessageThrowTo *msg);
37 static void throwToSendMsg (Capability *cap USED_IF_THREADS,
38 Capability *target_cap USED_IF_THREADS,
39 MessageThrowTo *msg USED_IF_THREADS);
41 static void performBlockedException (Capability *cap, MessageThrowTo *msg);
43 /* -----------------------------------------------------------------------------
46 This version of throwTo is safe to use if and only if one of the
51 - all the other threads in the system are stopped (eg. during GC).
53 - we surely own the target TSO (eg. we just took it from the
54 run queue of the current capability, or we are running it).
56 It doesn't cater for blocking the source thread until the exception
58 -------------------------------------------------------------------------- */
61 throwToSingleThreaded(Capability *cap, StgTSO *tso, StgClosure *exception)
63 throwToSingleThreaded_(cap, tso, exception, rtsFalse);
67 throwToSingleThreaded_(Capability *cap, StgTSO *tso, StgClosure *exception,
68 rtsBool stop_at_atomically)
72 // Thread already dead?
73 if (tso->what_next == ThreadComplete || tso->what_next == ThreadKilled) {
77 // Remove it from any blocking queues
78 removeFromQueues(cap,tso);
80 raiseAsync(cap, tso, exception, stop_at_atomically, NULL);
84 suspendComputation(Capability *cap, StgTSO *tso, StgUpdateFrame *stop_here)
88 // Thread already dead?
89 if (tso->what_next == ThreadComplete || tso->what_next == ThreadKilled) {
93 // Remove it from any blocking queues
94 removeFromQueues(cap,tso);
96 raiseAsync(cap, tso, NULL, rtsFalse, stop_here);
99 /* -----------------------------------------------------------------------------
102 This function may be used to throw an exception from one thread to
103 another, during the course of normal execution. This is a tricky
104 task: the target thread might be running on another CPU, or it
105 may be blocked and could be woken up at any point by another CPU.
106 We have some delicate synchronisation to do.
108 The underlying scheme when multiple Capabilities are in use is
109 message passing: when the target of a throwTo is on another
110 Capability, we send a message (a MessageThrowTo closure) to that
113 If the throwTo needs to block because the target TSO is masking
114 exceptions (the TSO_BLOCKEX flag), then the message is placed on
115 the blocked_exceptions queue attached to the target TSO. When the
116 target TSO enters the unmasked state again, it must check the
117 queue. The blocked_exceptions queue is not locked; only the
118 Capability owning the TSO may modify it.
120 To make things simpler for throwTo, we always create the message
121 first before deciding what to do. The message may get sent, or it
122 may get attached to a TSO's blocked_exceptions queue, or the
123 exception may get thrown immediately and the message dropped,
124 depending on the current state of the target.
126 Currently we send a message if the target belongs to another
127 Capability, and it is
129 - NotBlocked, BlockedOnMsgWakeup, BlockedOnMsgThrowTo,
132 - or it is masking exceptions (TSO_BLOCKEX)
134 Currently, if the target is BlockedOnMVar, BlockedOnSTM, or
135 BlockedOnBlackHole then we acquire ownership of the TSO by locking
136 its parent container (e.g. the MVar) and then raise the exception.
137 We might change these cases to be more message-passing-like in the
142 NULL exception was raised, ok to continue
144 MessageThrowTo * exception was not raised; the source TSO
145 should now put itself in the state
146 BlockedOnMsgThrowTo, and when it is ready
147 it should unlock the mssage using
148 unlockClosure(msg, &stg_MSG_THROWTO_info);
149 If it decides not to raise the exception after
150 all, it can revoke it safely with
151 unlockClosure(msg, &stg_IND_info);
153 -------------------------------------------------------------------------- */
156 throwTo (Capability *cap, // the Capability we hold
157 StgTSO *source, // the TSO sending the exception (or NULL)
158 StgTSO *target, // the TSO receiving the exception
159 StgClosure *exception) // the exception closure
163 msg = (MessageThrowTo *) allocate(cap, sizeofW(MessageThrowTo));
164 // message starts locked; the caller has to unlock it when it is
166 SET_HDR(msg, &stg_WHITEHOLE_info, CCS_SYSTEM);
167 msg->source = source;
168 msg->target = target;
169 msg->exception = exception;
171 switch (throwToMsg(cap, msg))
173 case THROWTO_SUCCESS:
175 case THROWTO_BLOCKED:
183 throwToMsg (Capability *cap, MessageThrowTo *msg)
186 StgTSO *target = msg->target;
187 Capability *target_cap;
193 debugTrace(DEBUG_sched, "throwTo: retrying...");
196 ASSERT(target != END_TSO_QUEUE);
198 // follow ThreadRelocated links in the target first
199 target = deRefTSO(target);
201 // Thread already dead?
202 if (target->what_next == ThreadComplete
203 || target->what_next == ThreadKilled) {
204 return THROWTO_SUCCESS;
207 debugTraceCap(DEBUG_sched, cap,
208 "throwTo: from thread %lu to thread %lu",
209 (unsigned long)msg->source->id,
210 (unsigned long)msg->target->id);
213 traceThreadStatus(DEBUG_sched, target);
216 target_cap = target->cap;
217 if (target->cap != cap) {
218 throwToSendMsg(cap, target_cap, msg);
219 return THROWTO_BLOCKED;
222 status = target->why_blocked;
226 case BlockedOnMsgWakeup:
227 /* if status==NotBlocked, and target->cap == cap, then
228 we own this TSO and can raise the exception.
230 How do we establish this condition? Very carefully.
233 P = (status == NotBlocked)
234 Q = (tso->cap == cap)
236 Now, if P & Q are true, then the TSO is locked and owned by
237 this capability. No other OS thread can steal it.
239 If P==0 and Q==1: the TSO is blocked, but attached to this
240 capabilty, and it can be stolen by another capability.
242 If P==1 and Q==0: the TSO is runnable on another
243 capability. At any time, the TSO may change from runnable
244 to blocked and vice versa, while it remains owned by
247 Suppose we test like this:
253 this is defeated by another capability stealing a blocked
254 TSO from us to wake it up (Schedule.c:unblockOne()). The
255 other thread is doing
260 assuming arbitrary reordering, we could see this
270 so we need a memory barrier:
277 this avoids the problematic case. There are other cases
278 to consider, but this is the tricky one.
280 Note that we must be sure that unblockOne() does the
281 writes in the correct order: Q before P. The memory
282 barrier ensures that if we have seen the write to P, we
283 have also seen the write to Q.
287 if ((target->flags & TSO_BLOCKEX) == 0) {
288 // It's on our run queue and not blocking exceptions
289 raiseAsync(cap, target, msg->exception, rtsFalse, NULL);
290 return THROWTO_SUCCESS;
292 blockedThrowTo(cap,target,msg);
293 return THROWTO_BLOCKED;
297 case BlockedOnMsgThrowTo:
299 const StgInfoTable *i;
302 m = target->block_info.throwto;
304 // target is local to this cap, but has sent a throwto
305 // message to another cap.
307 // The source message is locked. We need to revoke the
308 // target's message so that we can raise the exception, so
309 // we attempt to lock it.
311 // There's a possibility of a deadlock if two threads are both
312 // trying to throwTo each other (or more generally, a cycle of
313 // threads). To break the symmetry we compare the addresses
314 // of the MessageThrowTo objects, and the one for which m <
315 // msg gets to spin, while the other can only try to lock
316 // once, but must then back off and unlock both before trying
319 i = lockClosure((StgClosure *)m);
321 i = tryLockClosure((StgClosure *)m);
323 // debugBelch("collision\n");
324 throwToSendMsg(cap, target->cap, msg);
325 return THROWTO_BLOCKED;
329 if (i != &stg_MSG_THROWTO_info) {
330 // if it's an IND, this TSO has been woken up by another Cap
331 unlockClosure((StgClosure*)m, i);
335 if ((target->flags & TSO_BLOCKEX) &&
336 ((target->flags & TSO_INTERRUPTIBLE) == 0)) {
337 unlockClosure((StgClosure*)m, i);
338 blockedThrowTo(cap,target,msg);
339 return THROWTO_BLOCKED;
342 // nobody else can wake up this TSO after we claim the message
343 unlockClosure((StgClosure*)m, &stg_IND_info);
345 raiseAsync(cap, target, msg->exception, rtsFalse, NULL);
346 return THROWTO_SUCCESS;
352 To establish ownership of this TSO, we need to acquire a
353 lock on the MVar that it is blocked on.
356 StgInfoTable *info USED_IF_THREADS;
358 mvar = (StgMVar *)target->block_info.closure;
360 // ASSUMPTION: tso->block_info must always point to a
361 // closure. In the threaded RTS it does.
362 switch (get_itbl(mvar)->type) {
370 info = lockClosure((StgClosure *)mvar);
372 if (target->what_next == ThreadRelocated) {
373 target = target->_link;
374 unlockClosure((StgClosure *)mvar,info);
377 // we have the MVar, let's check whether the thread
378 // is still blocked on the same MVar.
379 if (target->why_blocked != BlockedOnMVar
380 || (StgMVar *)target->block_info.closure != mvar) {
381 unlockClosure((StgClosure *)mvar, info);
385 if ((target->flags & TSO_BLOCKEX) &&
386 ((target->flags & TSO_INTERRUPTIBLE) == 0)) {
387 blockedThrowTo(cap,target,msg);
388 unlockClosure((StgClosure *)mvar, info);
389 return THROWTO_BLOCKED;
391 removeThreadFromMVarQueue(cap, mvar, target);
392 raiseAsync(cap, target, msg->exception, rtsFalse, NULL);
393 if (info == &stg_MVAR_CLEAN_info) {
394 dirty_MVAR(&cap->r,(StgClosure*)mvar);
396 unlockClosure((StgClosure *)mvar, &stg_MVAR_DIRTY_info);
397 return THROWTO_SUCCESS;
401 case BlockedOnBlackHole:
403 // Revoke the message by replacing it with IND. We're not
404 // locking anything here, so we might still get a TRY_WAKEUP
405 // message from the owner of the blackhole some time in the
406 // future, but that doesn't matter.
407 ASSERT(target->block_info.bh->header.info == &stg_MSG_BLACKHOLE_info);
408 OVERWRITE_INFO(target->block_info.bh, &stg_IND_info);
409 raiseAsync(cap, target, msg->exception, rtsFalse, NULL);
410 return THROWTO_SUCCESS;
415 // Unblocking BlockedOnSTM threads requires the TSO to be
416 // locked; see STM.c:unpark_tso().
417 if (target->why_blocked != BlockedOnSTM) {
421 if ((target->flags & TSO_BLOCKEX) &&
422 ((target->flags & TSO_INTERRUPTIBLE) == 0)) {
423 blockedThrowTo(cap,target,msg);
425 return THROWTO_BLOCKED;
427 raiseAsync(cap, target, msg->exception, rtsFalse, NULL);
429 return THROWTO_SUCCESS;
433 case BlockedOnCCall_NoUnblockExc:
434 blockedThrowTo(cap,target,msg);
435 return THROWTO_BLOCKED;
437 #ifndef THREADEDED_RTS
441 #if defined(mingw32_HOST_OS)
442 case BlockedOnDoProc:
444 if ((target->flags & TSO_BLOCKEX) &&
445 ((target->flags & TSO_INTERRUPTIBLE) == 0)) {
446 blockedThrowTo(cap,target,msg);
447 return THROWTO_BLOCKED;
449 removeFromQueues(cap,target);
450 raiseAsync(cap, target, msg->exception, rtsFalse, NULL);
451 return THROWTO_SUCCESS;
456 barf("throwTo: unrecognised why_blocked value");
462 throwToSendMsg (Capability *cap STG_UNUSED,
463 Capability *target_cap USED_IF_THREADS,
464 MessageThrowTo *msg USED_IF_THREADS)
468 debugTraceCap(DEBUG_sched, cap, "throwTo: sending a throwto message to cap %lu", (unsigned long)target_cap->no);
470 sendMessage(cap, target_cap, (Message*)msg);
474 // Block a throwTo message on the target TSO's blocked_exceptions
475 // queue. The current Capability must own the target TSO in order to
476 // modify the blocked_exceptions queue.
478 blockedThrowTo (Capability *cap, StgTSO *target, MessageThrowTo *msg)
480 debugTraceCap(DEBUG_sched, cap, "throwTo: blocking on thread %lu",
481 (unsigned long)target->id);
483 ASSERT(target->cap == cap);
485 msg->link = target->blocked_exceptions;
486 target->blocked_exceptions = msg;
487 dirty_TSO(cap,target); // we modified the blocked_exceptions queue
490 /* -----------------------------------------------------------------------------
491 Waking up threads blocked in throwTo
493 There are two ways to do this: maybePerformBlockedException() will
494 perform the throwTo() for the thread at the head of the queue
495 immediately, and leave the other threads on the queue.
496 maybePerformBlockedException() also checks the TSO_BLOCKEX flag
497 before raising an exception.
499 awakenBlockedExceptionQueue() will wake up all the threads in the
500 queue, but not perform any throwTo() immediately. This might be
501 more appropriate when the target thread is the one actually running
504 Returns: non-zero if an exception was raised, zero otherwise.
505 -------------------------------------------------------------------------- */
508 maybePerformBlockedException (Capability *cap, StgTSO *tso)
511 const StgInfoTable *i;
513 if (tso->what_next == ThreadComplete || tso->what_next == ThreadFinished) {
514 if (tso->blocked_exceptions != END_BLOCKED_EXCEPTIONS_QUEUE) {
515 awakenBlockedExceptionQueue(cap,tso);
522 if (tso->blocked_exceptions != END_BLOCKED_EXCEPTIONS_QUEUE &&
523 (tso->flags & TSO_BLOCKEX) != 0) {
524 debugTraceCap(DEBUG_sched, cap, "throwTo: thread %lu has blocked exceptions but is inside block", (unsigned long)tso->id);
527 if (tso->blocked_exceptions != END_BLOCKED_EXCEPTIONS_QUEUE
528 && ((tso->flags & TSO_BLOCKEX) == 0
529 || ((tso->flags & TSO_INTERRUPTIBLE) && interruptible(tso)))) {
531 // We unblock just the first thread on the queue, and perform
532 // its throw immediately.
534 msg = tso->blocked_exceptions;
535 if (msg == END_BLOCKED_EXCEPTIONS_QUEUE) return 0;
536 i = lockClosure((StgClosure*)msg);
537 tso->blocked_exceptions = (MessageThrowTo*)msg->link;
538 if (i == &stg_IND_info) {
539 unlockClosure((StgClosure*)msg,i);
543 performBlockedException(cap, msg);
544 unblockOne_(cap, msg->source, rtsFalse/*no migrate*/);
545 unlockClosure((StgClosure*)msg,&stg_IND_info);
551 // awakenBlockedExceptionQueue(): Just wake up the whole queue of
552 // blocked exceptions and let them try again.
555 awakenBlockedExceptionQueue (Capability *cap, StgTSO *tso)
558 const StgInfoTable *i;
560 for (msg = tso->blocked_exceptions; msg != END_BLOCKED_EXCEPTIONS_QUEUE;
561 msg = (MessageThrowTo*)msg->link) {
562 i = lockClosure((StgClosure *)msg);
563 if (i != &stg_IND_info) {
564 unblockOne_(cap, msg->source, rtsFalse/*no migrate*/);
566 unlockClosure((StgClosure *)msg,i);
568 tso->blocked_exceptions = END_BLOCKED_EXCEPTIONS_QUEUE;
572 performBlockedException (Capability *cap, MessageThrowTo *msg)
576 source = msg->source;
578 ASSERT(source->why_blocked == BlockedOnMsgThrowTo);
579 ASSERT(source->block_info.closure == (StgClosure *)msg);
580 ASSERT(source->sp[0] == (StgWord)&stg_block_throwto_info);
581 ASSERT(((StgTSO *)source->sp[1])->id == msg->target->id);
582 // check ids not pointers, because the thread might be relocated
584 throwToSingleThreaded(cap, msg->target, msg->exception);
588 /* -----------------------------------------------------------------------------
589 Remove a thread from blocking queues.
591 This is for use when we raise an exception in another thread, which
594 Precondition: we have exclusive access to the TSO, via the same set
595 of conditions as throwToSingleThreaded() (c.f.).
596 -------------------------------------------------------------------------- */
599 removeFromQueues(Capability *cap, StgTSO *tso)
601 switch (tso->why_blocked) {
607 // Be careful: nothing to do here! We tell the scheduler that the
608 // thread is runnable and we leave it to the stack-walking code to
609 // abort the transaction while unwinding the stack. We should
610 // perhaps have a debugging test to make sure that this really
611 // happens and that the 'zombie' transaction does not get
616 removeThreadFromMVarQueue(cap, (StgMVar *)tso->block_info.closure, tso);
617 // we aren't doing a write barrier here: the MVar is supposed to
618 // be already locked, so replacing the info pointer would unlock it.
621 case BlockedOnBlackHole:
625 case BlockedOnMsgWakeup:
627 // kill the message, atomically:
628 OVERWRITE_INFO(tso->block_info.wakeup, &stg_IND_info);
632 case BlockedOnMsgThrowTo:
634 MessageThrowTo *m = tso->block_info.throwto;
635 // The message is locked by us, unless we got here via
636 // deleteAllThreads(), in which case we own all the
638 // ASSERT(m->header.info == &stg_WHITEHOLE_info);
640 // unlock and revoke it at the same time
641 unlockClosure((StgClosure*)m,&stg_IND_info);
645 #if !defined(THREADED_RTS)
648 #if defined(mingw32_HOST_OS)
649 case BlockedOnDoProc:
651 removeThreadFromDeQueue(cap, &blocked_queue_hd, &blocked_queue_tl, tso);
652 #if defined(mingw32_HOST_OS)
653 /* (Cooperatively) signal that the worker thread should abort
656 abandonWorkRequest(tso->block_info.async_result->reqID);
661 removeThreadFromQueue(cap, &sleeping_queue, tso);
666 barf("removeFromQueues: %d", tso->why_blocked);
670 unblockOne(cap, tso);
673 /* -----------------------------------------------------------------------------
676 * The following function implements the magic for raising an
677 * asynchronous exception in an existing thread.
679 * We first remove the thread from any queue on which it might be
680 * blocked. The possible blockages are MVARs, BLOCKING_QUEUESs, and
681 * TSO blocked_exception queues.
683 * We strip the stack down to the innermost CATCH_FRAME, building
684 * thunks in the heap for all the active computations, so they can
685 * be restarted if necessary. When we reach a CATCH_FRAME, we build
686 * an application of the handler to the exception, and push it on
687 * the top of the stack.
689 * How exactly do we save all the active computations? We create an
690 * AP_STACK for every UpdateFrame on the stack. Entering one of these
691 * AP_STACKs pushes everything from the corresponding update frame
692 * upwards onto the stack. (Actually, it pushes everything up to the
693 * next update frame plus a pointer to the next AP_STACK object.
694 * Entering the next AP_STACK object pushes more onto the stack until we
695 * reach the last AP_STACK object - at which point the stack should look
696 * exactly as it did when we killed the TSO and we can continue
697 * execution by entering the closure on top of the stack.
699 * We can also kill a thread entirely - this happens if either (a) the
700 * exception passed to raiseAsync is NULL, or (b) there's no
701 * CATCH_FRAME on the stack. In either case, we strip the entire
702 * stack and replace the thread with a zombie.
704 * ToDo: in THREADED_RTS mode, this function is only safe if either
705 * (a) we hold all the Capabilities (eg. in GC, or if there is only
706 * one Capability), or (b) we own the Capability that the TSO is
707 * currently blocked on or on the run queue of.
709 * -------------------------------------------------------------------------- */
712 raiseAsync(Capability *cap, StgTSO *tso, StgClosure *exception,
713 rtsBool stop_at_atomically, StgUpdateFrame *stop_here)
715 StgRetInfoTable *info;
720 debugTraceCap(DEBUG_sched, cap,
721 "raising exception in thread %ld.", (long)tso->id);
723 #if defined(PROFILING)
725 * Debugging tool: on raising an exception, show where we are.
726 * See also Exception.cmm:stg_raisezh.
727 * This wasn't done for asynchronous exceptions originally; see #1450
729 if (RtsFlags.ProfFlags.showCCSOnException)
731 fprintCCS_stderr(tso->prof.CCCS);
734 // ASSUMES: the thread is not already complete or dead, or
735 // ThreadRelocated. Upper layers should deal with that.
736 ASSERT(tso->what_next != ThreadComplete &&
737 tso->what_next != ThreadKilled &&
738 tso->what_next != ThreadRelocated);
740 // only if we own this TSO (except that deleteThread() calls this
741 ASSERT(tso->cap == cap);
744 if (tso->why_blocked != NotBlocked && tso->why_blocked != BlockedOnMsgWakeup) {
745 tso->why_blocked = NotBlocked;
746 appendToRunQueue(cap,tso);
749 // mark it dirty; we're about to change its stack.
754 if (stop_here != NULL) {
755 updatee = stop_here->updatee;
760 // The stack freezing code assumes there's a closure pointer on
761 // the top of the stack, so we have to arrange that this is the case...
763 if (sp[0] == (W_)&stg_enter_info) {
767 sp[0] = (W_)&stg_dummy_ret_closure;
771 while (stop_here == NULL || frame < (StgPtr)stop_here) {
773 // 1. Let the top of the stack be the "current closure"
775 // 2. Walk up the stack until we find either an UPDATE_FRAME or a
778 // 3. If it's an UPDATE_FRAME, then make an AP_STACK containing the
779 // current closure applied to the chunk of stack up to (but not
780 // including) the update frame. This closure becomes the "current
781 // closure". Go back to step 2.
783 // 4. If it's a CATCH_FRAME, then leave the exception handler on
784 // top of the stack applied to the exception.
786 // 5. If it's a STOP_FRAME, then kill the thread.
788 // NB: if we pass an ATOMICALLY_FRAME then abort the associated
791 info = get_ret_itbl((StgClosure *)frame);
793 switch (info->i.type) {
800 // First build an AP_STACK consisting of the stack chunk above the
801 // current update frame, with the top word on the stack as the
804 words = frame - sp - 1;
805 ap = (StgAP_STACK *)allocate(cap,AP_STACK_sizeW(words));
808 ap->fun = (StgClosure *)sp[0];
810 for(i=0; i < (nat)words; ++i) {
811 ap->payload[i] = (StgClosure *)*sp++;
814 SET_HDR(ap,&stg_AP_STACK_info,
815 ((StgClosure *)frame)->header.prof.ccs /* ToDo */);
816 TICK_ALLOC_UP_THK(words+1,0);
818 //IF_DEBUG(scheduler,
819 // debugBelch("sched: Updating ");
820 // printPtr((P_)((StgUpdateFrame *)frame)->updatee);
821 // debugBelch(" with ");
822 // printObj((StgClosure *)ap);
825 if (((StgUpdateFrame *)frame)->updatee == updatee) {
826 // If this update frame points to the same closure as
827 // the update frame further down the stack
828 // (stop_here), then don't perform the update. We
829 // want to keep the blackhole in this case, so we can
830 // detect and report the loop (#2783).
831 ap = (StgAP_STACK*)updatee;
833 // Perform the update
834 // TODO: this may waste some work, if the thunk has
835 // already been updated by another thread.
836 updateThunk(cap, tso,
837 ((StgUpdateFrame *)frame)->updatee, (StgClosure *)ap);
840 sp += sizeofW(StgUpdateFrame) - 1;
841 sp[0] = (W_)ap; // push onto stack
843 continue; //no need to bump frame
848 // We've stripped the entire stack, the thread is now dead.
849 tso->what_next = ThreadKilled;
850 tso->sp = frame + sizeofW(StgStopFrame);
855 // If we find a CATCH_FRAME, and we've got an exception to raise,
856 // then build the THUNK raise(exception), and leave it on
857 // top of the CATCH_FRAME ready to enter.
861 StgCatchFrame *cf = (StgCatchFrame *)frame;
865 if (exception == NULL) break;
867 // we've got an exception to raise, so let's pass it to the
868 // handler in this frame.
870 raise = (StgThunk *)allocate(cap,sizeofW(StgThunk)+1);
871 TICK_ALLOC_SE_THK(1,0);
872 SET_HDR(raise,&stg_raise_info,cf->header.prof.ccs);
873 raise->payload[0] = exception;
875 // throw away the stack from Sp up to the CATCH_FRAME.
879 /* Ensure that async excpetions are blocked now, so we don't get
880 * a surprise exception before we get around to executing the
883 tso->flags |= TSO_BLOCKEX | TSO_INTERRUPTIBLE;
885 /* Put the newly-built THUNK on top of the stack, ready to execute
886 * when the thread restarts.
889 sp[-1] = (W_)&stg_enter_info;
891 tso->what_next = ThreadRunGHC;
892 IF_DEBUG(sanity, checkTSO(tso));
896 case ATOMICALLY_FRAME:
897 if (stop_at_atomically) {
898 ASSERT(tso->trec->enclosing_trec == NO_TREC);
899 stmCondemnTransaction(cap, tso -> trec);
901 // The ATOMICALLY_FRAME expects to be returned a
902 // result from the transaction, which it stores in the
903 // stack frame. Hence we arrange to return a dummy
904 // result, so that the GC doesn't get upset (#3578).
905 // Perhaps a better way would be to have a different
906 // ATOMICALLY_FRAME instance for condemned
907 // transactions, but I don't fully understand the
908 // interaction with STM invariants.
909 tso->sp[1] = (W_)&stg_NO_TREC_closure;
910 tso->sp[0] = (W_)&stg_gc_unpt_r1_info;
911 tso->what_next = ThreadRunGHC;
914 // Not stop_at_atomically... fall through and abort the
917 case CATCH_STM_FRAME:
918 case CATCH_RETRY_FRAME:
919 // IF we find an ATOMICALLY_FRAME then we abort the
920 // current transaction and propagate the exception. In
921 // this case (unlike ordinary exceptions) we do not care
922 // whether the transaction is valid or not because its
923 // possible validity cannot have caused the exception
924 // and will not be visible after the abort.
927 StgTRecHeader *trec = tso -> trec;
928 StgTRecHeader *outer = trec -> enclosing_trec;
929 debugTraceCap(DEBUG_stm, cap,
930 "found atomically block delivering async exception");
931 stmAbortTransaction(cap, trec);
932 stmFreeAbortedTRec(cap, trec);
941 // move on to the next stack frame
942 frame += stack_frame_sizeW((StgClosure *)frame);
945 // if we got here, then we stopped at stop_here
946 ASSERT(stop_here != NULL);