Skip to content

Instantly share code, notes, and snippets.

@jnschulze
Created May 2, 2011 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnschulze/952253 to your computer and use it in GitHub Desktop.
Save jnschulze/952253 to your computer and use it in GitHub Desktop.
double-word compare-and-swap function using cmpxchg16b (D language)
// by Niklas Schulze / nischu7 <n7@pronghorn.org>
//
// requires cmpxchg16b CPU instruction
// required GDC patch: http://bitbucket.org/nischu7/gdc/changeset/d8a2a73fb3d8
bool dwcas16(T, V1, V2)(T* here, const V1 ifThis, const V2 writeThis)
{
asm
{
/*
mov RAX, ifThis; // copy ifThis[0] to RAX
mov RDX, 8[ifThis]; // copy ifThis[1] to RDX
mov RBX, writeThis; // copy writeThis[0] to RBX
mov RCX, 8[writeThis]; // copy writeThis[1] to RCX
*/
// slightly faster?
lea RSI, ifThis;
mov RAX, [RSI];
mov RDX, 8[RSI];
lea RSI, writeThis;
mov RBX, [RSI];
mov RCX, 8[RSI];
mov RSI, here;
lock; // lock always needed to make this op atomic
cmpxchg16b [RSI];
setz AL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment