1 /* -----------------------------------------------------------------------------
2 * $Id: Exception.hc,v 1.2 1999/12/02 09:52:41 simonmar Exp $
4 * (c) The GHC Team, 1998-1999
8 * ---------------------------------------------------------------------------*/
11 #include "Exception.h"
18 /* -----------------------------------------------------------------------------
21 A thread can request that asynchronous exceptions not be delivered
22 ("blocked") for the duration of an I/O computation. The primitive
24 blockAsyncExceptions# :: IO a -> IO a
26 is used for this purpose. During a blocked section, asynchronous
27 exceptions may be unblocked again temporarily:
29 unblockAsyncExceptions# :: IO a -> IO a
31 Furthermore, asynchronous exceptions are blocked automatically during
32 the execution of an exception handler. Both of these primitives
33 leave a continuation on the stack which reverts to the previous
34 state (blocked or unblocked) on exit.
36 A thread which wants to raise an exception in another thread (using
37 killThread#) must block until the target thread is ready to receive
38 it. The action of unblocking exceptions in a thread will release all
39 the threads waiting to deliver exceptions to that thread.
41 -------------------------------------------------------------------------- */
43 FN_(blockAsyncExceptionszh_fast)
46 /* Args: R1 :: IO a */
47 STK_CHK_GEN( 2/* worst case */, R1_PTR, blockAsyncExceptionszh_fast, );
49 if (CurrentTSO->blocked_exceptions == NULL) {
50 CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
52 Sp[0] = (W_)&unblockAsyncExceptionszh_ret_info;
56 JMP_(GET_ENTRY(R1.cl));
60 INFO_TABLE_SRT_BITMAP(unblockAsyncExceptionszh_ret_info, unblockAsyncExceptionszh_ret_entry, 0, 0, 0, 0, RET_SMALL, , EF_, 0, 0);
61 FN_(unblockAsyncExceptionszh_ret_entry)
64 ASSERT(CurrentTSO->blocked_exceptions != NULL);
65 awakenBlockedQueue(CurrentTSO->blocked_exceptions);
66 CurrentTSO->blocked_exceptions = NULL;
68 JMP_(ENTRY_CODE(Sp[0]));
72 FN_(unblockAsyncExceptionszh_fast)
75 /* Args: R1 :: IO a */
76 STK_CHK_GEN(2, R1_PTR, unblockAsyncExceptionszh_fast, );
78 if (CurrentTSO->blocked_exceptions != NULL) {
79 awakenBlockedQueue(CurrentTSO->blocked_exceptions);
80 CurrentTSO->blocked_exceptions = NULL;
82 Sp[0] = (W_)&blockAsyncExceptionszh_ret_info;
86 JMP_(GET_ENTRY(R1.cl));
90 INFO_TABLE_SRT_BITMAP(blockAsyncExceptionszh_ret_info, blockAsyncExceptionszh_ret_entry, 0, 0, 0, 0, RET_SMALL, , EF_, 0, 0);
91 FN_(blockAsyncExceptionszh_ret_entry)
94 ASSERT(CurrentTSO->blocked_exceptions == NULL);
95 CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
97 JMP_(ENTRY_CODE(Sp[0]));
102 FN_(killThreadzh_fast)
105 /* args: R1.p = TSO to kill, R2.p = Exception */
107 /* If the target thread is currently blocking async exceptions,
108 * we'll have to block until it's ready to accept them.
110 if (R1.t->blocked_exceptions != NULL) {
112 /* ToDo (SMP): locking if destination thread is currently
115 CurrentTSO->link = R1.t->blocked_exceptions;
116 R1.t->blocked_exceptions = CurrentTSO;
118 CurrentTSO->why_blocked = BlockedOnException;
119 CurrentTSO->block_info.tso = R1.t;
121 BLOCK( R1_PTR | R2_PTR, killThreadzh_fast );
124 /* Killed threads turn into zombies, which might be garbage
125 * collected at a later date. That's why we don't have to
126 * explicitly remove them from any queues they might be on.
129 /* We might have killed ourselves. In which case, better be *very*
130 * careful. If the exception killed us, then return to the scheduler.
131 * If the exception went to a catch frame, we'll just continue from
134 if (R1.t == CurrentTSO) {
135 SaveThreadState(); /* inline! */
136 STGCALL2(raiseAsync, R1.t, R2.cl);
137 if (CurrentTSO->whatNext == ThreadKilled) {
138 R1.w = ThreadYielding;
142 if (CurrentTSO->whatNext == ThreadEnterGHC) {
145 JMP_(GET_ENTRY(R1.cl));
147 barf("killThreadzh_fast");
150 STGCALL2(raiseAsync, R1.t, R2.cl);
153 JMP_(ENTRY_CODE(Sp[0]));
157 /* -----------------------------------------------------------------------------
159 -------------------------------------------------------------------------- */
161 #define CATCH_FRAME_ENTRY_TEMPLATE(label,ret) \
166 Su = ((StgCatchFrame *)Sp)->link; \
167 Sp += sizeofW(StgCatchFrame); \
172 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_entry,ENTRY_CODE(Sp[0]));
173 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_0_entry,RET_VEC(Sp[0],0));
174 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_1_entry,RET_VEC(Sp[0],1));
175 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_2_entry,RET_VEC(Sp[0],2));
176 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_3_entry,RET_VEC(Sp[0],3));
177 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_4_entry,RET_VEC(Sp[0],4));
178 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_5_entry,RET_VEC(Sp[0],5));
179 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_6_entry,RET_VEC(Sp[0],6));
180 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_7_entry,RET_VEC(Sp[0],7));
183 #define CATCH_FRAME_BITMAP 7
185 #define CATCH_FRAME_BITMAP 3
188 /* Catch frames are very similar to update frames, but when entering
189 * one we just pop the frame off the stack and perform the correct
190 * kind of return to the activation record underneath us on the stack.
193 VEC_POLY_INFO_TABLE(catch_frame, CATCH_FRAME_BITMAP, NULL/*srt*/, 0/*srt_off*/, 0/*srt_len*/, CATCH_FRAME,, EF_);
195 /* -----------------------------------------------------------------------------
196 * The catch infotable
198 * This should be exactly the same as would be generated by this STG code
200 * catch = {x,h} \n {} -> catch#{x,h}
202 * It is used in deleteThread when reverting blackholes.
203 * -------------------------------------------------------------------------- */
205 INFO_TABLE(catch_info,catch_entry,2,0,FUN,,EF_,0,0);
209 R2.cl = payloadCPtr(R1.cl,1); /* h */
210 R1.cl = payloadCPtr(R1.cl,0); /* x */
220 /* args: R1 = m, R2 = handler */
221 STK_CHK_GEN(sizeofW(StgCatchFrame), R1_PTR | R2_PTR, catchzh_fast, );
222 Sp -= sizeofW(StgCatchFrame);
223 fp = (StgCatchFrame *)Sp;
224 SET_HDR(fp,(StgInfoTable *)&catch_frame_info,CCCS);
225 fp -> handler = R2.cl;
226 fp -> exceptions_blocked = (CurrentTSO->blocked_exceptions != NULL);
228 Su = (StgUpdateFrame *)fp;
229 TICK_CATCHF_PUSHED();
231 JMP_(GET_ENTRY(R1.cl));
236 /* -----------------------------------------------------------------------------
237 * The raise infotable
239 * This should be exactly the same as would be generated by this STG code
241 * raise = {err} \n {} -> raise#{err}
243 * It is used in raisezh_fast to update thunks on the update list
244 * -------------------------------------------------------------------------- */
246 INFO_TABLE(raise_info,raise_entry,1,0,FUN,,EF_,0,0);
250 R1.cl = R1.cl->payload[0];
259 StgClosure *raise_closure;
261 /* args : R1 = error */
264 #if defined(PROFILING)
266 /* Debugging tool: on raising an exception, show where we are. */
268 /* ToDo: currently this is a hack. Would be much better if
269 * the info was only displayed for an *uncaught* exception.
271 if (RtsFlags.ProfFlags.showCCSOnException) {
272 STGCALL2(print_ccs,stderr,CCCS);
279 /* This closure represents the expression 'raise# E' where E
280 * is the exception raise. It is used to overwrite all the
281 * thunks which are currently under evaluataion.
283 raise_closure = (StgClosure *)RET_STGCALL1(P_,allocate,
284 sizeofW(StgClosure)+1);
285 raise_closure->header.info = &raise_info;
286 raise_closure->payload[0] = R1.cl;
290 switch (get_itbl(p)->type) {
293 UPD_IND(p->updatee,raise_closure);
298 p = ((StgSeqFrame *)p)->link;
306 barf("raisezh_fast: STOP_FRAME");
309 barf("raisezh_fast: weird activation record");
316 /* Ok, p points to the enclosing CATCH_FRAME. Pop everything down to
317 * and including this frame, update Su, push R1, and enter the handler.
319 Su = ((StgCatchFrame *)p)->link;
320 handler = ((StgCatchFrame *)p)->handler;
322 Sp = (P_)p + sizeofW(StgCatchFrame) - 1;
324 /* Restore the blocked/unblocked state for asynchronous exceptions
325 * at the CATCH_FRAME.
327 * If exceptions were unblocked, arrange that they are unblocked
328 * again after executing the handler by pushing an
329 * unblockAsyncExceptions_ret stack frame.
331 if (! ((StgCatchFrame *)p)->exceptions_blocked) {
332 *(Sp--) = (W_)&unblockAsyncExceptionszh_ret_info;
335 /* Ensure that async excpetions are blocked when running the handler.
337 if (CurrentTSO->blocked_exceptions == NULL) {
338 CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
341 /* Enter the handler, passing the exception value as an argument.
346 JMP_(GET_ENTRY(R1.cl));