Hazard pointers needed with GC ?

Martin Nowak dawg at dawgfoto.de
Wed Dec 7 17:41:21 PST 2011


On Wed, 07 Dec 2011 23:23:26 +0100, Walter Bright  
<newshound2 at digitalmars.com> wrote:

> On 12/7/2011 9:02 AM, Martin Nowak wrote:
>> I implemented a lock-free doubly linked list some time ago.
>> I omitted the use of hazard lists because flagging the lowest
>> bit would still make a valid pointer into the list node.
>> Afterwards I found that  
>> http://www.d-programming-language.org/garbage.html
>> explicitly states:
>> p = cast(void*)(cast(int)p | 1); // error: undefined behavior
>>
>> Is this really needed? Guess the current GC would work properly.
>>
>> Also if a list node were
>> Node(T)
>> {
>> ubyte[2] data;
>> T t;
>> }
>> Than both a pointer to data[0] as well as one to data[1] are valid
>> and effectively hold the node.
>
> If the GC runs a collection cycle while the pointer has that bit set,  
> and setting the bit causes it to point past the allocated data, then it  
> will not regard that data as being in use and will delete it.
>
> An easy way to make it work right is to make sure that or'ing in the bit  
> will not cause the pointer to point past the object.

Nice, this really simplifies lock-free data structures.


More information about the Digitalmars-d mailing list