D2 weak references

Jason House jason.james.house at gmail.com
Wed Apr 22 08:32:24 PDT 2009


Leandro Lucarella Wrote:

> Jason House, el 21 de abril a las 21:11 me escribiste:
> > Another alternative I came up with is notification when the GC clears or sets 
> > a mark on an object.  That would allow the weak ref to know that its 
> > information is suspect.  That approach would only required that the mark 
> > phase is done with all threads stopped.
> 
> 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.

Unfortunately, that is not efficient. The state used by the GC is in flux during the sweep phase, so there is no easy lockless way to access that data. Having to acquire a lock on every weakref dereference absolutely kills performance.

I'm definitely open to hearing more ideas on how to efficiently implement. 



More information about the Digitalmars-d mailing list