1 /* -----------------------------------------------------------------------------
2 * $Id: Exception.hc,v 1.8 2000/02/24 17:20:46 simonmar Exp $
4 * (c) The GHC Team, 1998-1999
8 * ---------------------------------------------------------------------------*/
11 #include "Exception.h"
21 /* -----------------------------------------------------------------------------
24 A thread can request that asynchronous exceptions not be delivered
25 ("blocked") for the duration of an I/O computation. The primitive
27 blockAsyncExceptions# :: IO a -> IO a
29 is used for this purpose. During a blocked section, asynchronous
30 exceptions may be unblocked again temporarily:
32 unblockAsyncExceptions# :: IO a -> IO a
34 Furthermore, asynchronous exceptions are blocked automatically during
35 the execution of an exception handler. Both of these primitives
36 leave a continuation on the stack which reverts to the previous
37 state (blocked or unblocked) on exit.
39 A thread which wants to raise an exception in another thread (using
40 killThread#) must block until the target thread is ready to receive
41 it. The action of unblocking exceptions in a thread will release all
42 the threads waiting to deliver exceptions to that thread.
44 -------------------------------------------------------------------------- */
46 FN_(blockAsyncExceptionszh_fast)
49 /* Args: R1 :: IO a */
50 STK_CHK_GEN( 2/* worst case */, R1_PTR, blockAsyncExceptionszh_fast, );
52 if (CurrentTSO->blocked_exceptions == NULL) {
53 CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
54 /* avoid growing the stack unnecessarily */
55 if (Sp[0] == (W_)&blockAsyncExceptionszh_ret_info) {
59 Sp[0] = (W_)&unblockAsyncExceptionszh_ret_info;
64 JMP_(GET_ENTRY(R1.cl));
68 INFO_TABLE_SRT_BITMAP(unblockAsyncExceptionszh_ret_info, unblockAsyncExceptionszh_ret_entry, 0, 0, 0, 0, RET_SMALL, , EF_, 0, 0);
69 FN_(unblockAsyncExceptionszh_ret_entry)
72 ASSERT(CurrentTSO->blocked_exceptions != NULL);
74 awakenBlockedQueue(CurrentTSO->blocked_exceptions,
75 CurrentTSO->block_info.closure);
77 // is CurrentTSO->block_info.closure always set to the node
78 // holding the blocking queue !? -- HWL
79 awakenBlockedQueue(CurrentTSO->blocked_exceptions,
80 CurrentTSO->block_info.closure);
82 awakenBlockedQueue(CurrentTSO->blocked_exceptions);
84 CurrentTSO->blocked_exceptions = NULL;
87 JMP_(ENTRY_CODE(Sp[0]));
91 JMP_(ENTRY_CODE(Sp[1]));
96 FN_(unblockAsyncExceptionszh_fast)
99 /* Args: R1 :: IO a */
100 STK_CHK_GEN(2, R1_PTR, unblockAsyncExceptionszh_fast, );
102 if (CurrentTSO->blocked_exceptions != NULL) {
104 awakenBlockedQueue(CurrentTSO->blocked_exceptions,
105 CurrentTSO->block_info.closure);
107 // is CurrentTSO->block_info.closure always set to the node
108 // holding the blocking queue !? -- HWL
109 awakenBlockedQueue(CurrentTSO->blocked_exceptions,
110 CurrentTSO->block_info.closure);
112 awakenBlockedQueue(CurrentTSO->blocked_exceptions);
114 CurrentTSO->blocked_exceptions = NULL;
116 /* avoid growing the stack unnecessarily */
117 if (Sp[0] == (W_)&unblockAsyncExceptionszh_ret_info) {
121 Sp[0] = (W_)&blockAsyncExceptionszh_ret_info;
126 JMP_(GET_ENTRY(R1.cl));
130 INFO_TABLE_SRT_BITMAP(blockAsyncExceptionszh_ret_info, blockAsyncExceptionszh_ret_entry, 0, 0, 0, 0, RET_SMALL, , EF_, 0, 0);
131 FN_(blockAsyncExceptionszh_ret_entry)
134 ASSERT(CurrentTSO->blocked_exceptions == NULL);
135 CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
138 JMP_(ENTRY_CODE(Sp[0]));
142 JMP_(ENTRY_CODE(Sp[1]));
148 FN_(killThreadzh_fast)
151 /* args: R1.p = TSO to kill, R2.p = Exception */
153 /* This thread may have been relocated.
154 * (see Schedule.c:threadStackOverflow)
156 while (R1.t->whatNext == ThreadRelocated) {
160 /* If the target thread is currently blocking async exceptions,
161 * we'll have to block until it's ready to accept them.
163 if (R1.t->blocked_exceptions != NULL) {
165 /* ToDo (SMP): locking if destination thread is currently
168 CurrentTSO->link = R1.t->blocked_exceptions;
169 R1.t->blocked_exceptions = CurrentTSO;
171 CurrentTSO->why_blocked = BlockedOnException;
172 CurrentTSO->block_info.tso = R1.t;
174 BLOCK( R1_PTR | R2_PTR, killThreadzh_fast );
177 /* Killed threads turn into zombies, which might be garbage
178 * collected at a later date. That's why we don't have to
179 * explicitly remove them from any queues they might be on.
182 /* We might have killed ourselves. In which case, better be *very*
183 * careful. If the exception killed us, then return to the scheduler.
184 * If the exception went to a catch frame, we'll just continue from
187 if (R1.t == CurrentTSO) {
188 SaveThreadState(); /* inline! */
189 STGCALL2(raiseAsync, R1.t, R2.cl);
190 if (CurrentTSO->whatNext == ThreadKilled) {
191 R1.w = ThreadYielding;
195 if (CurrentTSO->whatNext == ThreadEnterGHC) {
198 JMP_(GET_ENTRY(R1.cl));
200 barf("killThreadzh_fast");
203 STGCALL2(raiseAsync, R1.t, R2.cl);
206 JMP_(ENTRY_CODE(Sp[0]));
210 /* -----------------------------------------------------------------------------
212 -------------------------------------------------------------------------- */
215 #define CATCH_FRAME_ENTRY_TEMPLATE(label,ret) \
220 Su = ((StgCatchFrame *)Sp)->link; \
221 Sp += sizeofW(StgCatchFrame); \
226 #define CATCH_FRAME_ENTRY_TEMPLATE(label,ret) \
234 Su = ((StgCatchFrame *)Sp)->link; \
235 Sp += sizeofW(StgCatchFrame) - 1; \
248 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_entry,ENTRY_CODE(Sp[SP_OFF]));
249 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_0_entry,RET_VEC(Sp[SP_OFF],0));
250 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_1_entry,RET_VEC(Sp[SP_OFF],1));
251 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_2_entry,RET_VEC(Sp[SP_OFF],2));
252 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_3_entry,RET_VEC(Sp[SP_OFF],3));
253 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_4_entry,RET_VEC(Sp[SP_OFF],4));
254 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_5_entry,RET_VEC(Sp[SP_OFF],5));
255 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_6_entry,RET_VEC(Sp[SP_OFF],6));
256 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_7_entry,RET_VEC(Sp[SP_OFF],7));
259 #define CATCH_FRAME_BITMAP 7
261 #define CATCH_FRAME_BITMAP 3
264 /* Catch frames are very similar to update frames, but when entering
265 * one we just pop the frame off the stack and perform the correct
266 * kind of return to the activation record underneath us on the stack.
269 VEC_POLY_INFO_TABLE(catch_frame, CATCH_FRAME_BITMAP, NULL/*srt*/, 0/*srt_off*/, 0/*srt_len*/, CATCH_FRAME,, EF_);
271 /* -----------------------------------------------------------------------------
272 * The catch infotable
274 * This should be exactly the same as would be generated by this STG code
276 * catch = {x,h} \n {} -> catch#{x,h}
278 * It is used in deleteThread when reverting blackholes.
279 * -------------------------------------------------------------------------- */
281 INFO_TABLE(catch_info,catch_entry,2,0,FUN,,EF_,0,0);
285 R2.cl = payloadCPtr(R1.cl,1); /* h */
286 R1.cl = payloadCPtr(R1.cl,0); /* x */
296 /* args: R1 = m :: IO a, R2 = handler :: Exception -> IO a */
297 STK_CHK_GEN(sizeofW(StgCatchFrame) + 1, R1_PTR | R2_PTR, catchzh_fast, );
299 /* Set up the catch frame */
300 Sp -= sizeofW(StgCatchFrame);
301 fp = (StgCatchFrame *)Sp;
302 SET_HDR(fp,(StgInfoTable *)&catch_frame_info,CCCS);
303 fp -> handler = R2.cl;
304 fp -> exceptions_blocked = (CurrentTSO->blocked_exceptions != NULL);
306 Su = (StgUpdateFrame *)fp;
307 TICK_CATCHF_PUSHED();
309 /* Push realworld token and enter R1. */
313 JMP_(GET_ENTRY(R1.cl));
318 /* -----------------------------------------------------------------------------
319 * The raise infotable
321 * This should be exactly the same as would be generated by this STG code
323 * raise = {err} \n {} -> raise#{err}
325 * It is used in raisezh_fast to update thunks on the update list
326 * -------------------------------------------------------------------------- */
328 INFO_TABLE(raise_info,raise_entry,1,0,FUN,,EF_,0,0);
332 R1.cl = R1.cl->payload[0];
341 StgClosure *raise_closure;
343 /* args : R1 = error */
346 #if defined(PROFILING)
348 /* Debugging tool: on raising an exception, show where we are. */
350 /* ToDo: currently this is a hack. Would be much better if
351 * the info was only displayed for an *uncaught* exception.
353 if (RtsFlags.ProfFlags.showCCSOnException) {
354 STGCALL2(print_ccs,stderr,CCCS);
361 /* This closure represents the expression 'raise# E' where E
362 * is the exception raise. It is used to overwrite all the
363 * thunks which are currently under evaluataion.
365 raise_closure = (StgClosure *)RET_STGCALL1(P_,allocate,
366 sizeofW(StgClosure)+1);
367 raise_closure->header.info = &raise_info;
368 raise_closure->payload[0] = R1.cl;
372 switch (get_itbl(p)->type) {
375 UPD_IND(p->updatee,raise_closure);
380 p = ((StgSeqFrame *)p)->link;
388 barf("raisezh_fast: STOP_FRAME");
391 barf("raisezh_fast: weird activation record");
398 /* Ok, p points to the enclosing CATCH_FRAME. Pop everything down to
399 * and including this frame, update Su, push R1, and enter the handler.
401 Su = ((StgCatchFrame *)p)->link;
402 handler = ((StgCatchFrame *)p)->handler;
404 Sp = (P_)p + sizeofW(StgCatchFrame);
406 /* Restore the blocked/unblocked state for asynchronous exceptions
407 * at the CATCH_FRAME.
409 * If exceptions were unblocked, arrange that they are unblocked
410 * again after executing the handler by pushing an
411 * unblockAsyncExceptions_ret stack frame.
413 if (! ((StgCatchFrame *)p)->exceptions_blocked) {
414 *(--Sp) = (W_)&unblockAsyncExceptionszh_ret_info;
417 /* Ensure that async excpetions are blocked when running the handler.
419 if (CurrentTSO->blocked_exceptions == NULL) {
420 CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
423 /* Enter the handler, passing the exception value and a realworld
424 * token as arguments.
431 JMP_(GET_ENTRY(R1.cl));