1 /* -----------------------------------------------------------------------------
2 * $Id: Exception.hc,v 1.25 2002/04/23 06:34:26 sof Exp $
4 * (c) The GHC Team, 1998-2000
8 * ---------------------------------------------------------------------------*/
12 #include "Exception.h"
21 #if defined(PROFILING)
22 # include "Profiling.h"
25 /* -----------------------------------------------------------------------------
28 A thread can request that asynchronous exceptions not be delivered
29 ("blocked") for the duration of an I/O computation. The primitive
31 blockAsyncExceptions# :: IO a -> IO a
33 is used for this purpose. During a blocked section, asynchronous
34 exceptions may be unblocked again temporarily:
36 unblockAsyncExceptions# :: IO a -> IO a
38 Furthermore, asynchronous exceptions are blocked automatically during
39 the execution of an exception handler. Both of these primitives
40 leave a continuation on the stack which reverts to the previous
41 state (blocked or unblocked) on exit.
43 A thread which wants to raise an exception in another thread (using
44 killThread#) must block until the target thread is ready to receive
45 it. The action of unblocking exceptions in a thread will release all
46 the threads waiting to deliver exceptions to that thread.
48 -------------------------------------------------------------------------- */
50 FN_(blockAsyncExceptionszh_fast)
53 /* Args: R1 :: IO a */
54 STK_CHK_GEN( 2/* worst case */, R1_PTR, blockAsyncExceptionszh_fast, );
56 if (CurrentTSO->blocked_exceptions == NULL) {
57 CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
58 /* avoid growing the stack unnecessarily */
59 if (Sp[0] == (W_)&stg_blockAsyncExceptionszh_ret_info) {
63 Sp[0] = (W_)&stg_unblockAsyncExceptionszh_ret_info;
68 JMP_(GET_ENTRY(R1.cl));
72 INFO_TABLE_SRT_BITMAP(stg_unblockAsyncExceptionszh_ret_info, stg_unblockAsyncExceptionszh_ret_entry, 0, 0, 0, 0, RET_SMALL, , EF_, 0, 0);
73 FN_(stg_unblockAsyncExceptionszh_ret_entry)
76 ASSERT(CurrentTSO->blocked_exceptions != NULL);
78 awakenBlockedQueue(CurrentTSO->blocked_exceptions,
81 /* we don't need node info (2nd arg) in this case
82 (note that CurrentTSO->block_info.closure isn't always set) */
83 awakenBlockedQueue(CurrentTSO->blocked_exceptions,
86 awakenBlockedQueue(CurrentTSO->blocked_exceptions);
88 CurrentTSO->blocked_exceptions = NULL;
91 JMP_(ENTRY_CODE(Sp[0]));
95 JMP_(ENTRY_CODE(Sp[1]));
100 FN_(unblockAsyncExceptionszh_fast)
103 /* Args: R1 :: IO a */
104 STK_CHK_GEN(2, R1_PTR, unblockAsyncExceptionszh_fast, );
106 if (CurrentTSO->blocked_exceptions != NULL) {
108 awakenBlockedQueue(CurrentTSO->blocked_exceptions,
109 CurrentTSO->block_info.closure);
111 // is CurrentTSO->block_info.closure always set to the node
112 // holding the blocking queue !? -- HWL
113 awakenBlockedQueue(CurrentTSO->blocked_exceptions,
114 CurrentTSO->block_info.closure);
116 awakenBlockedQueue(CurrentTSO->blocked_exceptions);
118 CurrentTSO->blocked_exceptions = NULL;
120 /* avoid growing the stack unnecessarily */
121 if (Sp[0] == (W_)&stg_unblockAsyncExceptionszh_ret_info) {
125 Sp[0] = (W_)&stg_blockAsyncExceptionszh_ret_info;
130 JMP_(GET_ENTRY(R1.cl));
134 INFO_TABLE_SRT_BITMAP(stg_blockAsyncExceptionszh_ret_info, stg_blockAsyncExceptionszh_ret_entry, 0, 0, 0, 0, RET_SMALL, , EF_, 0, 0);
135 FN_(stg_blockAsyncExceptionszh_ret_entry)
138 ASSERT(CurrentTSO->blocked_exceptions == NULL);
139 CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
142 JMP_(ENTRY_CODE(Sp[0]));
146 JMP_(ENTRY_CODE(Sp[1]));
151 FN_(killThreadzh_fast)
154 /* args: R1.p = TSO to kill, R2.p = Exception */
156 /* This thread may have been relocated.
157 * (see Schedule.c:threadStackOverflow)
159 while (R1.t->what_next == ThreadRelocated) {
163 /* If the target thread is currently blocking async exceptions,
164 * we'll have to block until it's ready to accept them. The
165 * exception is interruptible threads - ie. those that are blocked
168 if (R1.t->blocked_exceptions != NULL && !interruptible(R1.t) ) {
170 /* ToDo (SMP): locking if destination thread is currently
173 CurrentTSO->link = R1.t->blocked_exceptions;
174 R1.t->blocked_exceptions = CurrentTSO;
176 CurrentTSO->why_blocked = BlockedOnException;
177 CurrentTSO->block_info.tso = R1.t;
179 BLOCK( R1_PTR | R2_PTR, killThreadzh_fast );
182 /* Killed threads turn into zombies, which might be garbage
183 * collected at a later date. That's why we don't have to
184 * explicitly remove them from any queues they might be on.
187 /* We might have killed ourselves. In which case, better be *very*
188 * careful. If the exception killed us, then return to the scheduler.
189 * If the exception went to a catch frame, we'll just continue from
192 if (R1.t == CurrentTSO) {
193 SaveThreadState(); /* inline! */
194 STGCALL2(raiseAsyncWithLock, R1.t, R2.cl);
195 if (CurrentTSO->what_next == ThreadKilled) {
196 R1.w = ThreadFinished;
200 if (CurrentTSO->what_next == ThreadEnterGHC) {
203 JMP_(GET_ENTRY(R1.cl));
205 barf("killThreadzh_fast");
208 STGCALL2(raiseAsyncWithLock, R1.t, R2.cl);
211 JMP_(ENTRY_CODE(Sp[0]));
216 /* -----------------------------------------------------------------------------
218 -------------------------------------------------------------------------- */
221 #define CATCH_FRAME_ENTRY_TEMPLATE(label,ret) \
226 Su = ((StgCatchFrame *)Sp)->link; \
227 Sp += sizeofW(StgCatchFrame); \
232 #define CATCH_FRAME_ENTRY_TEMPLATE(label,ret) \
240 Su = ((StgCatchFrame *)Sp)->link; \
241 Sp += sizeofW(StgCatchFrame) - 1; \
254 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_entry,ENTRY_CODE(Sp[SP_OFF]));
255 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_0_entry,RET_VEC(Sp[SP_OFF],0));
256 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_1_entry,RET_VEC(Sp[SP_OFF],1));
257 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_2_entry,RET_VEC(Sp[SP_OFF],2));
258 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_3_entry,RET_VEC(Sp[SP_OFF],3));
259 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_4_entry,RET_VEC(Sp[SP_OFF],4));
260 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_5_entry,RET_VEC(Sp[SP_OFF],5));
261 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_6_entry,RET_VEC(Sp[SP_OFF],6));
262 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_7_entry,RET_VEC(Sp[SP_OFF],7));
264 #if defined(PROFILING)
265 #define CATCH_FRAME_BITMAP 15
267 #define CATCH_FRAME_BITMAP 3
270 /* Catch frames are very similar to update frames, but when entering
271 * one we just pop the frame off the stack and perform the correct
272 * kind of return to the activation record underneath us on the stack.
275 VEC_POLY_INFO_TABLE(stg_catch_frame, CATCH_FRAME_BITMAP, NULL/*srt*/, 0/*srt_off*/, 0/*srt_len*/, CATCH_FRAME,, EF_);
277 /* -----------------------------------------------------------------------------
278 * The catch infotable
280 * This should be exactly the same as would be generated by this STG code
282 * catch = {x,h} \n {} -> catch#{x,h}
284 * It is used in deleteThread when reverting blackholes.
285 * -------------------------------------------------------------------------- */
287 INFO_TABLE(stg_catch_info,stg_catch_entry,2,0,FUN,,EF_,0,0);
288 STGFUN(stg_catch_entry)
291 R2.cl = R1.cl->payload[1]; /* h */
292 R1.cl = R1.cl->payload[0]; /* x */
302 /* args: R1 = m :: IO a, R2 = handler :: Exception -> IO a */
303 STK_CHK_GEN(sizeofW(StgCatchFrame) + 1, R1_PTR | R2_PTR, catchzh_fast, );
305 /* Set up the catch frame */
306 Sp -= sizeofW(StgCatchFrame);
307 fp = (StgCatchFrame *)Sp;
308 SET_HDR(fp,(StgInfoTable *)&stg_catch_frame_info,CCCS);
309 fp -> handler = R2.cl;
310 fp -> exceptions_blocked = (CurrentTSO->blocked_exceptions != NULL);
312 Su = (StgUpdateFrame *)fp;
313 TICK_CATCHF_PUSHED();
315 /* Push realworld token and enter R1. */
319 JMP_(GET_ENTRY(R1.cl));
324 /* -----------------------------------------------------------------------------
325 * The raise infotable
327 * This should be exactly the same as would be generated by this STG code
329 * raise = {err} \n {} -> raise#{err}
331 * It is used in raisezh_fast to update thunks on the update list
332 * -------------------------------------------------------------------------- */
334 INFO_TABLE(stg_raise_info,stg_raise_entry,1,0,THUNK,,EF_,0,0);
335 STGFUN(stg_raise_entry)
338 R1.cl = R1.cl->payload[0];
347 StgClosure *raise_closure;
349 /* args : R1 = exception */
352 #if defined(PROFILING)
353 /* Debugging tool: on raising an exception, show where we are. */
355 /* ToDo: currently this is a hack. Would be much better if
356 * the info was only displayed for an *uncaught* exception.
358 if (RtsFlags.ProfFlags.showCCSOnException) {
359 STGCALL2(fprintCCS,stderr,CCCS);
365 /* This closure represents the expression 'raise# E' where E
366 * is the exception raise. It is used to overwrite all the
367 * thunks which are currently under evaluataion.
371 // stg_raise_info has THUNK as its closure type. Since a THUNK takes at least
372 // MIN_UPD_SIZE words in its payload, MIN_UPD_SIZE is more approprate than 1.
373 // It seems that 1 does not cause any problem unless profiling is performed.
374 // However, when LDV profiling goes on, we need to linearly scan small object pool,
375 // where raise_closure is stored, so we should use MIN_UPD_SIZE.
376 raise_closure = (StgClosure *)RET_STGCALL1(P_,allocate,
377 sizeofW(StgClosure)+1);
379 raise_closure = (StgClosure *)RET_STGCALL1(P_,allocate,
380 sizeofW(StgClosure)+MIN_UPD_SIZE);
381 SET_HDR(raise_closure, &stg_raise_info, CCCS);
382 raise_closure->payload[0] = R1.cl;
386 switch (get_itbl(p)->type) {
389 UPD_IND(p->updatee,raise_closure);
394 p = ((StgSeqFrame *)p)->link;
402 /* We've stripped the entire stack, the thread is now dead. */
403 Sp = CurrentTSO->stack + CurrentTSO->stack_size - 1;
404 Sp[0] = R1.w; /* save the exception */
405 Su = (StgUpdateFrame *)(Sp+1);
406 CurrentTSO->what_next = ThreadKilled;
407 SaveThreadState(); /* inline! */
408 R1.w = ThreadFinished;
412 barf("raisezh_fast: weird activation record");
419 /* Ok, p points to the enclosing CATCH_FRAME. Pop everything down to
420 * and including this frame, update Su, push R1, and enter the handler.
422 Su = ((StgCatchFrame *)p)->link;
423 handler = ((StgCatchFrame *)p)->handler;
425 Sp = (P_)p + sizeofW(StgCatchFrame);
427 /* Restore the blocked/unblocked state for asynchronous exceptions
428 * at the CATCH_FRAME.
430 * If exceptions were unblocked, arrange that they are unblocked
431 * again after executing the handler by pushing an
432 * unblockAsyncExceptions_ret stack frame.
434 if (! ((StgCatchFrame *)p)->exceptions_blocked) {
435 *(--Sp) = (W_)&stg_unblockAsyncExceptionszh_ret_info;
438 /* Ensure that async excpetions are blocked when running the handler.
440 if (CurrentTSO->blocked_exceptions == NULL) {
441 CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
444 /* Enter the handler, passing the exception value and a realworld
445 * token as arguments.
452 JMP_(GET_ENTRY(R1.cl));