Use message-passing to implement throwTo in the RTS
[ghc-hetmet.git] / includes / rts / storage / Closures.h
1 /* ----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-2004
4  *
5  * Closures
6  *
7  * -------------------------------------------------------------------------- */
8
9 #ifndef RTS_STORAGE_CLOSURES_H
10 #define RTS_STORAGE_CLOSURES_H
11
12 /*
13  * The Layout of a closure header depends on which kind of system we're
14  * compiling for: profiling, parallel, ticky, etc.
15  */
16
17 /* -----------------------------------------------------------------------------
18    The profiling header
19    -------------------------------------------------------------------------- */
20
21 typedef struct {
22   CostCentreStack *ccs;
23   union {
24     struct _RetainerSet *rs;  /* Retainer Set */
25     StgWord ldvw;             /* Lag/Drag/Void Word */
26   } hp;
27 } StgProfHeader;
28
29 /* -----------------------------------------------------------------------------
30    The SMP header
31    
32    A thunk has a padding word to take the updated value.  This is so
33    that the update doesn't overwrite the payload, so we can avoid
34    needing to lock the thunk during entry and update.
35    
36    Note: this doesn't apply to THUNK_STATICs, which have no payload.
37
38    Note: we leave this padding word in all ways, rather than just SMP,
39    so that we don't have to recompile all our libraries for SMP.
40    -------------------------------------------------------------------------- */
41
42 typedef struct {
43     StgWord pad;
44 } StgSMPThunkHeader;
45
46 /* -----------------------------------------------------------------------------
47    The full fixed-size closure header
48
49    The size of the fixed header is the sum of the optional parts plus a single
50    word for the entry code pointer.
51    -------------------------------------------------------------------------- */
52
53 typedef struct {
54     const StgInfoTable* info;
55 #ifdef PROFILING
56     StgProfHeader         prof;
57 #endif
58 } StgHeader;
59
60 typedef struct {
61     const StgInfoTable* info;
62 #ifdef PROFILING
63     StgProfHeader         prof;
64 #endif
65     StgSMPThunkHeader     smp;
66 } StgThunkHeader;
67
68 #define THUNK_EXTRA_HEADER_W (sizeofW(StgThunkHeader)-sizeofW(StgHeader))
69
70 /* -----------------------------------------------------------------------------
71    Closure Types
72
73    For any given closure type (defined in InfoTables.h), there is a
74    corresponding structure defined below.  The name of the structure
75    is obtained by concatenating the closure type with '_closure'
76    -------------------------------------------------------------------------- */
77
78 /* All closures follow the generic format */
79
80 typedef struct StgClosure_ {
81     StgHeader   header;
82     struct StgClosure_ *payload[FLEXIBLE_ARRAY];
83 } *StgClosurePtr; // StgClosure defined in Rts.h
84
85 typedef struct {
86     StgThunkHeader  header;
87     struct StgClosure_ *payload[FLEXIBLE_ARRAY];
88 } StgThunk;
89
90 typedef struct {
91     StgThunkHeader   header;
92     StgClosure *selectee;
93 } StgSelector;
94
95 typedef struct {
96     StgHeader   header;
97     StgHalfWord arity;          /* zero if it is an AP */
98     StgHalfWord n_args;
99     StgClosure *fun;            /* really points to a fun */
100     StgClosure *payload[FLEXIBLE_ARRAY];
101 } StgPAP;
102
103 typedef struct {
104     StgThunkHeader   header;
105     StgHalfWord arity;          /* zero if it is an AP */
106     StgHalfWord n_args;
107     StgClosure *fun;            /* really points to a fun */
108     StgClosure *payload[FLEXIBLE_ARRAY];
109 } StgAP;
110
111 typedef struct {
112     StgThunkHeader   header;
113     StgWord     size;                    /* number of words in payload */
114     StgClosure *fun;
115     StgClosure *payload[FLEXIBLE_ARRAY]; /* contains a chunk of *stack* */
116 } StgAP_STACK;
117
118 typedef struct {
119     StgHeader   header;
120     StgClosure *indirectee;
121 } StgInd;
122
123 typedef struct {
124     StgHeader     header;
125     StgClosure   *indirectee;
126     StgClosure   *static_link;
127     StgInfoTable *saved_info;
128 } StgIndStatic;
129
130 typedef struct {
131     StgHeader  header;
132     StgWord    words;
133     StgWord    payload[FLEXIBLE_ARRAY];
134 } StgArrWords;
135
136 typedef struct {
137     StgHeader   header;
138     StgWord     ptrs;
139     StgWord     size; // ptrs plus card table
140     StgClosure *payload[FLEXIBLE_ARRAY];
141     // see also: StgMutArrPtrs macros in ClosureMacros.h
142 } StgMutArrPtrs;
143
144 typedef struct {
145     StgHeader   header;
146     StgClosure *var;
147 } StgMutVar;
148
149 typedef struct _StgUpdateFrame {
150     StgHeader  header;
151     StgClosure *updatee;
152 } StgUpdateFrame;
153
154 typedef struct {
155     StgHeader  header;
156     StgInt      exceptions_blocked;
157     StgClosure *handler;
158 } StgCatchFrame;
159
160 typedef struct {
161     StgHeader  header;
162 } StgStopFrame;  
163
164 typedef struct {
165   StgHeader header;
166   StgWord data;
167 } StgIntCharlikeClosure;
168
169 /* statically allocated */
170 typedef struct {
171   StgHeader  header;
172 } StgRetry;
173
174 typedef struct _StgStableName {
175   StgHeader      header;
176   StgWord        sn;
177 } StgStableName;
178
179 typedef struct _StgWeak {       /* Weak v */
180   StgHeader header;
181   StgClosure *cfinalizer;
182   StgClosure *key;
183   StgClosure *value;            /* v */
184   StgClosure *finalizer;
185   struct _StgWeak *link;
186 } StgWeak;
187
188 typedef struct _StgDeadWeak {   /* Weak v */
189   StgHeader header;
190   struct _StgWeak *link;
191 } StgDeadWeak;
192
193 /* Byte code objects.  These are fixed size objects with pointers to
194  * four arrays, designed so that a BCO can be easily "re-linked" to
195  * other BCOs, to facilitate GHC's intelligent recompilation.  The
196  * array of instructions is static and not re-generated when the BCO
197  * is re-linked, but the other 3 arrays will be regenerated.
198  *
199  * A BCO represents either a function or a stack frame.  In each case,
200  * it needs a bitmap to describe to the garbage collector the
201  * pointerhood of its arguments/free variables respectively, and in
202  * the case of a function it also needs an arity.  These are stored
203  * directly in the BCO, rather than in the instrs array, for two
204  * reasons:
205  * (a) speed: we need to get at the bitmap info quickly when
206  *     the GC is examining APs and PAPs that point to this BCO
207  * (b) a subtle interaction with the compacting GC.  In compacting
208  *     GC, the info that describes the size/layout of a closure
209  *     cannot be in an object more than one level of indirection
210  *     away from the current object, because of the order in
211  *     which pointers are updated to point to their new locations.
212  */
213
214 typedef struct {
215     StgHeader      header;
216     StgArrWords   *instrs;      /* a pointer to an ArrWords */
217     StgArrWords   *literals;    /* a pointer to an ArrWords */
218     StgMutArrPtrs *ptrs;        /* a pointer to a  MutArrPtrs */
219     StgHalfWord   arity;        /* arity of this BCO */
220     StgHalfWord   size;         /* size of this BCO (in words) */
221     StgWord       bitmap[FLEXIBLE_ARRAY];  /* an StgLargeBitmap */
222 } StgBCO;
223
224 #define BCO_BITMAP(bco)      ((StgLargeBitmap *)((StgBCO *)(bco))->bitmap)
225 #define BCO_BITMAP_SIZE(bco) (BCO_BITMAP(bco)->size)
226 #define BCO_BITMAP_BITS(bco) (BCO_BITMAP(bco)->bitmap)
227 #define BCO_BITMAP_SIZEW(bco) ((BCO_BITMAP_SIZE(bco) + BITS_IN(StgWord) - 1) \
228                                 / BITS_IN(StgWord))
229
230 /* -----------------------------------------------------------------------------
231    Dynamic stack frames for generic heap checks.
232
233    These generic heap checks are slow, but have the advantage of being
234    usable in a variety of situations.
235
236    The one restriction is that any relevant SRTs must already be pointed
237    to from the stack.  The return address doesn't need to have an info
238    table attached: hence it can be any old code pointer.
239
240    The liveness mask contains a 1 at bit n, if register Rn contains a
241    non-pointer.  The contents of all 8 vanilla registers are always saved
242    on the stack; the liveness mask tells the GC which ones contain
243    pointers.
244
245    Good places to use a generic heap check: 
246
247         - case alternatives (the return address with an SRT is already
248           on the stack).
249
250         - primitives (no SRT required).
251
252    The stack frame layout for a RET_DYN is like this:
253
254           some pointers         |-- RET_DYN_PTRS(liveness) words
255           some nonpointers      |-- RET_DYN_NONPTRS(liveness) words
256                                
257           L1                    \
258           D1-2                  |-- RET_DYN_NONPTR_REGS_SIZE words
259           F1-4                  /
260                                
261           R1-8                  |-- RET_DYN_BITMAP_SIZE words
262                                
263           return address        \
264           liveness mask         |-- StgRetDyn structure
265           stg_gen_chk_info      /
266
267    we assume that the size of a double is always 2 pointers (wasting a
268    word when it is only one pointer, but avoiding lots of #ifdefs).
269
270    See Liveness.h for the macros (RET_DYN_PTRS() etc.).
271
272    NOTE: if you change the layout of RET_DYN stack frames, then you
273    might also need to adjust the value of RESERVED_STACK_WORDS in
274    Constants.h.
275    -------------------------------------------------------------------------- */
276
277 typedef struct {
278     const StgInfoTable* info;
279     StgWord        liveness;
280     StgWord        ret_addr;
281     StgClosure *   payload[FLEXIBLE_ARRAY];
282 } StgRetDyn;
283
284 /* A function return stack frame: used when saving the state for a
285  * garbage collection at a function entry point.  The function
286  * arguments are on the stack, and we also save the function (its
287  * info table describes the pointerhood of the arguments).
288  *
289  * The stack frame size is also cached in the frame for convenience.
290  */
291 typedef struct {
292     const StgInfoTable* info;
293     StgWord        size;
294     StgClosure *   fun;
295     StgClosure *   payload[FLEXIBLE_ARRAY];
296 } StgRetFun;
297
298 /* Concurrent communication objects */
299
300 typedef struct {
301   StgHeader       header;
302   struct StgTSO_ *head;
303   struct StgTSO_ *tail;
304   StgClosure*     value;
305 } StgMVar;
306
307
308 /* STM data structures
309  *
310  *  StgTVar defines the only type that can be updated through the STM
311  *  interface.
312  * 
313  *  Note that various optimisations may be possible in order to use less
314  *  space for these data structures at the cost of more complexity in the
315  *  implementation:
316  *
317  *   - In StgTVar, current_value and first_watch_queue_entry could be held in
318  *     the same field: if any thread is waiting then its expected_value for
319  *     the tvar is the current value.  
320  *
321  *   - In StgTRecHeader, it might be worthwhile having separate chunks
322  *     of read-only and read-write locations.  This would save a
323  *     new_value field in the read-only locations.
324  *
325  *   - In StgAtomicallyFrame, we could combine the waiting bit into
326  *     the header (maybe a different info tbl for a waiting transaction).
327  *     This means we can specialise the code for the atomically frame
328  *     (it immediately switches on frame->waiting anyway).
329  */
330
331 typedef struct StgTRecHeader_ StgTRecHeader;
332
333 typedef struct StgTVarWatchQueue_ {
334   StgHeader                  header;
335   StgClosure                *closure; // StgTSO or StgAtomicInvariant
336   struct StgTVarWatchQueue_ *next_queue_entry;
337   struct StgTVarWatchQueue_ *prev_queue_entry;
338 } StgTVarWatchQueue;
339
340 typedef struct {
341   StgHeader                  header;
342   StgClosure                *volatile current_value;
343   StgTVarWatchQueue         *volatile first_watch_queue_entry;
344 #if defined(THREADED_RTS)
345   StgInt                     volatile num_updates;
346 #endif
347 } StgTVar;
348
349 typedef struct {
350   StgHeader      header;
351   StgClosure    *code;
352   StgTRecHeader *last_execution;
353   StgWord        lock;
354 } StgAtomicInvariant;
355
356 /* new_value == expected_value for read-only accesses */
357 /* new_value is a StgTVarWatchQueue entry when trec in state TREC_WAITING */
358 typedef struct {
359   StgTVar                   *tvar;
360   StgClosure                *expected_value;
361   StgClosure                *new_value; 
362 #if defined(THREADED_RTS)
363   StgInt                     num_updates;
364 #endif
365 } TRecEntry;
366
367 #define TREC_CHUNK_NUM_ENTRIES 16
368
369 typedef struct StgTRecChunk_ {
370   StgHeader                  header;
371   struct StgTRecChunk_      *prev_chunk;
372   StgWord                    next_entry_idx;
373   TRecEntry                  entries[TREC_CHUNK_NUM_ENTRIES];
374 } StgTRecChunk;
375
376 typedef enum { 
377   TREC_ACTIVE,        /* Transaction in progress, outcome undecided */
378   TREC_CONDEMNED,     /* Transaction in progress, inconsistent / out of date reads */
379   TREC_COMMITTED,     /* Transaction has committed, now updating tvars */
380   TREC_ABORTED,       /* Transaction has aborted, now reverting tvars */
381   TREC_WAITING,       /* Transaction currently waiting */
382 } TRecState;
383
384 typedef struct StgInvariantCheckQueue_ {
385   StgHeader                       header;
386   StgAtomicInvariant             *invariant;
387   StgTRecHeader                  *my_execution;
388   struct StgInvariantCheckQueue_ *next_queue_entry;
389 } StgInvariantCheckQueue;
390
391 struct StgTRecHeader_ {
392   StgHeader                  header;
393   struct StgTRecHeader_     *enclosing_trec;
394   StgTRecChunk              *current_chunk;
395   StgInvariantCheckQueue    *invariants_to_check;
396   TRecState                  state;
397 };
398
399 typedef struct {
400   StgHeader   header;
401   StgClosure *code;
402   StgTVarWatchQueue *next_invariant_to_check;
403   StgClosure *result;
404 } StgAtomicallyFrame;
405
406 typedef struct {
407   StgHeader   header;
408   StgClosure *code;
409   StgClosure *handler;
410 } StgCatchSTMFrame;
411
412 typedef struct {
413   StgHeader      header;
414   StgBool        running_alt_code;
415   StgClosure    *first_code;
416   StgClosure    *alt_code;
417 } StgCatchRetryFrame;
418
419 /* ----------------------------------------------------------------------------
420    Messages
421    ------------------------------------------------------------------------- */
422
423 typedef struct Message_ {
424     StgHeader        header;
425     struct Message_ *link;
426 } Message;
427
428 typedef struct MessageWakeup_ {
429     StgHeader header;
430     Message  *link;
431     StgTSO   *tso;
432 } MessageWakeup;
433
434 typedef struct MessageThrowTo_ {
435     StgHeader   header;
436     Message    *link;
437     StgTSO     *source;
438     StgTSO     *target;
439     StgClosure *exception;
440 } MessageThrowTo;
441
442 #endif /* RTS_STORAGE_CLOSURES_H */