D2 weak references

Jason House jason.james.house at gmail.com
Sat Apr 25 15:32:02 PDT 2009


Leandro Lucarella Wrote:

> Jason House, el 25 de abril a las 14:19 me escribiste:
> > > Is that I guess is very likely that 'tmp' get optimized out. Would
> > > declaring it as volatile help? If not, I guess the only way to avoid that
> > > race is add a root manually to the GC (or mark the weakref again as no
> > > NO_SCAN). But both requires synchronization. =(
> > 
> > I would use tango.core.atomic that I took from the Tango D2 branch.
> > Another option is inline asm, but I'd have to research how to do that.
> 
> I don't know how an atomic can help, I don't know much about atomics but
> I think they don't have to be pointer sized (different architectures
> provide different atomic types). And in any case you don't want tmp to be
> atomic, you just don't want it to be optimized out, I think..

Not tmp, the hidden pointer t. The read calls can't be optimized away, even with inlining because they use asm code.


> I guess asm can never get optimized (right?) so I think that should do.

Right. D2 treats all asm as volatile asm.
 
> BTW, that's wrong with volatile? I think it should work too.

The volatile keyword does not exist in D2. Declaring the t (reference) as volatile would work if it existed in D2.
 
> > > > You're not considering what can happen if the weak ref dereferencing
> > > > thread stops and the rest of the world wreaks havoc on the state of the
> > > > world. A collection could run after checking the mark. If one is careful
> > > > with that, and a copy of t is kept on the stack, will the optimizer
> > > > reuse it? Or will it reread the value? That matters if the finalizer
> > > > runs. The mark bit check can fail from reallocation after deletion.
> > > > 
> > > > I hope that helps...
> > > 
> > > Yes, thank you.
> > 
> > It's amazingly difficult to get right for something so "simple"
> 
> Indeed.
> 
> -- 
> Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
> ----------------------------------------------------------------------------
> GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
> ----------------------------------------------------------------------------
> Wake from your sleep,
> the drying of your tears,
> Today we escape, we escape.




More information about the Digitalmars-d mailing list