1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2004
7 * ---------------------------------------------------------------------------*/
12 /* -----------------------------------------------------------------------------
15 We have two layers of update macros. The top layer, UPD_IND() and
16 friends perform all the work of an update. In detail:
18 - if the closure being updated is a blocking queue, then all the
19 threads waiting on the blocking queue are updated.
21 - then the lower level updateWithIndirection() macro is invoked
22 to actually replace the closure with an indirection (see below).
24 -------------------------------------------------------------------------- */
27 # define UPD_IND(updclosure, heapptr) \
28 UPD_PERM_IND(updclosure,heapptr)
29 # define UPD_SPEC_IND(updclosure, ind_info, heapptr, and_then) \
30 UPD_PERM_IND(updclosure,heapptr); and_then
33 # define UPD_IND(updclosure, heapptr) \
34 UPD_REAL_IND(updclosure,INFO_PTR(stg_IND_info),heapptr,SEMI)
35 # define UPD_SPEC_IND(updclosure, ind_info, heapptr, and_then) \
36 UPD_REAL_IND(updclosure,ind_info,heapptr,and_then)
39 /* These macros have to work in both C and C--, so here's the
45 #define DECLARE_IPTR(info) W_ info
46 #define FCALL foreign "C"
47 #define INFO_PTR(info) info
52 #define DECLARE_IPTR(info) const StgInfoTable *(info)
53 #define FCALL /* nothing */
54 #define INFO_PTR(info) &info
55 #define StgBlockingQueue_blocking_queue(closure) \
56 (((StgBlockingQueue *)closure)->blocking_queue)
57 #define ARG_PTR /* nothing */
60 /* UPD_IND actually does a PERM_IND if TICKY_TICKY is on;
61 if you *really* need an IND use UPD_REAL_IND
63 #define UPD_REAL_IND(updclosure, ind_info, heapptr, and_then) \
66 info = GET_INFO(updclosure); \
67 updateWithIndirection(ind_info, \
73 #if defined(PROFILING) || defined(TICKY_TICKY)
74 #define UPD_PERM_IND(updclosure, heapptr) \
76 updateWithPermIndirection(updclosure, \
81 #if defined(RTS_SUPPORTS_THREADS)
84 # define UPD_IND_NOLOCK(updclosure, heapptr) \
86 updateWithPermIndirection(updclosure, \
90 # define UPD_IND_NOLOCK(updclosure, heapptr) \
92 updateWithIndirection(INFO_PTR(stg_IND_info), \
99 #define UPD_IND_NOLOCK(updclosure,heapptr) UPD_IND(updclosure,heapptr)
102 /* -----------------------------------------------------------------------------
103 Awaken any threads waiting on a blocking queue (BLACKHOLE_BQ).
104 -------------------------------------------------------------------------- */
109 In a parallel setup several types of closures might have a blocking queue:
110 BLACKHOLE_BQ ... same as in the default concurrent setup; it will be
111 reawakened via calling UPD_IND on that closure after
112 having finished the computation of the graph
113 FETCH_ME_BQ ... a global indirection (FETCH_ME) may be entered by a
114 local TSO, turning it into a FETCH_ME_BQ; it will be
115 reawakened via calling processResume
116 RBH ... a revertible black hole may be entered by another
117 local TSO, putting it onto its blocking queue; since
118 RBHs only exist while the corresponding closure is in
119 transit, they will be reawakened via calling
120 convertToFetchMe (upon processing an ACK message)
122 In a parallel setup a blocking queue may contain 3 types of closures:
123 TSO ... as in the default concurrent setup
124 BLOCKED_FETCH ... indicating that a TSO on another PE is waiting for
125 the result of the current computation
126 CONSTR ... an RBHSave closure (which contains data ripped out of
127 the closure to make room for a blocking queue; since
128 it only contains data we use the exisiting type of
129 a CONSTR closure); this closure is the end of a
130 blocking queue for an RBH closure; it only exists in
131 this kind of blocking queue and must be at the end
134 extern void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
135 #define DO_AWAKEN_BQ(bqe, node) STGCALL2(awakenBlockedQueue, bqe, node);
137 #define AWAKEN_BQ(info,closure) \
138 if (info == &stg_BLACKHOLE_BQ_info || \
139 info == &stg_FETCH_ME_BQ_info || \
140 get_itbl(closure)->type == RBH) { \
141 DO_AWAKEN_BQ(((StgBlockingQueue *)closure)->blocking_queue, closure); \
146 extern void awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node);
147 #define DO_AWAKEN_BQ(bq, node) STGCALL2(awakenBlockedQueue, bq, node);
149 /* In GranSim we don't have FETCH_ME or FETCH_ME_BQ closures, so they are
150 not checked. The rest of the code is the same as for GUM.
152 #define AWAKEN_BQ(info,closure) \
153 if (info == &stg_BLACKHOLE_BQ_info || \
154 get_itbl(closure)->type == RBH) { \
155 DO_AWAKEN_BQ(((StgBlockingQueue *)closure)->blocking_queue, closure); \
158 #endif /* GRAN || PAR */
160 /* -----------------------------------------------------------------------------
161 Updates: lower-level macros which update a closure with an
162 indirection to another closure.
164 There are several variants of this code.
167 -------------------------------------------------------------------------- */
170 * We call LDV_recordDead_FILL_SLOP_DYNAMIC(p1) regardless of the generation in
174 * After all, we do *NOT* need to call LDV_RECORD_CREATE() for both IND and
175 * IND_OLDGEN closures because they are inherently used. But, it corrupts
176 * the invariants that every closure keeps its creation time in the profiling
177 * field. So, we call LDV_RECORD_CREATE().
180 /* In the DEBUG case, we also zero out the slop of the old closure,
181 * so that the sanity checker can tell where the next closure is.
183 * Two important invariants: we should never try to update a closure
184 * to point to itself, and the closure being updated should not
185 * already have been updated (the mutable list will get messed up
190 #define DEBUG_FILL_SLOP(p) /* nothing */
196 #define DEBUG_FILL_SLOP(p) \
201 inf = %GET_STD_INFO(p); \
202 np = TO_W_(%INFO_PTRS(inf)); \
203 nw = TO_W_(%INFO_NPTRS(inf)); \
204 if (%INFO_TYPE(inf) != HALF_W_(THUNK_SELECTOR)) { \
208 StgClosure_payload(p,i) = 0; \
215 #else /* !CMINUSMINUS */
218 DEBUG_FILL_SLOP(StgClosure *p)
220 StgInfoTable *inf = get_itbl(p);
221 nat np = inf->layout.payload.ptrs,
222 nw = inf->layout.payload.nptrs, i;
223 if (inf->type != THUNK_SELECTOR) {
224 for (i = 0; i < np + nw; i++) {
225 ((StgClosure *)p)->payload[i] = 0;
230 #endif /* CMINUSMINUS */
233 /* We have two versions of this macro (sadly), one for use in C-- code,
234 * and the other for C.
236 * The and_then argument is a performance hack so that we can paste in
237 * the continuation code directly. It helps shave a couple of
238 * instructions off the common case in the update code, which is
239 * worthwhile (the update code is often part of the inner loop).
240 * (except that gcc now appears to common up this code again and
241 * invert the optimisation. Grrrr --SDM).
244 #define generation(n) (W_[generations] + n*SIZEOF_generation)
245 #define updateWithIndirection(ind_info, p1, p2, and_then) \
248 /* ASSERT( p1 != p2 && !closure_IND(p1) ); \
249 */ LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC(p1); \
251 if (bdescr_gen_no(bd) == 0 :: CInt) { \
252 StgInd_indirectee(p1) = p2; \
253 SET_INFO(p1, ind_info); \
254 LDV_RECORD_CREATE(p1); \
255 TICK_UPD_NEW_IND(); \
258 DEBUG_FILL_SLOP(p1); \
259 foreign "C" recordMutableGen(p1 "ptr", \
260 generation(TO_W_(bdescr_gen_no(bd))) "ptr"); \
261 StgInd_indirectee(p1) = p2; \
262 SET_INFO(p1, stg_IND_OLDGEN_info); \
263 LDV_RECORD_CREATE(p1); \
264 TICK_UPD_OLD_IND(); \
268 #define updateWithIndirection(ind_info, p1, p2, and_then) \
272 ASSERT( (P_)p1 != (P_)p2 && !closure_IND(p1) ); \
273 LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC(p1); \
274 bd = Bdescr((P_)p1); \
275 if (bd->gen_no == 0) { \
276 ((StgInd *)p1)->indirectee = p2; \
277 SET_INFO(p1, ind_info); \
278 LDV_RECORD_CREATE(p1); \
279 TICK_UPD_NEW_IND(); \
282 DEBUG_FILL_SLOP(p1); \
283 recordMutableGen(p1, &generations[bd->gen_no]); \
284 ((StgInd *)p1)->indirectee = p2; \
285 SET_INFO(p1, &stg_IND_OLDGEN_info); \
286 TICK_UPD_OLD_IND(); \
292 /* The permanent indirection version isn't performance critical. We
293 * therefore use an inline C function instead of the C-- macro.
297 updateWithPermIndirection(StgClosure *p1,
302 ASSERT( p1 != p2 && !closure_IND(p1) );
306 * Destroy the old closure.
307 * Nb: LDV_* stuff cannot mix with ticky-ticky
309 LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC(p1);
312 if (bd->gen_no == 0) {
313 ((StgInd *)p1)->indirectee = p2;
314 SET_INFO(p1, &stg_IND_PERM_info);
317 * We have just created a new closure.
319 LDV_RECORD_CREATE(p1);
320 TICK_UPD_NEW_PERM_IND(p1);
322 recordMutableGen(p1, &generations[bd->gen_no]);
323 ((StgInd *)p1)->indirectee = p2;
324 SET_INFO(p1, &stg_IND_OLDGEN_PERM_info);
327 * We have just created a new closure.
329 LDV_RECORD_CREATE(p1);
330 TICK_UPD_OLD_PERM_IND();
335 #endif /* UPDATES_H */