[project @ 2000-01-14 11:45:21 by hwloidl]
[ghc-hetmet.git] / ghc / rts / Exception.hc
1 /* -----------------------------------------------------------------------------
2  * $Id: Exception.hc,v 1.4 2000/01/14 11:45:21 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       awakenBlockedQueue(CurrentTSO->blocked_exceptions, 
70                          CurrentTSO->block_info.closure);
71 #elif defined(PAR)
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);
76 #else
77     awakenBlockedQueue(CurrentTSO->blocked_exceptions);
78 #endif
79     CurrentTSO->blocked_exceptions = NULL;
80     Sp++;
81     JMP_(ENTRY_CODE(Sp[0]));
82   FE_
83 }
84
85 FN_(unblockAsyncExceptionszh_fast)
86 {
87   FB_
88     /* Args: R1 :: IO a */
89     STK_CHK_GEN(2, R1_PTR, unblockAsyncExceptionszh_fast, );
90
91     if (CurrentTSO->blocked_exceptions != NULL) {
92 #if defined(GRAN)
93       awakenBlockedQueue(CurrentTSO->blocked_exceptions, 
94                          CurrentTSO->block_info.closure);
95 #elif defined(PAR)
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);
100 #else
101       awakenBlockedQueue(CurrentTSO->blocked_exceptions);
102 #endif
103       CurrentTSO->blocked_exceptions = NULL;
104       Sp--;
105       Sp[0] = (W_)&blockAsyncExceptionszh_ret_info;
106     }
107     Sp--;
108     Sp[0] = ARG_TAG(0);
109     JMP_(GET_ENTRY(R1.cl));
110   FE_
111 }
112
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)
115 {
116   FB_
117     ASSERT(CurrentTSO->blocked_exceptions == NULL);
118     CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
119     Sp++;
120     JMP_(ENTRY_CODE(Sp[0]));
121   FE_
122 }
123
124
125 FN_(killThreadzh_fast)
126 {
127   FB_
128   /* args: R1.p = TSO to kill, R2.p = Exception */
129
130   /* If the target thread is currently blocking async exceptions,
131    * we'll have to block until it's ready to accept them.
132    */
133   if (R1.t->blocked_exceptions != NULL) {
134
135         /* ToDo (SMP): locking if destination thread is currently
136          * running...
137          */
138         CurrentTSO->link = R1.t->blocked_exceptions;
139         R1.t->blocked_exceptions = CurrentTSO;
140
141         CurrentTSO->why_blocked = BlockedOnException;
142         CurrentTSO->block_info.tso = R1.t;
143
144         BLOCK( R1_PTR | R2_PTR, killThreadzh_fast );
145   }
146
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.
150    */
151
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
155    * the handler.
156    */
157   if (R1.t == CurrentTSO) {
158         SaveThreadState();      /* inline! */
159         STGCALL2(raiseAsync, R1.t, R2.cl);
160         if (CurrentTSO->whatNext == ThreadKilled) {
161                 R1.w = ThreadYielding;
162                 JMP_(StgReturn);
163         }
164         LoadThreadState();
165         if (CurrentTSO->whatNext == ThreadEnterGHC) {
166                 R1.w = Sp[0];
167                 Sp++;
168                 JMP_(GET_ENTRY(R1.cl));
169         } else {
170                 barf("killThreadzh_fast");
171         }
172   } else {
173         STGCALL2(raiseAsync, R1.t, R2.cl);
174   }
175
176   JMP_(ENTRY_CODE(Sp[0]));
177   FE_
178 }
179
180 /* -----------------------------------------------------------------------------
181    Catch frames
182    -------------------------------------------------------------------------- */
183
184 #define CATCH_FRAME_ENTRY_TEMPLATE(label,ret)   \
185    FN_(label);                                  \
186    FN_(label)                                   \
187    {                                            \
188       FB_                                       \
189       Su = ((StgCatchFrame *)Sp)->link;         \
190       Sp += sizeofW(StgCatchFrame);             \
191       JMP_(ret);                                \
192       FE_                                       \
193    }
194
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));
204
205 #ifdef PROFILING
206 #define CATCH_FRAME_BITMAP 7
207 #else
208 #define CATCH_FRAME_BITMAP 3
209 #endif
210
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.
214  */
215
216 VEC_POLY_INFO_TABLE(catch_frame, CATCH_FRAME_BITMAP, NULL/*srt*/, 0/*srt_off*/, 0/*srt_len*/, CATCH_FRAME,, EF_);
217
218 /* -----------------------------------------------------------------------------
219  * The catch infotable
220  *
221  * This should be exactly the same as would be generated by this STG code
222  *
223  * catch = {x,h} \n {} -> catch#{x,h}
224  *
225  * It is used in deleteThread when reverting blackholes.
226  * -------------------------------------------------------------------------- */
227
228 INFO_TABLE(catch_info,catch_entry,2,0,FUN,,EF_,0,0);
229 STGFUN(catch_entry)
230 {
231   FB_
232   R2.cl = payloadCPtr(R1.cl,1); /* h */
233   R1.cl = payloadCPtr(R1.cl,0); /* x */
234   JMP_(catchzh_fast);
235   FE_
236 }
237
238 FN_(catchzh_fast)
239 {
240   StgCatchFrame *fp;
241   FB_
242
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);
250     fp -> link = Su;
251     Su = (StgUpdateFrame *)fp;
252     TICK_CATCHF_PUSHED();
253     TICK_ENT_VIA_NODE();
254     JMP_(GET_ENTRY(R1.cl));
255     
256   FE_
257 }      
258
259 /* -----------------------------------------------------------------------------
260  * The raise infotable
261  * 
262  * This should be exactly the same as would be generated by this STG code
263  *
264  *   raise = {err} \n {} -> raise#{err}
265  *
266  * It is used in raisezh_fast to update thunks on the update list
267  * -------------------------------------------------------------------------- */
268
269 INFO_TABLE(raise_info,raise_entry,1,0,FUN,,EF_,0,0);
270 STGFUN(raise_entry)
271 {
272   FB_
273   R1.cl = R1.cl->payload[0];
274   JMP_(raisezh_fast);
275   FE_
276 }
277
278 FN_(raisezh_fast)
279 {
280   StgClosure *handler;
281   StgUpdateFrame *p;
282   StgClosure *raise_closure;
283   FB_
284     /* args : R1 = error */
285
286
287 #if defined(PROFILING)
288
289     /* Debugging tool: on raising an  exception, show where we are. */
290
291     /* ToDo: currently this is a hack.  Would be much better if
292      * the info was only displayed for an *uncaught* exception.
293      */
294     if (RtsFlags.ProfFlags.showCCSOnException) {
295       STGCALL2(print_ccs,stderr,CCCS);
296     }
297
298 #endif
299
300     p = Su;
301
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.
305      */
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;
310
311     while (1) {
312
313       switch (get_itbl(p)->type) {
314
315       case UPDATE_FRAME:
316         UPD_IND(p->updatee,raise_closure);
317         p = p->link;
318         continue;
319
320       case SEQ_FRAME:
321         p = ((StgSeqFrame *)p)->link;
322         continue;
323
324       case CATCH_FRAME:
325         /* found it! */
326         break;
327
328       case STOP_FRAME:
329         barf("raisezh_fast: STOP_FRAME");
330
331       default:
332         barf("raisezh_fast: weird activation record");
333       }
334       
335       break;
336
337     }
338     
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.
341      */
342     Su = ((StgCatchFrame *)p)->link; 
343     handler = ((StgCatchFrame *)p)->handler;
344     
345     Sp = (P_)p + sizeofW(StgCatchFrame) - 1;
346
347     /* Restore the blocked/unblocked state for asynchronous exceptions
348      * at the CATCH_FRAME.  
349      *
350      * If exceptions were unblocked, arrange that they are unblocked
351      * again after executing the handler by pushing an
352      * unblockAsyncExceptions_ret stack frame.
353      */
354     if (! ((StgCatchFrame *)p)->exceptions_blocked) {
355       *(Sp--) = (W_)&unblockAsyncExceptionszh_ret_info;
356     }
357
358     /* Ensure that async excpetions are blocked when running the handler.
359     */
360     if (CurrentTSO->blocked_exceptions == NULL) {
361       CurrentTSO->blocked_exceptions = END_TSO_QUEUE;
362     }
363
364     /* Enter the handler, passing the exception value as an argument.
365      */
366     *Sp = R1.w;
367     TICK_ENT_VIA_NODE();
368     R1.cl = handler;
369     JMP_(GET_ENTRY(R1.cl));
370
371   FE_
372 }