draft proposal for ref counting in D

Rainer Schuetze r.sagitario at gmx.de
Fri Oct 11 23:16:17 PDT 2013



On 12.10.2013 04:16, inout wrote:
> On Thursday, 10 October 2013 at 08:55:00 UTC, Robert Schadek
> wrote:
>> On 10/10/2013 03:45 AM, Walter Bright wrote:
>>> Rainer Schuetze wrote:
>>>
>>> You have to put the lock around the pair of AddRef and Release, but if
>>> the compiler already splits this into two function calls, this cannot
>>> be done in the implementation.
>> I would imagine the counter to be manipulated with atomic_add_and_fetch
>> operations, so no locks are required.
>
> On shared objects, yes. Local objects need no atomics at all.

Atomic increments/decrements are not good enough for shared references. 
See this example from later in the discussion:

Consider a global shared reference R that holds the last reference to an 
object O. One thread exchanges the reference with another reference P 
while another thread reads the reference into S.

shared(C) R = O;      ; refcnt of O is 1

in pseudo-assembly missing null-checks:

Thread1 (R = P)        Thread2 (S = R)

                        mov ecx,[R]
                        ; thread suspended
mov eax,[P]
inc [eax].refcnt
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.


More information about the Digitalmars-d mailing list