Test and Set
BTS 指令,一般用在自旋锁上或者类似概念。自旋锁核心操作就是InterlockedBitTestAndSet。
InterlockedBitTestAndSet有两种实现:
1. ReactOS方法
static __inline__ BOOLEAN InterlockedBitTestAndSet(IN LONG volatile *Base, IN LONG Bit) { LONG OldBit; __asm__ __volatile__("lock " // 总线加锁 "btsl %2,%1/n/t" "sbbl %0,%0/n/t" :"=r" (OldBit),"=m" (*Base) :"Ir" (Bit) : "memory"); return OldBit; }
2. Windows方法
BOOLEAN bRet = InterlockedBitTestAndSet(&num,3);
0042F9D5 lea eax,[num]
0042F9D8 lock bts dword ptr [eax],3
0042F9DD setb cl
0042F9E0 mov byte ptr [bRet],cl