How do I steal 2 MSBs from an address to do an atomic operation? I'm trying to do a single word CAS
An example
public class Node
{
long key;
long value;
Node lchild; // format is flag1,flag2,address
Node rchild; // format is flag1,flag2,address
}
public void createNode()
{
Node n1 = new Node(); //this should create a node with format 0,0,address1
}
public void setFlag1(Node n1)
{
Now the new address should be in format 1,0,address1
}
public void setFlag2(Node n1)
{
Now the new address should be in format 0,1,address1
}
AtomicReference
could be used if I needed only one extra flag.
AtomicStampedReference
could be used but it is not efficient as it creates an extra box containing timeStamp and a reference.
A similar problem in C is discussed in stealing bits from a pointer
The best suggestion I can give you for working in Java would be to do your bit-twiddling on array indices rather than addresses, since Java does not expose addresses.