1 /* -----------------------------------------------------------------------------
2 * $Id: Exception.hc,v 1.4 2000/01/14 11:45:21 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);
69 awakenBlockedQueue(CurrentTSO->blocked_exceptions,
70 CurrentTSO->block_info.closure);
72 // is CurrentTSO->block_info.closure always set to the node
73 // holding the blocking queue !? -- HWL
74 awakenBlockedQueue(CurrentTSO->blocked_exceptions,
75 CurrentTSO->block_info.closure);
77 awakenBlockedQueue(CurrentTSO->blocked_exceptions);
79 CurrentTSO->blocked_exceptions = NULL;
81 JMP_(ENTRY_CODE(Sp[0]));
85 FN_(unblockAsyncExceptionszh_fast)
88 /* Args: R1 :: IO a */
89 STK_CHK_GEN(2, R1_PTR, unblockAsyncExceptionszh_fast, );
91 if (CurrentTSO->blocked_exceptions != NULL) {
93 awakenBlockedQueue(CurrentTSO->blocked_exceptions,
94 CurrentTSO->block_info.closure);
96 // is CurrentTSO->block_info.closure always set to the node
97 // holding the blocking queue !? -- HWL
98 awakenBlockedQueue(CurrentTSO->blocked_exceptions,
99 CurrentTSO->block_info.closure);
101 awakenBlockedQueue(CurrentTSO->blocked_exceptions);
103 CurrentTSO->blocked_exceptions = NULL;
105 Sp[0] = (W_)&blockAsyncExceptionszh_ret_info;
109 JMP_(GET_ENTRY(R1.cl));
113 INFO_TABLE_SRT_BITMAP(blockAsyncExceptionszh_ret_info, blockAsyncExceptionszh_ret_entry, 0, 0, 0, 0, RET_SMALL, , EF_, 0, 0);
114 FN_(blockAsyncExceptionszh_ret_entry)
117 ASSERT(CurrentTSO->blocked_exceptions == NULL);
118 CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
120 JMP_(ENTRY_CODE(Sp[0]));
125 FN_(killThreadzh_fast)
128 /* args: R1.p = TSO to kill, R2.p = Exception */
130 /* If the target thread is currently blocking async exceptions,
131 * we'll have to block until it's ready to accept them.
133 if (R1.t->blocked_exceptions != NULL) {
135 /* ToDo (SMP): locking if destination thread is currently
138 CurrentTSO->link = R1.t->blocked_exceptions;
139 R1.t->blocked_exceptions = CurrentTSO;
141 CurrentTSO->why_blocked = BlockedOnException;
142 CurrentTSO->block_info.tso = R1.t;
144 BLOCK( R1_PTR | R2_PTR, killThreadzh_fast );
147 /* Killed threads turn into zombies, which might be garbage
148 * collected at a later date. That's why we don't have to
149 * explicitly remove them from any queues they might be on.
152 /* We might have killed ourselves. In which case, better be *very*
153 * careful. If the exception killed us, then return to the scheduler.
154 * If the exception went to a catch frame, we'll just continue from
157 if (R1.t == CurrentTSO) {
158 SaveThreadState(); /* inline! */
159 STGCALL2(raiseAsync, R1.t, R2.cl);
160 if (CurrentTSO->whatNext == ThreadKilled) {
161 R1.w = ThreadYielding;
165 if (CurrentTSO->whatNext == ThreadEnterGHC) {
168 JMP_(GET_ENTRY(R1.cl));
170 barf("killThreadzh_fast");
173 STGCALL2(raiseAsync, R1.t, R2.cl);
176 JMP_(ENTRY_CODE(Sp[0]));
180 /* -----------------------------------------------------------------------------
182 -------------------------------------------------------------------------- */
184 #define CATCH_FRAME_ENTRY_TEMPLATE(label,ret) \
189 Su = ((StgCatchFrame *)Sp)->link; \
190 Sp += sizeofW(StgCatchFrame); \
195 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_entry,ENTRY_CODE(Sp[0]));
196 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_0_entry,RET_VEC(Sp[0],0));
197 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_1_entry,RET_VEC(Sp[0],1));
198 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_2_entry,RET_VEC(Sp[0],2));
199 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_3_entry,RET_VEC(Sp[0],3));
200 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_4_entry,RET_VEC(Sp[0],4));
201 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_5_entry,RET_VEC(Sp[0],5));
202 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_6_entry,RET_VEC(Sp[0],6));
203 CATCH_FRAME_ENTRY_TEMPLATE(catch_frame_7_entry,RET_VEC(Sp[0],7));
206 #define CATCH_FRAME_BITMAP 7
208 #define CATCH_FRAME_BITMAP 3
211 /* Catch frames are very similar to update frames, but when entering
212 * one we just pop the frame off the stack and perform the correct
213 * kind of return to the activation record underneath us on the stack.
216 VEC_POLY_INFO_TABLE(catch_frame, CATCH_FRAME_BITMAP, NULL/*srt*/, 0/*srt_off*/, 0/*srt_len*/, CATCH_FRAME,, EF_);
218 /* -----------------------------------------------------------------------------
219 * The catch infotable
221 * This should be exactly the same as would be generated by this STG code
223 * catch = {x,h} \n {} -> catch#{x,h}
225 * It is used in deleteThread when reverting blackholes.
226 * -------------------------------------------------------------------------- */
228 INFO_TABLE(catch_info,catch_entry,2,0,FUN,,EF_,0,0);
232 R2.cl = payloadCPtr(R1.cl,1); /* h */
233 R1.cl = payloadCPtr(R1.cl,0); /* x */
243 /* args: R1 = m, R2 = handler */
244 STK_CHK_GEN(sizeofW(StgCatchFrame), R1_PTR | R2_PTR, catchzh_fast, );
245 Sp -= sizeofW(StgCatchFrame);
246 fp = (StgCatchFrame *)Sp;
247 SET_HDR(fp,(StgInfoTable *)&catch_frame_info,CCCS);
248 fp -> handler = R2.cl;
249 fp -> exceptions_blocked = (CurrentTSO->blocked_exceptions != NULL);
251 Su = (StgUpdateFrame *)fp;
252 TICK_CATCHF_PUSHED();
254 JMP_(GET_ENTRY(R1.cl));
259 /* -----------------------------------------------------------------------------
260 * The raise infotable
262 * This should be exactly the same as would be generated by this STG code
264 * raise = {err} \n {} -> raise#{err}
266 * It is used in raisezh_fast to update thunks on the update list
267 * -------------------------------------------------------------------------- */
269 INFO_TABLE(raise_info,raise_entry,1,0,FUN,,EF_,0,0);
273 R1.cl = R1.cl->payload[0];
282 StgClosure *raise_closure;
284 /* args : R1 = error */
287 #if defined(PROFILING)
289 /* Debugging tool: on raising an exception, show where we are. */
291 /* ToDo: currently this is a hack. Would be much better if
292 * the info was only displayed for an *uncaught* exception.
294 if (RtsFlags.ProfFlags.showCCSOnException) {
295 STGCALL2(print_ccs,stderr,CCCS);
302 /* This closure represents the expression 'raise# E' where E
303 * is the exception raise. It is used to overwrite all the
304 * thunks which are currently under evaluataion.
306 raise_closure = (StgClosure *)RET_STGCALL1(P_,allocate,
307 sizeofW(StgClosure)+1);
308 raise_closure->header.info = &raise_info;
309 raise_closure->payload[0] = R1.cl;
313 switch (get_itbl(p)->type) {
316 UPD_IND(p->updatee,raise_closure);
321 p = ((StgSeqFrame *)p)->link;
329 barf("raisezh_fast: STOP_FRAME");
332 barf("raisezh_fast: weird activation record");
339 /* Ok, p points to the enclosing CATCH_FRAME. Pop everything down to
340 * and including this frame, update Su, push R1, and enter the handler.
342 Su = ((StgCatchFrame *)p)->link;
343 handler = ((StgCatchFrame *)p)->handler;
345 Sp = (P_)p + sizeofW(StgCatchFrame) - 1;
347 /* Restore the blocked/unblocked state for asynchronous exceptions
348 * at the CATCH_FRAME.
350 * If exceptions were unblocked, arrange that they are unblocked
351 * again after executing the handler by pushing an
352 * unblockAsyncExceptions_ret stack frame.
354 if (! ((StgCatchFrame *)p)->exceptions_blocked) {
355 *(Sp--) = (W_)&unblockAsyncExceptionszh_ret_info;
358 /* Ensure that async excpetions are blocked when running the handler.
360 if (CurrentTSO->blocked_exceptions == NULL) {
361 CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
364 /* Enter the handler, passing the exception value as an argument.
369 JMP_(GET_ENTRY(R1.cl));