D2 weak references

Sean Kelly sean at invisibleduck.org
Mon Apr 20 10:07:57 PDT 2009


Daniel Keep wrote:
> 
> Sean Kelly wrote:
>> Jason House wrote:
>>> Jason House wrote:
>>>
>>>> Tango's GC (and therefore
>>>> druntime's GC) has an explicit notification mechanism.
>>> I guess I shouldn't have assumed that the features of tango became
>>> part of druntime.  I don't see any notification mechanism :(
>> Same as Tango:
>>
>> alias void delegate(Object) DEvent;
>> extern (C) void rt_attachDisposeEvent(Object h, DEvent e);
>> extern (C) void rt_detachDisposeEvent(Object h, DEvent e);
>>
>> I should probably expose these in core.runtime.
> 
> How are these events dispatched?  I remember a discussion about race
> conditions if these events were fired off after all threads had been
> resumed; you could have the event zero out the weak ref AFTER something
> else had dereferenced it.

It's the responsibility of the person making the weak ref to make it 
thread-safe if that's a design consideration.  If the cleanup is 
performed before the threads are restarted and the weak ref (or 
signal/slot mechanism) uses mutexes then a deadlock is possible.  At 
least this approach provides the option for correct code to actually work.

> If that's still a possibility, maybe you could add a case where if the
> delegate's funcptr is null, it just assumes the context pointer points
> to a void*.sizeof word and zeroes it out.  You could do that without
> resuming threads.

This is all tied into the finalizer.  To do what you suggest the GC 
would have to perform two sweeps--one before resuming threads and one after.



More information about the Digitalmars-d mailing list