1 /* -----------------------------------------------------------------------------
2 * $Id: Exception.hc,v 1.3 2000/01/13 14:34:02 hwloidl 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;
55 Sp[0] = (W_)&unblockAsyncExceptionszh_ret_info;
59 JMP_(GET_ENTRY(R1.cl));
63 INFO_TABLE_SRT_BITMAP(unblockAsyncExceptionszh_ret_info, unblockAsyncExceptionszh_ret_entry, 0, 0, 0, 0, RET_SMALL, , EF_, 0, 0);
64 FN_(unblockAsyncExceptionszh_ret_entry)
67 ASSERT(CurrentTSO->blocked_exceptions != NULL);
71 // is CurrentTSO->block_info.closure always set to the node
72 // holding the blocking queue !? -- HWL
73 awakenBlockedQueue(CurrentTSO->blocked_exceptions,
74 CurrentTSO->block_info.closure);
76 awakenBlockedQueue(CurrentTSO->blocked_exceptions);
78 CurrentTSO->blocked_exceptions = NULL;
80 JMP_(ENTRY_CODE(Sp[0]));
84 FN_(unblockAsyncExceptionszh_fast)
87 /* Args: R1 :: IO a */
88 STK_CHK_GEN(2, R1_PTR, unblockAsyncExceptionszh_fast, );
90 if (CurrentTSO->blocked_exceptions != NULL) {
94 // is CurrentTSO->block_info.closure always set to the node
95 // holding the blocking queue !? -- HWL
96 awakenBlockedQueue(CurrentTSO->blocked_exceptions,
97 CurrentTSO->block_info.closure);
99 awakenBlockedQueue(CurrentTSO->blocked_exceptions);
101 CurrentTSO->blocked_exceptions = NULL;
103 Sp[0] = (W_)&blockAsyncExceptionszh_ret_info;
107 JMP_(GET_ENTRY(R1.cl));
111 INFO_TABLE_SRT_BITMAP(blockAsyncExceptionszh_ret_info, blockAsyncExceptionszh_ret_entry, 0, 0, 0, 0, RET_SMALL, , EF_, 0, 0);
112 FN_(blockAsyncExceptionszh_ret_entry)
115 ASSERT(CurrentTSO->blocked_exceptions == NULL);
116 CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
118 JMP_(ENTRY_CODE(Sp[0]));
123 FN_(killThreadzh_fast)
126 /* args: R1.p = TSO to kill, R2.p = Exception */
128 /* If the target thread is currently blocking async exceptions,
129 * we'll have to block until it's ready to accept them.
131 if (R1.t->blocked_exceptions != NULL) {
133 /* ToDo (SMP): locking if destination thread is currently
136 CurrentTSO->link = R1.t->blocked_exceptions;
137 R1.t->blocked_exceptions = CurrentTSO;
139 CurrentTSO->why_blocked = BlockedOnException;
140 CurrentTSO->block_info.tso = R1.t;
142 BLOCK( R1_PTR | R2_PTR, killThreadzh_fast );
145 /* Killed threads turn into zombies, which might be garbage
146 * collected at a later date. That's why we don't have to
147 * explicitly remove them from any queues they might be on.
150 /* We might have killed ourselves. In which case, better be *very*
151 * careful. If the exception killed us, then return to the scheduler.
152 * If the exception went to a catch frame, we'll just continue from
155 if (R1.t == CurrentTSO) {
156 SaveThreadState(); /* inline! */
157 STGCALL2(raiseAsync, R1.t, R2.cl);
158 if (CurrentTSO->whatNext == ThreadKilled) {
159 R1.w = ThreadYielding;
163 if (CurrentTSO->whatNext == ThreadEnterGHC) {
166 JMP_(GET_ENTRY(R1.cl));
168 barf("killThreadzh_fast");
171 STGCALL2(raiseAsync, R1.t, R2.cl);
174 JMP_(ENTRY_CODE(Sp[0]));
178 /* -----------------------------------------------------------------------------
180 -------------------------------------------------------------------------- */
182 #define CATCH_FRAME_ENTRY_TEMPLATE(label,ret) \
187 Su = ((StgCatchFrame *)Sp)->link; \
188 Sp += sizeofW(StgCatchFrame); \
193 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_entry,ENTRY_CODE(Sp[0]));
194 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_0_entry,RET_VEC(Sp[0],0));
195 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_1_entry,RET_VEC(Sp[0],1));
196 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_2_entry,RET_VEC(Sp[0],2));
197 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_3_entry,RET_VEC(Sp[0],3));
198 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_4_entry,RET_VEC(Sp[0],4));
199 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_5_entry,RET_VEC(Sp[0],5));
200 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_6_entry,RET_VEC(Sp[0],6));
201 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_7_entry,RET_VEC(Sp[0],7));
204 #define CATCH_FRAME_BITMAP 7
206 #define CATCH_FRAME_BITMAP 3
209 /* Catch frames are very similar to update frames, but when entering
210 * one we just pop the frame off the stack and perform the correct
211 * kind of return to the activation record underneath us on the stack.
214 VEC_POLY_INFO_TABLE(catch_frame, CATCH_FRAME_BITMAP, NULL/*srt*/, 0/*srt_off*/, 0/*srt_len*/, CATCH_FRAME,, EF_);
216 /* -----------------------------------------------------------------------------
217 * The catch infotable
219 * This should be exactly the same as would be generated by this STG code
221 * catch = {x,h} \n {} -> catch#{x,h}
223 * It is used in deleteThread when reverting blackholes.
224 * -------------------------------------------------------------------------- */
226 INFO_TABLE(catch_info,catch_entry,2,0,FUN,,EF_,0,0);
230 R2.cl = payloadCPtr(R1.cl,1); /* h */
231 R1.cl = payloadCPtr(R1.cl,0); /* x */
241 /* args: R1 = m, R2 = handler */
242 STK_CHK_GEN(sizeofW(StgCatchFrame), R1_PTR | R2_PTR, catchzh_fast, );
243 Sp -= sizeofW(StgCatchFrame);
244 fp = (StgCatchFrame *)Sp;
245 SET_HDR(fp,(StgInfoTable *)&catch_frame_info,CCCS);
246 fp -> handler = R2.cl;
247 fp -> exceptions_blocked = (CurrentTSO->blocked_exceptions != NULL);
249 Su = (StgUpdateFrame *)fp;
250 TICK_CATCHF_PUSHED();
252 JMP_(GET_ENTRY(R1.cl));
257 /* -----------------------------------------------------------------------------
258 * The raise infotable
260 * This should be exactly the same as would be generated by this STG code
262 * raise = {err} \n {} -> raise#{err}
264 * It is used in raisezh_fast to update thunks on the update list
265 * -------------------------------------------------------------------------- */
267 INFO_TABLE(raise_info,raise_entry,1,0,FUN,,EF_,0,0);
271 R1.cl = R1.cl->payload[0];
280 StgClosure *raise_closure;
282 /* args : R1 = error */
285 #if defined(PROFILING)
287 /* Debugging tool: on raising an exception, show where we are. */
289 /* ToDo: currently this is a hack. Would be much better if
290 * the info was only displayed for an *uncaught* exception.
292 if (RtsFlags.ProfFlags.showCCSOnException) {
293 STGCALL2(print_ccs,stderr,CCCS);
300 /* This closure represents the expression 'raise# E' where E
301 * is the exception raise. It is used to overwrite all the
302 * thunks which are currently under evaluataion.
304 raise_closure = (StgClosure *)RET_STGCALL1(P_,allocate,
305 sizeofW(StgClosure)+1);
306 raise_closure->header.info = &raise_info;
307 raise_closure->payload[0] = R1.cl;
311 switch (get_itbl(p)->type) {
314 UPD_IND(p->updatee,raise_closure);
319 p = ((StgSeqFrame *)p)->link;
327 barf("raisezh_fast: STOP_FRAME");
330 barf("raisezh_fast: weird activation record");
337 /* Ok, p points to the enclosing CATCH_FRAME. Pop everything down to
338 * and including this frame, update Su, push R1, and enter the handler.
340 Su = ((StgCatchFrame *)p)->link;
341 handler = ((StgCatchFrame *)p)->handler;
343 Sp = (P_)p + sizeofW(StgCatchFrame) - 1;
345 /* Restore the blocked/unblocked state for asynchronous exceptions
346 * at the CATCH_FRAME.
348 * If exceptions were unblocked, arrange that they are unblocked
349 * again after executing the handler by pushing an
350 * unblockAsyncExceptions_ret stack frame.
352 if (! ((StgCatchFrame *)p)->exceptions_blocked) {
353 *(Sp--) = (W_)&unblockAsyncExceptionszh_ret_info;
356 /* Ensure that async excpetions are blocked when running the handler.
358 if (CurrentTSO->blocked_exceptions == NULL) {
359 CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
362 /* Enter the handler, passing the exception value as an argument.
367 JMP_(GET_ENTRY(R1.cl));