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