[Proposal] Weak reference implementation for D
Denis Shelomovskij
verylonglogin.reg at gmail.com
Mon Oct 14 04:44:33 PDT 2013
14.10.2013 13:04, robert пишет:
>
> Why would I be angry with a stranger who insults me in public? I
> don't understand your concerns.
No insults assumed! Just ugly truth about all of us. )
> If you are more
> experienced in this area I am glad if you share your insights and
Walter and Andrei often do silly mistakes. Can we suppose we are more
experienced than they? Of course no, they just don't have enough time to
think and check. Here the situation can be the same. I probably just
have enough time to investigate the problem and solve it.
> if you think something is wrong, I am glad to discuss it and fix
> it if you are right, but just saying your implementation is
> wrong, does not really help. It implies that you are obviously
> right and everyone who does not see this is obviously a moron, if
> someone has a bit of a problem with his ego, I don't think it is
> me.
Easy, man. I have never met morons here, except, probably, myself.
Concurrent programming is fun so I just don't want to spoil the pleasure
of investigation. And yes, I'm also lazy. )
Now about your code. First, I was completely incorrect about it, sorry
for that. My mistake. I didn't even think the code containing such loop
can be "so much correct". But, and this is the second, the code can't be
"more or less" correct. It is either correct or not correct. It
remembers me our (Russian) recent Moscow mayoral elections when we tried
to change something in our country (we failed, yes) and after government
won officials said: "It was the most honest elections of all preceding." )
So you code is incorrect and lets show it. When you give your code for
eating to the compiler, it can does whatever it want but guarantee your
program will work as you have written it (except special cases like copy
construction elimination) and it doesn't assume every variable can be
accessed from other threads. E.g. here is you code with unwinded loop in
SSA (static single assignment) form:
```
auto tmp1 = atomicLoad(_obj);
void* o1 = tmp1.address;
if(o1 is null) return null;
void* o2 = GC.addrOf(tmp1.address);
auto tmp2 = atomicLoad(_obj);
void* o3 = tmp2.address;
if(o3 is null) return null;
void* o4 = GC.addrOf(tmp2.address);
if(o4) return cast(Object) o4;
return null;
```
`o1` is only used once and `o2` is never used so the compiler is free to
discard the former and ignore the latter. So your code equals to this code:
```
auto tmp1 = atomicLoad(_obj);
if(tmp1.address is null) return null;
GC.addrOf(tmp1.address);
auto tmp2 = atomicLoad(_obj);
if(tmp2.address is null) return null;
void* o4 = GC.addrOf(tmp2.address);
if(o4) return cast(Object) o4;
return null;
```
So the second iteration gives nothing except decreasing (squaring to be
precise) a variety of undesired threads execution order.
--
Денис В. Шеломовский
Denis V. Shelomovskij
More information about the Digitalmars-d
mailing list