linux _InterlockedCompareExchange128的实现

#include <stdint.h>

namespace types
{
struct uint128_t
{
uint64_t lo
;
uint64_t hi
;
}
__attribute__
(( __aligned__( 16 ) ));
}

template< class T > inline bool cas( volatile T * src, T cmp, T with );

template<> inline bool cas( volatile types::uint128_t * src, types::uint128_t cmp, types::uint128_t with )
{
bool result;
__asm__ __volatile__
(
"lock cmpxchg16b  %1\n\t"
"setz %0"
: "=q" ( result )
, "+m" ( *src )
, "+d" ( cmp.hi )
, "+a" ( cmp.lo )
: "c" ( with.hi )
, "b" ( with.lo )
: "cc"
);
return result;
}

int main()
{
using namespace types;
uint128_t test
= { 0xdecafbad, 0xfeedbeef };
uint128_t cmp
= test;
uint128_t with
= { 0x55555555, 0xaaaaaaaa };
return ! cas( & test, cmp, with );
}
posted @ 2012-04-01 17:55  Lunaa  阅读(336)  评论(0编辑  收藏  举报