1 /* -----------------------------------------------------------------------------
2 * $Id: Exception.hc,v 1.5 2000/01/22 18:00:03 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) {
57 Sp[0] = (W_)&blockAsyncExceptionszh_ret_info;
62 JMP_(GET_ENTRY(R1.cl));
66 INFO_TABLE_SRT_BITMAP(unblockAsyncExceptionszh_ret_info, unblockAsyncExceptionszh_ret_entry, 0, 0, 0, 0, RET_SMALL, , EF_, 0, 0);
67 FN_(unblockAsyncExceptionszh_ret_entry)
70 ASSERT(CurrentTSO->blocked_exceptions != NULL);
72 awakenBlockedQueue(CurrentTSO->blocked_exceptions,
73 CurrentTSO->block_info.closure);
75 // is CurrentTSO->block_info.closure always set to the node
76 // holding the blocking queue !? -- HWL
77 awakenBlockedQueue(CurrentTSO->blocked_exceptions,
78 CurrentTSO->block_info.closure);
80 awakenBlockedQueue(CurrentTSO->blocked_exceptions);
82 CurrentTSO->blocked_exceptions = NULL;
84 JMP_(ENTRY_CODE(Sp[0]));
88 FN_(unblockAsyncExceptionszh_fast)
91 /* Args: R1 :: IO a */
92 STK_CHK_GEN(2, R1_PTR, unblockAsyncExceptionszh_fast, );
94 if (CurrentTSO->blocked_exceptions != NULL) {
96 awakenBlockedQueue(CurrentTSO->blocked_exceptions,
97 CurrentTSO->block_info.closure);
99 // is CurrentTSO->block_info.closure always set to the node
100 // holding the blocking queue !? -- HWL
101 awakenBlockedQueue(CurrentTSO->blocked_exceptions,
102 CurrentTSO->block_info.closure);
104 awakenBlockedQueue(CurrentTSO->blocked_exceptions);
106 CurrentTSO->blocked_exceptions = NULL;
108 /* avoid growing the stack unnecessarily */
109 if (Sp[0] != (W_)&blockAsyncExceptionszh_ret_info) {
111 Sp[0] = (W_)&blockAsyncExceptionszh_ret_info;
116 JMP_(GET_ENTRY(R1.cl));
120 INFO_TABLE_SRT_BITMAP(blockAsyncExceptionszh_ret_info, blockAsyncExceptionszh_ret_entry, 0, 0, 0, 0, RET_SMALL, , EF_, 0, 0);
121 FN_(blockAsyncExceptionszh_ret_entry)
124 ASSERT(CurrentTSO->blocked_exceptions == NULL);
125 CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
127 JMP_(ENTRY_CODE(Sp[0]));
132 FN_(killThreadzh_fast)
135 /* args: R1.p = TSO to kill, R2.p = Exception */
137 /* This thread may have been relocated.
138 * (see Schedule.c:threadStackOverflow)
140 while (R1.t->whatNext == ThreadRelocated) {
144 /* If the target thread is currently blocking async exceptions,
145 * we'll have to block until it's ready to accept them.
147 if (R1.t->blocked_exceptions != NULL) {
149 /* ToDo (SMP): locking if destination thread is currently
152 CurrentTSO->link = R1.t->blocked_exceptions;
153 R1.t->blocked_exceptions = CurrentTSO;
155 CurrentTSO->why_blocked = BlockedOnException;
156 CurrentTSO->block_info.tso = R1.t;
158 BLOCK( R1_PTR | R2_PTR, killThreadzh_fast );
161 /* Killed threads turn into zombies, which might be garbage
162 * collected at a later date. That's why we don't have to
163 * explicitly remove them from any queues they might be on.
166 /* We might have killed ourselves. In which case, better be *very*
167 * careful. If the exception killed us, then return to the scheduler.
168 * If the exception went to a catch frame, we'll just continue from
171 if (R1.t == CurrentTSO) {
172 SaveThreadState(); /* inline! */
173 STGCALL2(raiseAsync, R1.t, R2.cl);
174 if (CurrentTSO->whatNext == ThreadKilled) {
175 R1.w = ThreadYielding;
179 if (CurrentTSO->whatNext == ThreadEnterGHC) {
182 JMP_(GET_ENTRY(R1.cl));
184 barf("killThreadzh_fast");
187 STGCALL2(raiseAsync, R1.t, R2.cl);
190 JMP_(ENTRY_CODE(Sp[0]));
194 /* -----------------------------------------------------------------------------
196 -------------------------------------------------------------------------- */
198 #define CATCH_FRAME_ENTRY_TEMPLATE(label,ret) \
203 Su = ((StgCatchFrame *)Sp)->link; \
204 Sp += sizeofW(StgCatchFrame); \
209 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_entry,ENTRY_CODE(Sp[0]));
210 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_0_entry,RET_VEC(Sp[0],0));
211 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_1_entry,RET_VEC(Sp[0],1));
212 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_2_entry,RET_VEC(Sp[0],2));
213 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_3_entry,RET_VEC(Sp[0],3));
214 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_4_entry,RET_VEC(Sp[0],4));
215 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_5_entry,RET_VEC(Sp[0],5));
216 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_6_entry,RET_VEC(Sp[0],6));
217 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_7_entry,RET_VEC(Sp[0],7));
220 #define CATCH_FRAME_BITMAP 7
222 #define CATCH_FRAME_BITMAP 3
225 /* Catch frames are very similar to update frames, but when entering
226 * one we just pop the frame off the stack and perform the correct
227 * kind of return to the activation record underneath us on the stack.
230 VEC_POLY_INFO_TABLE(catch_frame, CATCH_FRAME_BITMAP, NULL/*srt*/, 0/*srt_off*/, 0/*srt_len*/, CATCH_FRAME,, EF_);
232 /* -----------------------------------------------------------------------------
233 * The catch infotable
235 * This should be exactly the same as would be generated by this STG code
237 * catch = {x,h} \n {} -> catch#{x,h}
239 * It is used in deleteThread when reverting blackholes.
240 * -------------------------------------------------------------------------- */
242 INFO_TABLE(catch_info,catch_entry,2,0,FUN,,EF_,0,0);
246 R2.cl = payloadCPtr(R1.cl,1); /* h */
247 R1.cl = payloadCPtr(R1.cl,0); /* x */
257 /* args: R1 = m, R2 = handler */
258 STK_CHK_GEN(sizeofW(StgCatchFrame), R1_PTR | R2_PTR, catchzh_fast, );
259 Sp -= sizeofW(StgCatchFrame);
260 fp = (StgCatchFrame *)Sp;
261 SET_HDR(fp,(StgInfoTable *)&catch_frame_info,CCCS);
262 fp -> handler = R2.cl;
263 fp -> exceptions_blocked = (CurrentTSO->blocked_exceptions != NULL);
265 Su = (StgUpdateFrame *)fp;
266 TICK_CATCHF_PUSHED();
268 JMP_(GET_ENTRY(R1.cl));
273 /* -----------------------------------------------------------------------------
274 * The raise infotable
276 * This should be exactly the same as would be generated by this STG code
278 * raise = {err} \n {} -> raise#{err}
280 * It is used in raisezh_fast to update thunks on the update list
281 * -------------------------------------------------------------------------- */
283 INFO_TABLE(raise_info,raise_entry,1,0,FUN,,EF_,0,0);
287 R1.cl = R1.cl->payload[0];
296 StgClosure *raise_closure;
298 /* args : R1 = error */
301 #if defined(PROFILING)
303 /* Debugging tool: on raising an exception, show where we are. */
305 /* ToDo: currently this is a hack. Would be much better if
306 * the info was only displayed for an *uncaught* exception.
308 if (RtsFlags.ProfFlags.showCCSOnException) {
309 STGCALL2(print_ccs,stderr,CCCS);
316 /* This closure represents the expression 'raise# E' where E
317 * is the exception raise. It is used to overwrite all the
318 * thunks which are currently under evaluataion.
320 raise_closure = (StgClosure *)RET_STGCALL1(P_,allocate,
321 sizeofW(StgClosure)+1);
322 raise_closure->header.info = &raise_info;
323 raise_closure->payload[0] = R1.cl;
327 switch (get_itbl(p)->type) {
330 UPD_IND(p->updatee,raise_closure);
335 p = ((StgSeqFrame *)p)->link;
343 barf("raisezh_fast: STOP_FRAME");
346 barf("raisezh_fast: weird activation record");
353 /* Ok, p points to the enclosing CATCH_FRAME. Pop everything down to
354 * and including this frame, update Su, push R1, and enter the handler.
356 Su = ((StgCatchFrame *)p)->link;
357 handler = ((StgCatchFrame *)p)->handler;
359 Sp = (P_)p + sizeofW(StgCatchFrame) - 1;
361 /* Restore the blocked/unblocked state for asynchronous exceptions
362 * at the CATCH_FRAME.
364 * If exceptions were unblocked, arrange that they are unblocked
365 * again after executing the handler by pushing an
366 * unblockAsyncExceptions_ret stack frame.
368 if (! ((StgCatchFrame *)p)->exceptions_blocked) {
369 *(Sp--) = (W_)&unblockAsyncExceptionszh_ret_info;
372 /* Ensure that async excpetions are blocked when running the handler.
374 if (CurrentTSO->blocked_exceptions == NULL) {
375 CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
378 /* Enter the handler, passing the exception value as an argument.
383 JMP_(GET_ENTRY(R1.cl));