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

转载于:https://www.cnblogs.com/fanzi2009/archive/2012/04/12/2444279.html

你可能感兴趣的:(Test and Set)