draft proposal for ref counting in D

Rainer Schuetze r.sagitario at gmx.de
Sun Oct 13 00:03:31 PDT 2013



On 12.10.2013 20:31, deadalnix wrote:
> On Saturday, 12 October 2013 at 06:16:24 UTC, Rainer Schuetze wrote:
>> in pseudo-assembly missing null-checks:
>>
>> Thread1 (R = P)        Thread2 (S = R)
>>
>>                        mov ecx,[R]
>>                        ; thread suspended
>
> You need an sequentially consistent write. You also need to increment
> the refcount BEFORE ! This codegen is incorrect.

How do you increment the counter without reading its address?

>
>> mov eax,[P]
>> inc [eax].refcnt
>
> Same here.

Same here ;-)

>
>> mov ebx,[R]
>> mov [R],eax
>> dec [ebx].refcnt      ; refcnt of O now 0
>> jnz done
>> call delete_ebx
>>                        ; thread resumed
>>                        inc [ecx].refcnt
>> done:
>>
>> The increment on [ecx].refcnt modifies garbage.
>
> This can be done atomically (even with eventually consistent increment,
> don't need full sequential consistency here, you still need fully
> sequential consistent decrement).

According to the "Handbook of Garbage Collection" by Richard Jones eager 
lock-free reference counting can only be done with a cas2 operation 
modifying two seperate locations atomically (algorithm 18.2 "Eager 
reference counting with CompareAndSwap is broken"). This might be the 
quoted paper: http://scholr.ly/paper/2199608/lock-free-reference-counting

Unfortunately the CAS2 operation does not exist in most processors.


More information about the Digitalmars-d mailing list