FastMutex 의 동작방식


FastMutex 는 초기화 시에 count 값이 1으로 설정되고 동기화를 위한 동기 Event 하나를 생성한다. Acuier 하게 되면 Count 값이 1 감소하고 이 값이 0 이면 Mutex 를 얻게되고 0 이 아닐 경우 Wait 에서 대기하게 된다. Release 에서는 Count 값을 1 증가 시킨다.
typedef struct _FAST_MUTEX {

#define FM_LOCK_BIT          0x1 // Actual lock bit, 1 = Unlocked, 0 = Locked
#define FM_LOCK_BIT_V        0x0 // Lock bit as a bit number
#define FM_LOCK_WAITER_WOKEN 0x2 // A single waiter has been woken to acquire this lock
#define FM_LOCK_WAITER_INC   0x4 // Increment value to change the waiters count

    volatile LONG Count;
    PKTHREAD Owner;
    ULONG Contention;
    KEVENT Gate;
    ULONG OldIrql;
} FAST_MUTEX, *PFAST_MUTEX;

VOID
FORCEINLINE
ExInitializeFastMutex (
    __out PFAST_MUTEX FastMutex
    )
{
FastMutex->Count = FM_LOCK_BIT;
    FastMutex->Owner = NULL;
    FastMutex->Contention = 0;
KeInitializeEvent(&FastMutex->Gate, SynchronizationEvent, FALSE);
    return;
}
3: kd> uf ExAcquireFastMutex
hal!ExAcquireFastMutex [..\..\halmps\i386\mpspin.asm @ 401]:
  401 80065600 a18000feff      mov     eax,dword ptr ds:[FFFE0080h]
  404 80065605 c7058000feff3d000000 mov dword ptr ds:[0FFFE0080h],3Dh
  406 8006560f f0ff09         lock dec dword ptr [ecx]
  407 80065612 7419           je      hal!ExAcquireFastMutex+0x2d (8006562d)

hal!ExAcquireFastMutex+0x14 [..\..\halmps\i386\mpspin.asm @ 409]:
  409 80065614 ff4108          inc     dword ptr [ecx+8]
  412 80065617 51              push    ecx
  413 80065618 50              push    eax
  414 80065619 83c10c          add     ecx,0Ch
  415 8006561c 6a00            push    0
  415 8006561e 6a00            push    0
  415 80065620 6a00            push    0
  415 80065622 6a00            push    0
  415 80065624 51              push    ecx
  415 80065625 ff1574230680    call    dword ptr [hal!_imp__KeWaitForSingleObject (80062374)]
  416 8006562b 58              pop     eax
  417 8006562c 59              pop     ecx

hal!ExAcquireFastMutex+0x2d [..\..\halmps\i386\mpspin.asm @ 421]:
  421 8006562d 88411c          mov     byte ptr [ecx+1Ch],al
  430 80065630 896104          mov     dword ptr [ecx+4],esp
  431 80065633 c3              ret
3: kd> uf ExReleaseFastMutex
hal!ExReleaseFastMutex [..\..\halmps\i386\mpspin.asm @ 458]:
  458 80065634 33c0            xor     eax,eax
  461 80065636 8a411c          mov     al,byte ptr [ecx+1Ch]
  463 80065639 f0830101        lock add dword ptr [ecx],1
  464 8006563d 7802            js      hal!ExReleaseFastMutex+0xd (80065641)

hal!ExReleaseFastMutex+0xb [..\..\halmps\i386\mpspin.asm @ 465]:
  465 8006563f 750e            jne     hal!ExReleaseFastMutex+0x1b (8006564f)

hal!ExReleaseFastMutex+0xd [..\..\halmps\i386\mpspin.asm @ 468]:
  468 80065641 83c10c          add     ecx,0Ch
  469 80065644 50              push    eax
  470 80065645 6a00            push    0
  470 80065647 51              push    ecx
  470 80065648 ff1570230680    call    dword ptr [hal!_imp__KeSetEventBoostPriority (80062370)]
  471 8006564e 58              pop     eax

hal!ExReleaseFastMutex+0x1b [..\..\halmps\i386\mpspin.asm @ 474]:
  474 8006564f a38000feff      mov     dword ptr ds:[FFFE0080h],eax
  475 80065654 8b0d8000feff    mov     ecx,dword ptr ds:[0FFFE0080h]
  482 8006565a c3              ret


Skip to main content