D2 weak references

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Wed Apr 22 13:14:39 PDT 2009


Leandro Lucarella wrote:
> I think instead of a notification mechanism access to the mark bit can be
> provided.
> 
> The mark phase is ran with all thread paused, so you don't need any
> notifications in that phase because nobody can ask the weak ref object for
> the underlaying pointer then.
> 
> When threads are resumed, the mark phase is complete, so the invariant of
> all live objects having the mark bit set and all the garbage having the
> bit unset should hold. If the GC guarantee that the mark bit is set
> *always* when a object is live (the mark bit clearing should be done with
> all threads stopped and that should be it), except only when the mark
> phase is running, then, a weak ref implementation can look at the mark bit
> to safely see if the reference is garbage or not (even before the sweep
> phase find it and call the finalizer).
> 
> I think this approach is both simple and efficient, it doesn't have races
> at all (unless I'm missing something, of course). The only problem is it's
> too tied to the current GC implementation. But I think any weak ref
> implementation will, so it looks like weak ref really belong to the GC.

I think you're missing something. Consider this sequence of events:
- Object A is collected by the GC. (And its mark bit M is cleared)
- Object B is allocated, and gets the memory formerly used by object A. Mark bit 
M is set.
- A weak reference to A gets used; it checks mark bit M, which is set...

In other words, the "this object was collected" state needs to be stored in the 
weak reference, not in the object that got collected (or anything tied to its 
memory location) because that memory may get reused.



More information about the Digitalmars-d mailing list