1 /* -----------------------------------------------------------------------------
2 * $Id: Exception.hc,v 1.1 1999/12/01 14:34:38 simonmar Exp $
4 * (c) The GHC Team, 1998-1999
8 * ---------------------------------------------------------------------------*/
11 #include "Exception.h"
17 /* -----------------------------------------------------------------------------
20 A thread can request that asynchronous exceptions not be delivered
21 ("blocked") for the duration of an I/O computation. The primitive
23 blockAsyncExceptions# :: IO a -> IO a
25 is used for this purpose. During a blocked section, asynchronous
26 exceptions may be unblocked again temporarily:
28 unblockAsyncExceptions# :: IO a -> IO a
30 Furthermore, asynchronous exceptions are blocked automatically during
31 the execution of an exception handler. Both of these primitives
32 leave a continuation on the stack which reverts to the previous
33 state (blocked or unblocked) on exit.
35 A thread which wants to raise an exception in another thread (using
36 killThread#) must block until the target thread is ready to receive
37 it. The action of unblocking exceptions in a thread will release all
38 the threads waiting to deliver exceptions to that thread.
40 -------------------------------------------------------------------------- */
42 FN_(blockAsyncExceptionszh_fast)
45 /* Args: R1 :: IO a */
46 STK_CHK_GEN( 2/* worst case */, R1_PTR, blockAsyncExceptionszh_fast, );
48 if (CurrentTSO->blocked_exceptions == NULL) {
49 CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
51 Sp[0] = (W_)&unblockAsyncExceptionszh_ret_info;
55 JMP_(GET_ENTRY(R1.cl));
59 INFO_TABLE_SRT_BITMAP(unblockAsyncExceptionszh_ret_info, unblockAsyncExceptionszh_ret_entry, 0, 0, 0, 0, RET_SMALL, , EF_, 0, 0);
60 FN_(unblockAsyncExceptionszh_ret_entry)
63 ASSERT(CurrentTSO->blocked_exceptions != NULL);
64 awakenBlockedQueue(CurrentTSO->blocked_exceptions);
65 CurrentTSO->blocked_exceptions = NULL;
67 JMP_(ENTRY_CODE(Sp[0]));
71 FN_(unblockAsyncExceptionszh_fast)
74 /* Args: R1 :: IO a */
75 STK_CHK_GEN(2, R1_PTR, unblockAsyncExceptionszh_fast, );
77 if (CurrentTSO->blocked_exceptions != NULL) {
78 awakenBlockedQueue(CurrentTSO->blocked_exceptions);
79 CurrentTSO->blocked_exceptions = NULL;
81 Sp[0] = (W_)&blockAsyncExceptionszh_ret_info;
85 JMP_(GET_ENTRY(R1.cl));
89 INFO_TABLE_SRT_BITMAP(blockAsyncExceptionszh_ret_info, blockAsyncExceptionszh_ret_entry, 0, 0, 0, 0, RET_SMALL, , EF_, 0, 0);
90 FN_(blockAsyncExceptionszh_ret_entry)
93 ASSERT(CurrentTSO->blocked_exceptions == NULL);
94 CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
96 JMP_(ENTRY_CODE(Sp[0]));
101 FN_(killThreadzh_fast)
104 /* args: R1.p = TSO to kill, R2.p = Exception */
106 /* If the target thread is currently blocking async exceptions,
107 * we'll have to block until it's ready to accept them.
109 if (R1.t->blocked_exceptions != NULL) {
111 /* ToDo (SMP): locking if destination thread is currently
114 CurrentTSO->link = R1.t->blocked_exceptions;
115 R1.t->blocked_exceptions = CurrentTSO;
117 CurrentTSO->why_blocked = BlockedOnException;
118 CurrentTSO->block_info.tso = R1.t;
120 BLOCK( R1_PTR | R2_PTR, killThreadzh_fast );
123 /* Killed threads turn into zombies, which might be garbage
124 * collected at a later date. That's why we don't have to
125 * explicitly remove them from any queues they might be on.
128 /* We might have killed ourselves. In which case, better be *very*
129 * careful. If the exception killed us, then return to the scheduler.
130 * If the exception went to a catch frame, we'll just continue from
133 if (R1.t == CurrentTSO) {
134 SaveThreadState(); /* inline! */
135 STGCALL2(raiseAsync, R1.t, R2.cl);
136 if (CurrentTSO->whatNext == ThreadKilled) {
137 R1.w = ThreadYielding;
141 if (CurrentTSO->whatNext == ThreadEnterGHC) {
144 JMP_(GET_ENTRY(R1.cl));
146 barf("killThreadzh_fast");
149 STGCALL2(raiseAsync, R1.t, R2.cl);
152 JMP_(ENTRY_CODE(Sp[0]));
156 /* -----------------------------------------------------------------------------
158 -------------------------------------------------------------------------- */
160 #define CATCH_FRAME_ENTRY_TEMPLATE(label,ret) \
165 Su = ((StgCatchFrame *)Sp)->link; \
166 Sp += sizeofW(StgCatchFrame); \
171 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_entry,ENTRY_CODE(Sp[0]));
172 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_0_entry,RET_VEC(Sp[0],0));
173 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_1_entry,RET_VEC(Sp[0],1));
174 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_2_entry,RET_VEC(Sp[0],2));
175 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_3_entry,RET_VEC(Sp[0],3));
176 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_4_entry,RET_VEC(Sp[0],4));
177 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_5_entry,RET_VEC(Sp[0],5));
178 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_6_entry,RET_VEC(Sp[0],6));
179 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_7_entry,RET_VEC(Sp[0],7));
182 #define CATCH_FRAME_BITMAP 7
184 #define CATCH_FRAME_BITMAP 3
187 /* Catch frames are very similar to update frames, but when entering
188 * one we just pop the frame off the stack and perform the correct
189 * kind of return to the activation record underneath us on the stack.
192 VEC_POLY_INFO_TABLE(catch_frame, CATCH_FRAME_BITMAP, NULL/*srt*/, 0/*srt_off*/, 0/*srt_len*/, CATCH_FRAME,, EF_);
194 /* -----------------------------------------------------------------------------
195 * The catch infotable
197 * This should be exactly the same as would be generated by this STG code
199 * catch = {x,h} \n {} -> catch#{x,h}
201 * It is used in deleteThread when reverting blackholes.
202 * -------------------------------------------------------------------------- */
204 INFO_TABLE(catch_info,catch_entry,2,0,FUN,,EF_,0,0);
208 R2.cl = payloadCPtr(R1.cl,1); /* h */
209 R1.cl = payloadCPtr(R1.cl,0); /* x */
219 /* args: R1 = m, R2 = handler */
220 STK_CHK_GEN(sizeofW(StgCatchFrame), R1_PTR | R2_PTR, catchzh_fast, );
221 Sp -= sizeofW(StgCatchFrame);
222 fp = (StgCatchFrame *)Sp;
223 SET_HDR(fp,(StgInfoTable *)&catch_frame_info,CCCS);
224 fp -> handler = R2.cl;
225 fp -> exceptions_blocked = (CurrentTSO->blocked_exceptions != NULL);
227 Su = (StgUpdateFrame *)fp;
228 TICK_CATCHF_PUSHED();
230 JMP_(GET_ENTRY(R1.cl));
235 /* -----------------------------------------------------------------------------
236 * The raise infotable
238 * This should be exactly the same as would be generated by this STG code
240 * raise = {err} \n {} -> raise#{err}
242 * It is used in raisezh_fast to update thunks on the update list
243 * -------------------------------------------------------------------------- */
245 INFO_TABLE(raise_info,raise_entry,1,0,FUN,,EF_,0,0);
249 R1.cl = R1.cl->payload[0];
258 StgClosure *raise_closure;
260 /* args : R1 = error */
263 #if defined(PROFILING)
265 /* Debugging tool: on raising an exception, show where we are. */
267 /* ToDo: currently this is a hack. Would be much better if
268 * the info was only displayed for an *uncaught* exception.
270 if (RtsFlags.ProfFlags.showCCSOnException) {
271 STGCALL2(print_ccs,stderr,CCCS);
278 /* This closure represents the expression 'raise# E' where E
279 * is the exception raise. It is used to overwrite all the
280 * thunks which are currently under evaluataion.
282 raise_closure = (StgClosure *)RET_STGCALL1(P_,allocate,
283 sizeofW(StgClosure)+1);
284 raise_closure->header.info = &raise_info;
285 raise_closure->payload[0] = R1.cl;
289 switch (get_itbl(p)->type) {
292 UPD_IND(p->updatee,raise_closure);
297 p = ((StgSeqFrame *)p)->link;
305 barf("raisezh_fast: STOP_FRAME");
308 barf("raisezh_fast: weird activation record");
315 /* Ok, p points to the enclosing CATCH_FRAME. Pop everything down to
316 * and including this frame, update Su, push R1, and enter the handler.
318 Su = ((StgCatchFrame *)p)->link;
319 handler = ((StgCatchFrame *)p)->handler;
321 Sp = (P_)p + sizeofW(StgCatchFrame) - 1;
323 /* Restore the blocked/unblocked state for asynchronous exceptions
324 * at the CATCH_FRAME.
326 * If exceptions were unblocked, arrange that they are unblocked
327 * again after executing the handler by pushing an
328 * unblockAsyncExceptions_ret stack frame.
330 if (! ((StgCatchFrame *)p)->exceptions_blocked) {
331 *(Sp--) = (W_)&unblockAsyncExceptionszh_ret_info;
334 /* Ensure that async excpetions are blocked when running the handler.
336 if (CurrentTSO->blocked_exceptions == NULL) {
337 CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
340 /* Enter the handler, passing the exception value as an argument.
345 JMP_(GET_ENTRY(R1.cl));