[project @ 2000-01-13 14:33:57 by hwloidl]
[ghc-hetmet.git] / ghc / rts / Exception.hc
1 /* -----------------------------------------------------------------------------
2  * $Id: Exception.hc,v 1.3 2000/01/13 14:34:02 hwloidl Exp $
3  *
4  * (c) The GHC Team, 1998-1999
5  *
6  * Exception support
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #include "Rts.h"
11 #include "Exception.h"
12 #include "Schedule.h"
13 #include "StgRun.h"
14 #include "Storage.h"
15 #include "RtsUtils.h"
16 #include "RtsFlags.h"
17 #if defined(PAR)
18 # include "FetchMe.h"
19 #endif
20
21 /* -----------------------------------------------------------------------------
22    Exception Primitives
23
24    A thread can request that asynchronous exceptions not be delivered
25    ("blocked") for the duration of an I/O computation.  The primitive
26    
27         blockAsyncExceptions# :: IO a -> IO a
28
29    is used for this purpose.  During a blocked section, asynchronous
30    exceptions may be unblocked again temporarily:
31
32         unblockAsyncExceptions# :: IO a -> IO a
33
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.
38
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.
43
44    -------------------------------------------------------------------------- */
45
46 FN_(blockAsyncExceptionszh_fast)
47 {
48   FB_
49     /* Args: R1 :: IO a */
50     STK_CHK_GEN( 2/* worst case */, R1_PTR, blockAsyncExceptionszh_fast, );
51
52     if (CurrentTSO->blocked_exceptions == NULL) {
53       CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
54       Sp--;
55       Sp[0] = (W_)&unblockAsyncExceptionszh_ret_info;
56     }
57     Sp--;
58     Sp[0] = ARG_TAG(0);
59     JMP_(GET_ENTRY(R1.cl));
60   FE_
61 }
62
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)
65 {
66   FB_
67     ASSERT(CurrentTSO->blocked_exceptions != NULL);
68 #if defined(GRAN)
69 # error FixME
70 #elif defined(PAR)
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);
75 #else
76     awakenBlockedQueue(CurrentTSO->blocked_exceptions);
77 #endif
78     CurrentTSO->blocked_exceptions = NULL;
79     Sp++;
80     JMP_(ENTRY_CODE(Sp[0]));
81   FE_
82 }
83
84 FN_(unblockAsyncExceptionszh_fast)
85 {
86   FB_
87     /* Args: R1 :: IO a */
88     STK_CHK_GEN(2, R1_PTR, unblockAsyncExceptionszh_fast, );
89
90     if (CurrentTSO->blocked_exceptions != NULL) {
91 #if defined(GRAN)
92 # error FixME
93 #elif defined(PAR)
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);
98 #else
99       awakenBlockedQueue(CurrentTSO->blocked_exceptions);
100 #endif
101       CurrentTSO->blocked_exceptions = NULL;
102       Sp--;
103       Sp[0] = (W_)&blockAsyncExceptionszh_ret_info;
104     }
105     Sp--;
106     Sp[0] = ARG_TAG(0);
107     JMP_(GET_ENTRY(R1.cl));
108   FE_
109 }
110
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)
113 {
114   FB_
115     ASSERT(CurrentTSO->blocked_exceptions == NULL);
116     CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
117     Sp++;
118     JMP_(ENTRY_CODE(Sp[0]));
119   FE_
120 }
121
122
123 FN_(killThreadzh_fast)
124 {
125   FB_
126   /* args: R1.p = TSO to kill, R2.p = Exception */
127
128   /* If the target thread is currently blocking async exceptions,
129    * we'll have to block until it's ready to accept them.
130    */
131   if (R1.t->blocked_exceptions != NULL) {
132
133         /* ToDo (SMP): locking if destination thread is currently
134          * running...
135          */
136         CurrentTSO->link = R1.t->blocked_exceptions;
137         R1.t->blocked_exceptions = CurrentTSO;
138
139         CurrentTSO->why_blocked = BlockedOnException;
140         CurrentTSO->block_info.tso = R1.t;
141
142         BLOCK( R1_PTR | R2_PTR, killThreadzh_fast );
143   }
144
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.
148    */
149
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
153    * the handler.
154    */
155   if (R1.t == CurrentTSO) {
156         SaveThreadState();      /* inline! */
157         STGCALL2(raiseAsync, R1.t, R2.cl);
158         if (CurrentTSO->whatNext == ThreadKilled) {
159                 R1.w = ThreadYielding;
160                 JMP_(StgReturn);
161         }
162         LoadThreadState();
163         if (CurrentTSO->whatNext == ThreadEnterGHC) {
164                 R1.w = Sp[0];
165                 Sp++;
166                 JMP_(GET_ENTRY(R1.cl));
167         } else {
168                 barf("killThreadzh_fast");
169         }
170   } else {
171         STGCALL2(raiseAsync, R1.t, R2.cl);
172   }
173
174   JMP_(ENTRY_CODE(Sp[0]));
175   FE_
176 }
177
178 /* -----------------------------------------------------------------------------
179    Catch frames
180    -------------------------------------------------------------------------- */
181
182 #define CATCH_FRAME_ENTRY_TEMPLATE(label,ret)   \
183    FN_(label);                                  \
184    FN_(label)                                   \
185    {                                            \
186       FB_                                       \
187       Su = ((StgCatchFrame *)Sp)->link;         \
188       Sp += sizeofW(StgCatchFrame);             \
189       JMP_(ret);                                \
190       FE_                                       \
191    }
192
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));
202
203 #ifdef PROFILING
204 #define CATCH_FRAME_BITMAP 7
205 #else
206 #define CATCH_FRAME_BITMAP 3
207 #endif
208
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.
212  */
213
214 VEC_POLY_INFO_TABLE(catch_frame, CATCH_FRAME_BITMAP, NULL/*srt*/, 0/*srt_off*/, 0/*srt_len*/, CATCH_FRAME,, EF_);
215
216 /* -----------------------------------------------------------------------------
217  * The catch infotable
218  *
219  * This should be exactly the same as would be generated by this STG code
220  *
221  * catch = {x,h} \n {} -> catch#{x,h}
222  *
223  * It is used in deleteThread when reverting blackholes.
224  * -------------------------------------------------------------------------- */
225
226 INFO_TABLE(catch_info,catch_entry,2,0,FUN,,EF_,0,0);
227 STGFUN(catch_entry)
228 {
229   FB_
230   R2.cl = payloadCPtr(R1.cl,1); /* h */
231   R1.cl = payloadCPtr(R1.cl,0); /* x */
232   JMP_(catchzh_fast);
233   FE_
234 }
235
236 FN_(catchzh_fast)
237 {
238   StgCatchFrame *fp;
239   FB_
240
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);
248     fp -> link = Su;
249     Su = (StgUpdateFrame *)fp;
250     TICK_CATCHF_PUSHED();
251     TICK_ENT_VIA_NODE();
252     JMP_(GET_ENTRY(R1.cl));
253     
254   FE_
255 }      
256
257 /* -----------------------------------------------------------------------------
258  * The raise infotable
259  * 
260  * This should be exactly the same as would be generated by this STG code
261  *
262  *   raise = {err} \n {} -> raise#{err}
263  *
264  * It is used in raisezh_fast to update thunks on the update list
265  * -------------------------------------------------------------------------- */
266
267 INFO_TABLE(raise_info,raise_entry,1,0,FUN,,EF_,0,0);
268 STGFUN(raise_entry)
269 {
270   FB_
271   R1.cl = R1.cl->payload[0];
272   JMP_(raisezh_fast);
273   FE_
274 }
275
276 FN_(raisezh_fast)
277 {
278   StgClosure *handler;
279   StgUpdateFrame *p;
280   StgClosure *raise_closure;
281   FB_
282     /* args : R1 = error */
283
284
285 #if defined(PROFILING)
286
287     /* Debugging tool: on raising an  exception, show where we are. */
288
289     /* ToDo: currently this is a hack.  Would be much better if
290      * the info was only displayed for an *uncaught* exception.
291      */
292     if (RtsFlags.ProfFlags.showCCSOnException) {
293       STGCALL2(print_ccs,stderr,CCCS);
294     }
295
296 #endif
297
298     p = Su;
299
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.
303      */
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;
308
309     while (1) {
310
311       switch (get_itbl(p)->type) {
312
313       case UPDATE_FRAME:
314         UPD_IND(p->updatee,raise_closure);
315         p = p->link;
316         continue;
317
318       case SEQ_FRAME:
319         p = ((StgSeqFrame *)p)->link;
320         continue;
321
322       case CATCH_FRAME:
323         /* found it! */
324         break;
325
326       case STOP_FRAME:
327         barf("raisezh_fast: STOP_FRAME");
328
329       default:
330         barf("raisezh_fast: weird activation record");
331       }
332       
333       break;
334
335     }
336     
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.
339      */
340     Su = ((StgCatchFrame *)p)->link; 
341     handler = ((StgCatchFrame *)p)->handler;
342     
343     Sp = (P_)p + sizeofW(StgCatchFrame) - 1;
344
345     /* Restore the blocked/unblocked state for asynchronous exceptions
346      * at the CATCH_FRAME.  
347      *
348      * If exceptions were unblocked, arrange that they are unblocked
349      * again after executing the handler by pushing an
350      * unblockAsyncExceptions_ret stack frame.
351      */
352     if (! ((StgCatchFrame *)p)->exceptions_blocked) {
353       *(Sp--) = (W_)&unblockAsyncExceptionszh_ret_info;
354     }
355
356     /* Ensure that async excpetions are blocked when running the handler.
357     */
358     if (CurrentTSO->blocked_exceptions == NULL) {
359       CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
360     }
361
362     /* Enter the handler, passing the exception value as an argument.
363      */
364     *Sp = R1.w;
365     TICK_ENT_VIA_NODE();
366     R1.cl = handler;
367     JMP_(GET_ENTRY(R1.cl));
368
369   FE_
370 }