Snippet: Double-Word Compare-And-Swap in the D language

Update: My changes to DMD have been pulled in. DMD's inline assembler now supports cmpxchg16b.

 

I'm currently working on a bunch of lock-free data structures including a lock-free FIFO queue which is primarily used for the event queue.

The implementation of such lock-free structures, however, is plagued by a major obstacle: The risk of ABA occurrence, see http://en.wikipedia.org/wiki/ABA_problem

One approach to overcome this problem is described in "An optimistic approach to lock-free FIFO queues" using tags being incremented together with the node pointer being exchanged (atomically!) - see http://people.csail.mit.edu/edya/publications/OptimisticFIFOQueue-journal.pdf

This approach however requires the cmpxchg16b CPU instruction on x86-64, respectively cmpxchg8b on x86. The availability of these instructions is indicated by the flag CX8/16 in /proc/cpuinfo.

Since neither GDC nor DMD implement the cmpxchg16b instruction in their inline assembler yet, I had to add it on my own. Just checkout my changes to GDC at https://bitbucket.org/nischu7/gdc/changeset/d8a2a73fb3d8.

As soon as you've rebuilt GDC you'll be able to compile this function: