自旋锁试验

#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;
}
posted @ 2010-06-24 13:29  Fan Zhang  阅读(403)  评论(0编辑  收藏  举报