summaryrefslogtreecommitdiff
path: root/ghc/runtime/gum/RBH.lc
blob: 18fef5a22aa4c3442f66044fd23bd2ca2676c0fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
%
% (c) The AQUA/Parade Projects, Glasgow University, 1995
%
%************************************************************************
%*                                                                      *
\section[RBH.lc]{Revertible Black Hole Manipulation}
%*									*
%************************************************************************

\begin{code}
#if defined(PAR) || defined(GRAN) /* whole file */

#include "rtsdefs.h"
\end{code}

Turn a closure into a revertible black hole.  After the conversion,
the first two words of the closure will be a link to the mutables
list (if appropriate for the garbage collector), and a pointer
to the blocking queue.  The blocking queue is terminated by a 2-word
SPEC closure which holds the original contents of the first two
words of the closure.

\begin{code}
EXTFUN(RBH_Save_0_info);
EXTFUN(RBH_Save_1_info);
EXTFUN(RBH_Save_2_info);

P_
convertToRBH(closure)
P_ closure;
{
    P_ infoPtr, newInfoPtr;
    W_ size, ptrs, nonptrs, vhs;
    P_	rbh_save;
    rtsBool isSpec;
    char str[80];

    if ((rbh_save = AllocateHeap(SPEC_HS + 2)) == NULL)
	return NULL;

    infoPtr = get_closure_info(closure, &size, &ptrs, &nonptrs, &vhs, str);
    ASSERT(size >= MIN_UPD_SIZE);

    switch (BASE_INFO_TYPE(infoPtr)) {
    case INFO_SPEC_TYPE:
	isSpec = rtsTrue;
	break;
    case INFO_GEN_TYPE:
	isSpec = rtsFalse;
	break;
    default:
	fprintf(stderr, "Panic: turn %#lx (IP %#lx) into RBH\n", (W_)closure, (W_)infoPtr);
	EXIT(EXIT_FAILURE);
    }

    /* Fill in the RBH_Save closure with the original data */
    rbh_save[SPEC_HS] = closure[isSpec ? SPEC_HS : GEN_HS];
    rbh_save[SPEC_HS + 1] = closure[(isSpec ? SPEC_HS : GEN_HS) + 1];

    /*
     * Set the info_ptr for the rbh_Save closure according to the number of pointers
     * in the original
     */

    newInfoPtr = (P_) (ptrs == 0 ? RBH_Save_0_info :
                       ptrs == 1 ? RBH_Save_1_info :
                       RBH_Save_2_info);
    SET_INFO_PTR(rbh_save, newInfoPtr);

    /* Do some magic garbage collection mangling on the first word */

#if defined(GCap) || defined(GCgn)

    /*
     * If the closure's in the old generation, we have to make sure it goes on the
     * mutables list
     */

    if (closure <= StorageMgrInfo.OldLim) {
	MUT_LINK(closure) = (W_) StorageMgrInfo.OldMutables;
	StorageMgrInfo.OldMutables = closure;
    } else
	MUT_LINK(closure) = MUT_NOT_LINKED;
#endif

    /*
     * Second word points to the RBH_Save closure with the original data. This may
     * become a blocking queue terminated by the RBH_Save closure.
     */
    if (isSpec)
        SPEC_RBH_BQ(closure) = (W_) rbh_save;
    else
	GEN_RBH_BQ(closure) = (W_) rbh_save;

    /* OK, now actually turn it into a RBH (what a great system!) */
    SET_INFO_PTR(closure, RBH_INFOPTR(INFO_PTR(closure)));

    return closure;
}

\end{code}

Converting a closure to a FetchMe is trivial, unless the closure has
acquired a blocking queue.  If that has happened, we first have to
awaken the blocking queue.  What a nuisance!  Fortunately,
@AwakenBlockingQueue@ should now know what to do.

A note on GrAnSim: In GrAnSim we don't have FetchMe closures. However, we
have to turn a RBH back to its original form when the simulated transfer
of the closure has been finished. Therefore we need the @convertFromRBH@
routine below. After converting the RBH back to its original form and 
awakening all TSOs, the first TSO will reenter the closure which is now
local and carry on merrily reducing it (the other TSO will be less merrily 
blocked on the now local closure; we're costing the difference between
local and global blocks in the BQ code).

\begin{code}
#if defined(PAR)

EXTDATA_RO(FetchMe_info);

void
convertToFetchMe(closure, ga)
P_ closure;
globalAddr *ga;
{
    P_ ip = (P_) INFO_PTR(closure);
    P_ bqe;
#if defined(GCap) || defined(GCgn)    
    rtsBool linked = IS_MUTABLE(ip) && MUT_LINK(closure) != MUT_NOT_LINKED;
#endif

    switch(INFO_TYPE(ip)) {
    case INFO_SPEC_RBH_TYPE:
	bqe = (P_) SPEC_RBH_BQ(closure);
	break;
    case INFO_GEN_RBH_TYPE:
	bqe = (P_) GEN_RBH_BQ(closure);
	break;
    default:
#ifdef DEBUG
	fprintf(stderr, "Weird...just tried to convert %#lx (IP %#lx) to FetchMe\n",
	  closure, ip);
#endif
	return;
    }

    SET_INFO_PTR(closure, FetchMe_info);

#if defined(GCap) || defined(GCgn)
    /* If we modify a fetchme in the old generation,
       we have to make sure it goes on the mutables list */

    if(closure <= StorageMgrInfo.OldLim) {
	if (!linked) {
	    MUT_LINK(closure) = (W_) StorageMgrInfo.OldMutables;
            StorageMgrInfo.OldMutables = closure;
	}
    } else
        MUT_LINK(closure) = MUT_NOT_LINKED;
#endif
    
    FETCHME_GA(closure) = ga;
    if (IS_MUTABLE(INFO_PTR(bqe)))
	AwakenBlockingQueue(bqe);
}
#else  /* GRAN */
/* Prototype */
void UnlinkFromMUT(P_ closure); 

void
convertFromRBH(closure)  /* The corresponding function in GUM is: */  
                         /* convertToFetchMe */
P_ closure;
{
    P_ ip = (P_) INFO_PTR(closure);
    P_ bqe, rbh_save = Prelude_Z91Z93_closure;
    int isSpec;
#if defined(GCap) || defined(GCgn)    
    rtsBool linked = IS_MUTABLE(ip) && MUT_LINK(closure) != MUT_NOT_LINKED;
    P_ oldLink = MUT_LINK(closure);
#endif

    switch(INFO_TYPE(ip)) {
    case INFO_SPEC_RBH_TYPE:
	bqe = (P_) SPEC_RBH_BQ(closure);
	isSpec = 1;
	break;
    case INFO_GEN_RBH_TYPE:
	bqe = (P_) GEN_RBH_BQ(closure);
	isSpec = 0;
	break;
    default:
#if 1
	fprintf(stderr, "Weird...just tried to convert %#lx (IP %#lx) to FetchMe\n",
	  closure, ip);
#endif
	return;
    }

#  if defined(GCap) || defined(GCgn)
    /* If the RBH is turned back to a SPEC or GEN closure we have to take 
       it off  the mutables list */

    if (linked) {
#  if defined(GRAN_CHECK)
      if (RTSflags.GranFlags.debug & 0x100) {
            fprintf(stderr,"\n**>>>> Unlinking closure %#lx from mutables list on PE %d @ %ld (next mutable=%#lx)\n",
                           closure,
                           where_is(closure), CurrentTime[where_is(closure)],
                           MUT_LINK(closure));
            GN(closure);
          }
#  endif
      UnlinkFromMUT(closure);
    }
#  endif
    
    /* FETCHME_GA(closure) = ga; */
    if (IS_MUTABLE(INFO_PTR(bqe))) {
      PROC old_proc = CurrentProc,        /* NB: For AwakenBlockingQueue, */
           new_proc = where_is(closure);  /*     CurentProc must be where */
					  /*     closure lives. */
      CurrentProc = new_proc;

#  if defined(GRAN_CHECK)
      if (RTSflags.GranFlags.debug & 0x100)
        fprintf(stderr,"===== AwBQ of node 0x%lx (%s) [PE %2u]\n",
	               closure, (isSpec ? "SPEC_RBH" : "GEN_RBH"), new_proc);
#  endif

      rbh_save = AwakenBlockingQueue(bqe);     /* AwakenBlockingQueue(bqe); */
      CurrentProc = old_proc;
    } else {
        rbh_save = bqe;
    }

    /* Put data from special RBH save closures back into the closure */
    if ( rbh_save == Prelude_Z91Z93_closure ) {
      fprintf(stderr,"convertFromRBH: No RBH_Save_? closure found at end of BQ!\n");
      EXIT(EXIT_FAILURE);
    } else {
      closure[isSpec ? SPEC_HS : GEN_HS] = rbh_save[SPEC_HS];
      closure[(isSpec ? SPEC_HS : GEN_HS) + 1] = rbh_save[SPEC_HS + 1];
    }

    /* Put back old info pointer (only in GrAnSim) -- HWL */
    SET_INFO_PTR(closure, REVERT_INFOPTR(INFO_PTR(closure)));

#  if (defined(GCap) || defined(GCgn))
    /* If we convert from an RBH in the old generation,
       we have to make sure it goes on the mutables list */

    if(closure <= StorageMgrInfo.OldLim) {
	if (IS_MUTABLE(INFO_PTR(closure)) && MUT_LINK(closure) == MUT_NOT_LINKED) {
	    MUT_LINK(closure) = (W_) StorageMgrInfo.OldMutables;
            StorageMgrInfo.OldMutables = closure;
	}
    }
#  endif
}

/* Remove closure from the mutables list */

void
UnlinkFromMUT(P_ closure) 
{
  P_ curr = StorageMgrInfo.OldMutables, prev = NULL;

  while (curr != NULL && curr != closure) {
    ASSERT(MUT_LINK(curr)!=MUT_NOT_LINKED);
    prev=curr;
    curr=MUT_LINK(curr); 
  }
  if (curr==closure) {   
   if (prev==NULL) 
     StorageMgrInfo.OldMutables = MUT_LINK(curr);
   else   
     MUT_LINK(prev) = MUT_LINK(curr);
   MUT_LINK(curr) = MUT_NOT_LINKED;
  }

#if 0 && (defined(GCap) || defined(GCgn))
  {
    closq newclos;
    extern closq ex_RBH_q;

    newclos = (closq) stgMallocBytes(sizeof(struct clos), "UnlinkFromMUT");
    CLOS_CLOSURE(newclos) = closure;
    CLOS_PREV(newclos) = NULL;
    CLOS_NEXT(newclos) = ex_RBH_q;
    if (ex_RBH_q!=NULL)
      CLOS_PREV(ex_RBH_q) = newclos;
    ex_RBH_q = newclos;
  }
#endif
}

#endif /* PAR */

#endif /* PAR || GRAN -- whole file */
\end{code}