现在的位置: 首页 > 综合 > 正文

自旋锁试验

2011年05月17日 ⁄ 综合 ⁄ 共 724字 ⁄ 字号 评论关闭
#include <windows.h>
#include <stdio.h>

typedef ULONG_PTR KSPIN_LOCK, *PKSPIN_LOCK;

VOID WINAPI
KeInitializeSpinLock(
					 IN PKSPIN_LOCK  SpinLock
					 )
{
	*SpinLock = 0;
}

__declspec( naked ) 
VOID WINAPI
KeAcquireSpinLockAtDpcLevel(
							IN PKSPIN_LOCK  SpinLock
							)
{
	//BTS -- Bit Test and Set

	//BTS saves the value of the bit indicated by the base (first operand) and 
	//the bit offset (second operand) into the carry flag and then stores 1 in the bit. 


	// JB rel8           7+m,3    Jump short if below (CF=1)
	__asm
	{
		mov ecx, [esp+4]
l1:
		lock bts dword ptr [ecx], 0//测试第0BIT
		jb short l2
		ret 4
l2:
		test    dword ptr [ecx], 1
		jz      short l1
		pause
		jmp     short l2
	}
}

VOID 
KeReleaseSpinLockFromDpcLevel(
							  IN PKSPIN_LOCK  SpinLock
							  )
{
	*SpinLock = 0;
}
int main()
{
	KSPIN_LOCK lock;
	KeInitializeSpinLock(&lock);

	KeAcquireSpinLockAtDpcLevel(&lock);
	KeReleaseSpinLockFromDpcLevel(&lock);

	return 0;
}

抱歉!评论已关闭.