1 /* ----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2004
7 * -------------------------------------------------------------------------- */
9 #ifndef RTS_STORAGE_CLOSURES_H
10 #define RTS_STORAGE_CLOSURES_H
13 * The Layout of a closure header depends on which kind of system we're
14 * compiling for: profiling, parallel, ticky, etc.
17 /* -----------------------------------------------------------------------------
19 -------------------------------------------------------------------------- */
24 struct _RetainerSet *rs; /* Retainer Set */
25 StgWord ldvw; /* Lag/Drag/Void Word */
29 /* -----------------------------------------------------------------------------
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.
36 Note: this doesn't apply to THUNK_STATICs, which have no payload.
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 -------------------------------------------------------------------------- */
46 /* -----------------------------------------------------------------------------
47 The full fixed-size closure header
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 -------------------------------------------------------------------------- */
54 const StgInfoTable* info;
61 const StgInfoTable* info;
65 StgSMPThunkHeader smp;
68 #define THUNK_EXTRA_HEADER_W (sizeofW(StgThunkHeader)-sizeofW(StgHeader))
70 /* -----------------------------------------------------------------------------
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 -------------------------------------------------------------------------- */
78 /* All closures follow the generic format */
80 typedef struct StgClosure_ {
82 struct StgClosure_ *payload[FLEXIBLE_ARRAY];
83 } *StgClosurePtr; // StgClosure defined in Rts.h
86 StgThunkHeader header;
87 struct StgClosure_ *payload[FLEXIBLE_ARRAY];
91 StgThunkHeader header;
97 StgHalfWord arity; /* zero if it is an AP */
99 StgClosure *fun; /* really points to a fun */
100 StgClosure *payload[FLEXIBLE_ARRAY];
104 StgThunkHeader header;
105 StgHalfWord arity; /* zero if it is an AP */
107 StgClosure *fun; /* really points to a fun */
108 StgClosure *payload[FLEXIBLE_ARRAY];
112 StgThunkHeader header;
113 StgWord size; /* number of words in payload */
115 StgClosure *payload[FLEXIBLE_ARRAY]; /* contains a chunk of *stack* */
120 StgClosure *indirectee;
125 StgClosure *indirectee;
126 StgClosure *static_link;
127 StgInfoTable *saved_info;
133 StgWord payload[FLEXIBLE_ARRAY];
139 StgClosure *payload[FLEXIBLE_ARRAY];
147 typedef struct _StgUpdateFrame {
154 StgInt exceptions_blocked;
165 } StgIntCharlikeClosure;
167 /* statically allocated */
172 typedef struct _StgStableName {
177 typedef struct _StgWeak { /* Weak v */
179 StgClosure *cfinalizer;
181 StgClosure *value; /* v */
182 StgClosure *finalizer;
183 struct _StgWeak *link;
186 typedef struct _StgDeadWeak { /* Weak v */
188 struct _StgWeak *link;
191 /* Byte code objects. These are fixed size objects with pointers to
192 * four arrays, designed so that a BCO can be easily "re-linked" to
193 * other BCOs, to facilitate GHC's intelligent recompilation. The
194 * array of instructions is static and not re-generated when the BCO
195 * is re-linked, but the other 3 arrays will be regenerated.
197 * A BCO represents either a function or a stack frame. In each case,
198 * it needs a bitmap to describe to the garbage collector the
199 * pointerhood of its arguments/free variables respectively, and in
200 * the case of a function it also needs an arity. These are stored
201 * directly in the BCO, rather than in the instrs array, for two
203 * (a) speed: we need to get at the bitmap info quickly when
204 * the GC is examining APs and PAPs that point to this BCO
205 * (b) a subtle interaction with the compacting GC. In compacting
206 * GC, the info that describes the size/layout of a closure
207 * cannot be in an object more than one level of indirection
208 * away from the current object, because of the order in
209 * which pointers are updated to point to their new locations.
214 StgArrWords *instrs; /* a pointer to an ArrWords */
215 StgArrWords *literals; /* a pointer to an ArrWords */
216 StgMutArrPtrs *ptrs; /* a pointer to a MutArrPtrs */
217 StgHalfWord arity; /* arity of this BCO */
218 StgHalfWord size; /* size of this BCO (in words) */
219 StgWord bitmap[FLEXIBLE_ARRAY]; /* an StgLargeBitmap */
222 #define BCO_BITMAP(bco) ((StgLargeBitmap *)((StgBCO *)(bco))->bitmap)
223 #define BCO_BITMAP_SIZE(bco) (BCO_BITMAP(bco)->size)
224 #define BCO_BITMAP_BITS(bco) (BCO_BITMAP(bco)->bitmap)
225 #define BCO_BITMAP_SIZEW(bco) ((BCO_BITMAP_SIZE(bco) + BITS_IN(StgWord) - 1) \
228 /* -----------------------------------------------------------------------------
229 Dynamic stack frames for generic heap checks.
231 These generic heap checks are slow, but have the advantage of being
232 usable in a variety of situations.
234 The one restriction is that any relevant SRTs must already be pointed
235 to from the stack. The return address doesn't need to have an info
236 table attached: hence it can be any old code pointer.
238 The liveness mask contains a 1 at bit n, if register Rn contains a
239 non-pointer. The contents of all 8 vanilla registers are always saved
240 on the stack; the liveness mask tells the GC which ones contain
243 Good places to use a generic heap check:
245 - case alternatives (the return address with an SRT is already
248 - primitives (no SRT required).
250 The stack frame layout for a RET_DYN is like this:
252 some pointers |-- RET_DYN_PTRS(liveness) words
253 some nonpointers |-- RET_DYN_NONPTRS(liveness) words
256 D1-2 |-- RET_DYN_NONPTR_REGS_SIZE words
259 R1-8 |-- RET_DYN_BITMAP_SIZE words
262 liveness mask |-- StgRetDyn structure
265 we assume that the size of a double is always 2 pointers (wasting a
266 word when it is only one pointer, but avoiding lots of #ifdefs).
268 See Liveness.h for the macros (RET_DYN_PTRS() etc.).
270 NOTE: if you change the layout of RET_DYN stack frames, then you
271 might also need to adjust the value of RESERVED_STACK_WORDS in
273 -------------------------------------------------------------------------- */
276 const StgInfoTable* info;
279 StgClosure * payload[FLEXIBLE_ARRAY];
282 /* A function return stack frame: used when saving the state for a
283 * garbage collection at a function entry point. The function
284 * arguments are on the stack, and we also save the function (its
285 * info table describes the pointerhood of the arguments).
287 * The stack frame size is also cached in the frame for convenience.
290 const StgInfoTable* info;
293 StgClosure * payload[FLEXIBLE_ARRAY];
296 /* Concurrent communication objects */
300 struct StgTSO_ *head;
301 struct StgTSO_ *tail;
306 /* STM data structures
308 * StgTVar defines the only type that can be updated through the STM
311 * Note that various optimisations may be possible in order to use less
312 * space for these data structures at the cost of more complexity in the
315 * - In StgTVar, current_value and first_watch_queue_entry could be held in
316 * the same field: if any thread is waiting then its expected_value for
317 * the tvar is the current value.
319 * - In StgTRecHeader, it might be worthwhile having separate chunks
320 * of read-only and read-write locations. This would save a
321 * new_value field in the read-only locations.
323 * - In StgAtomicallyFrame, we could combine the waiting bit into
324 * the header (maybe a different info tbl for a waiting transaction).
325 * This means we can specialise the code for the atomically frame
326 * (it immediately switches on frame->waiting anyway).
329 typedef struct StgTRecHeader_ StgTRecHeader;
331 typedef struct StgTVarWatchQueue_ {
333 StgClosure *closure; // StgTSO or StgAtomicInvariant
334 struct StgTVarWatchQueue_ *next_queue_entry;
335 struct StgTVarWatchQueue_ *prev_queue_entry;
340 StgClosure *volatile current_value;
341 StgTVarWatchQueue *volatile first_watch_queue_entry;
342 #if defined(THREADED_RTS)
343 StgInt volatile num_updates;
350 StgTRecHeader *last_execution;
352 } StgAtomicInvariant;
354 /* new_value == expected_value for read-only accesses */
355 /* new_value is a StgTVarWatchQueue entry when trec in state TREC_WAITING */
358 StgClosure *expected_value;
359 StgClosure *new_value;
360 #if defined(THREADED_RTS)
365 #define TREC_CHUNK_NUM_ENTRIES 16
367 typedef struct StgTRecChunk_ {
369 struct StgTRecChunk_ *prev_chunk;
370 StgWord next_entry_idx;
371 TRecEntry entries[TREC_CHUNK_NUM_ENTRIES];
375 TREC_ACTIVE, /* Transaction in progress, outcome undecided */
376 TREC_CONDEMNED, /* Transaction in progress, inconsistent / out of date reads */
377 TREC_COMMITTED, /* Transaction has committed, now updating tvars */
378 TREC_ABORTED, /* Transaction has aborted, now reverting tvars */
379 TREC_WAITING, /* Transaction currently waiting */
382 typedef struct StgInvariantCheckQueue_ {
384 StgAtomicInvariant *invariant;
385 StgTRecHeader *my_execution;
386 struct StgInvariantCheckQueue_ *next_queue_entry;
387 } StgInvariantCheckQueue;
389 struct StgTRecHeader_ {
392 struct StgTRecHeader_ *enclosing_trec;
393 StgTRecChunk *current_chunk;
394 StgInvariantCheckQueue *invariants_to_check;
400 StgTVarWatchQueue *next_invariant_to_check;
402 } StgAtomicallyFrame;
412 StgBool running_alt_code;
413 StgClosure *first_code;
414 StgClosure *alt_code;
415 } StgCatchRetryFrame;
417 #endif /* RTS_STORAGE_CLOSURES_H */