Created
May 2, 2011 20:04
-
-
Save jnschulze/952253 to your computer and use it in GitHub Desktop.
double-word compare-and-swap function using cmpxchg16b (D language)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// by Niklas Schulze / nischu7 <[email protected]> | |
// | |
// 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