CriticalSection的ASM原代码

        title   "Critical Section Support"
None.gif;
++
None.gif;
None.gif;  Copyright (c) 
1991  Microsoft Corporation
None.gif;
None.gif;  Module Name:
None.gif;
None.gif;     critsect.asm
None.gif;
None.gif;  Abstract:
None.gif;
None.gif;     This module implements functions to support user mode critical sections.
None.gif;
None.gif;  Author:
None.gif;
None.gif;     Bryan M. Willman (bryanwi) 
2-Oct-91
None.gif;
None.gif;  Environment:
None.gif;
None.gif;     Any mode.
None.gif;
None.gif;  Revision History:
None.gif;
None.gif;
--
None.gif
None.gif.486p
None.gif        .xlist
None.gifinclude ks386.inc
None.gifinclude callconv.inc                    ; calling convention macros
None.gifinclude mac386.inc
None.gif        .list
None.gif
None.gif_DATA   SEGMENT DWORD PUBLIC 
'DATA'
None.gif    
public _LdrpLockPrefixTable
None.gif_LdrpLockPrefixTable    label dword
None.gif        dd offset FLAT:Lock1
None.gif        dd offset FLAT:Lock2
None.gif        dd offset FLAT:Lock3
None.gif        dd offset FLAT:Lock4
None.gif        dd offset FLAT:Lock5
None.gif        dd offset FLAT:Lock6
None.gif        dd offset FLAT:Lock7
None.gif        dd 
0
None.gif_DATA   ENDS
None.gif
None.gif_TEXT   SEGMENT PARA PUBLIC 
'CODE'
None.gif        ASSUME  DS:FLAT, ES:FLAT, SS:NOTHING, FS:NOTHING, GS:NOTHING
None.gif
None.gif        EXTRNP  _RtlpWaitForCriticalSection,
1
None.gif        EXTRNP  _RtlpUnWaitCriticalSection,
1
None.gif
if DEVL
None.gif        EXTRNP  _RtlpNotOwnerCriticalSection,
1
None.gifendif
None.gif
if DBG
None.gif        EXTRNP  _RtlpCriticalSectionIsOwned,
1
None.gifendif
None.gif
None.gifCriticalSection equ     [esp 
+ 4]
None.gif
None.gif        page , 
132
None.gif        subttl  
"RtlEnterCriticalSection"
None.gif
None.gif;
++
None.gif;
None.gif; NTSTATUS
None.gif; RtlEnterCriticalSection(
None.gif;    IN PRTL_CRITICAL_SECTION CriticalSection
None.gif;    )
None.gif;
None.gif; Routine Description:
None.gif;
None.gif;    This function enters a critical section.
None.gif;
None.gif; Arguments:
None.gif;
None.gif;    CriticalSection 
- supplies a pointer to a critical section.
None.gif;
None.gif; Return Value:
None.gif;
None.gif;   STATUS_SUCCESS or raises an exception 
if an error occured.
None.gif;
None.gif;
--
None.gif
None.gif        align   
16
None.gifcPublicProc _RtlEnterCriticalSection,
1
None.gifcPublicFpo 
1,0
None.gif
None.gif        mov     ecx,fs:PcTeb            ; 
get current TEB address
None.gif        mov     edx,CriticalSection     ; 
get address of critical section
None.gif        cmp     CsSpinCount[edx],
0      ; check if spin count is zero
None.gif        jne     
short Ent40             ; if ne, spin count specified
None.gif
None.gif;
None.gif; Attempt to acquire critical section.
None.gif;
None.gif
None.gifLock1:                                  ;
None.gif   
lock inc     dword ptr CsLockCount[edx] ; increment lock count
None.gif        jnz     
short Ent20             ; if nz, already owned
None.gif
None.gif;
None.gif; Set critical section owner and initialize recursion count.
None.gif;
None.gif
None.gifEnt10:
None.gif
if DBG
None.gif        cmp     CsOwningThread[edx],
0
None.gif        je      @F
None.gif        stdCall _RtlpCriticalSectionIsOwned, 
<edx>
None.gif        mov     ecx,fs:PcTeb            ; 
get current TEB address
None.gif        mov     edx,CriticalSection     ; 
get address of critical section
None.gif@@:
None.gifendif ; DBG
None.gif        mov     eax,TbClientId 
+ 4[ecx] ; get current client ID
None.gif        mov     CsOwningThread[edx],eax ; 
set critical section owner
None.gif        mov     dword ptr CsRecursionCount[edx],
1 ; set recursion count
None.gif
None.gif
if DBG
None.gif
None.gif        inc     dword ptr TbCountOfOwnedCriticalSections[ecx] ; increment owned count
None.gif        mov     eax,CsDebugInfo[edx]    ; 
get debug information address
None.gif        inc     dword ptr CsEntryCount[eax] ; increment entry count
None.gif
None.gifendif ; DBG
None.gif
None.gif        xor     eax,eax                 ; 
set success status
None.gif
None.gif        stdRET  _RtlEnterCriticalSection
None.gif
None.gif;
None.gif; The critical section 
is already owned, but may be owned by the current thread.
None.gif;
None.gif
None.gif        align   
16
None.gifEnt20:  mov     eax,TbClientId 
+ 4[ecx] ; get current client ID
None.gif        cmp     CsOwningThread[edx],eax ; check 
if current thread is owner
None.gif        jne     
short Ent30             ; if ne, current thread not owner
None.gif        inc     dword ptr CsRecursionCount[edx] ; increment recursion count
None.gif
None.gif
if DBG
None.gif
None.gif        mov     eax,CsDebugInfo[edx]    ; 
get debug information address
None.gif        inc     dword ptr CsEntryCount[eax] ; increment entry count
None.gif
None.gifendif ; DBG
None.gif
None.gif        xor     eax,eax                 ; 
set success status
None.gif
None.gif        stdRET  _RtlEnterCriticalSection
None.gif
None.gif;
None.gif; The critcal section 
is owned by another thread and the current thread must
None.gif; wait 
for ownership.
None.gif;
None.gif
None.gifEnt30:  stdCall _RtlpWaitForCriticalSection, 
<edx> ; wait for ownership
None.gif        mov     ecx,fs:PcTeb            ; 
get current TEB address
None.gif        mov     edx,CriticalSection     ; 
get address of critical section
None.gif        jmp     Ent10                   ; 
set owner and recursion count
None.gif
None.gif;
None.gif; A nonzero spin count 
is specified.
None.gif;
None.gif
None.gif        align   
16
None.gifEnt40:  mov     eax,TbClientId 
+ 4[ecx] ; get current client ID
None.gif        cmp     CsOwningThread[edx],eax ; check 
if current thread is owner
None.gif        jne     
short Ent50             ; if ne, current thread not owner
None.gif
None.gif;
None.gif; The critical section 
is owned by the current thread. Increment the lock
None.gif; count and the recursion count.
None.gif;
None.gif
None.gifLock6:                                  ;
None.gif   
lock inc     dword ptr CsLockCount[edx] ; increment lock count
None.gif        inc     dword ptr CsRecursionCount[edx] ; increment recursion count
None.gif
None.gif
if DBG
None.gif
None.gif        mov     eax,CsDebugInfo[edx]    ; 
get debug information address
None.gif        inc     dword ptr CsEntryCount[eax] ; increment entry count
None.gif
None.gifendif ; DBG
None.gif
None.gif        xor     eax,eax                 ; 
set success status
None.gif
None.gif        stdRET  _RtlEnterCriticalSection
None.gif
None.gif;
None.gif; A nonzero spin count 
is specified and the current thread is not the owner.
None.gif;
None.gif
None.gif        align   
16
None.gifEnt50:  push    CsSpinCount[edx]        ; 
get spin count value
None.gifEnt60:  mov     eax,
-1                  ; set comparand value
None.gif        mov     ecx,
0                   ; set exchange value
None.gif
None.gifLock7:
None.gif   
lock cmpxchg dword ptr CsLockCount[edx],ecx ; attempt to acquire critical section
None.gif        jnz     
short Ent70             ; if nz, critical section not acquired
None.gif
None.gif;
None.gif; The critical section has been acquired. Set the owning thread and the initial
None.gif; recursion count.
None.gif;
None.gif
None.gif        add     esp,
4                   ; remove spin count from stack
None.gif        mov     ecx,fs:PcTeb            ; 
get current TEB address
None.gif        mov     eax,TbClientId 
+ 4[ecx] ; get current client ID
None.gif        mov     CsOwningThread[edx],eax ; 
set critical section owner
None.gif        mov     dword ptr CsRecursionCount[edx],
1 ; set recursion count
None.gif
None.gif
if DBG
None.gif
None.gif        inc     dword ptr TbCountOfOwnedCriticalSections[ecx] ; increment owned count
None.gif        mov     eax,CsDebugInfo[edx]    ; 
get debug information address
None.gif        inc     dword ptr CsEntryCount[eax] ; increment entry count
None.gif
None.gifendif ; DBG
None.gif
None.gif        xor     eax,eax                 ; 
set success status
None.gif
None.gif        stdRET  _RtlEnterCriticalSection
None.gif
None.gif;
None.gif; The critical section 
is currently owned. Spin until it is either unowned
None.gif; or the spin count has reached zero.
None.gif;
None.gif; If waiters are present, don
't spin on the lock since we will never see it go free
None.gif
;
None.gif
None.gifEnt70:  cmp     CsLockCount[edx],
1      ; check if waiters are present,
None.gif        jge     
short Ent76             ; if ge 1, then do not spin
None.gif
None.gifEnt75:  YIELD
None.gif        cmp     CsLockCount[edx],
-1     ; check if lock is owned
None.gif        je      
short Ent60             ; if e, lock is not owned
None.gif        dec     dword ptr [esp]         ; decrement spin count
None.gif        jnz     
short Ent75             ; if nz, continue spinning
None.gifEnt76:  add     esp,
4                   ; remove spin count from stack
None.gif        mov     ecx,fs:PcTeb            ; 
get current TEB address
None.gif        jmp     Lock1                   ;
None.gif
None.gifstdENDP _RtlEnterCriticalSection
None.gif
None.gif        page , 
132
None.gif        subttl  
"RtlLeaveCriticalSection"
None.gif;
++
None.gif;
None.gif; NTSTATUS
None.gif; RtlLeaveCriticalSection(
None.gif;    IN PRTL_CRITICAL_SECTION CriticalSection
None.gif;    )
None.gif;
None.gif; Routine Description:
None.gif;
None.gif;    This function leaves a critical section.
None.gif;
None.gif; Arguments:
None.gif;
None.gif;    CriticalSection 
- supplies a pointer to a critical section.
None.gif;
None.gif; Return Value:
None.gif;
None.gif;   STATUS_SUCCESS or raises an exception 
if an error occured.
None.gif;
None.gif;
--
None.gif
None.gif        align   
16
None.gifcPublicProc _RtlLeaveCriticalSection,
1
None.gifcPublicFpo 
1,0
None.gif
None.gif        mov     edx,CriticalSection
None.gif
if DBG
None.gif        mov     ecx,fs:PcTeb                ; (ecx) 
== NtCurrentTeb()
None.gif        mov     eax,TbClientId
+4[ecx]       ; (eax) == NtCurrentTeb()->ClientId.UniqueThread
None.gif        cmp     eax,CsOwningThread[edx]
None.gif        je      @F
None.gif        stdCall _RtlpNotOwnerCriticalSection, 
<edx>
None.gif        mov     eax,STATUS_INVALID_OWNER
None.gif        stdRET  _RtlLeaveCriticalSection
None.gif@@:
None.gifendif ; DBG
None.gif        xor     eax,eax                     ; Assume STATUS_SUCCESS
None.gif        dec     dword ptr CsRecursionCount[edx]
None.gif        jnz     leave_recurs                ; skip 
if only leaving recursion
None.gif
None.gif        mov     CsOwningThread[edx],eax     ; clear owning thread id
None.gif
None.gif
if DBG
None.gif        mov     ecx,fs:PcTeb                ; (ecx) 
== NtCurrentTeb()
None.gif        dec     dword ptr TbCountOfOwnedCriticalSections[ecx]
None.gifendif ; DBG
None.gif
None.gifLock2:
None.gif   
lock dec     dword ptr CsLockCount[edx]  ; interlocked dec of
None.gif                                            ; dot.gif CriticalSection
->LockCount
None.gif        jge     @F
None.gif        stdRET  _RtlLeaveCriticalSection
None.gif
None.gif@@:
None.gif        stdCall _RtlpUnWaitCriticalSection, 
<edx>
None.gif        xor     eax,eax                     ; 
return STATUS_SUCCESS
None.gif        stdRET  _RtlLeaveCriticalSection
None.gif
None.gif        align   
16
None.gifleave_recurs:
None.gifLock3:
None.gif   
lock dec     dword ptr CsLockCount[edx]  ; interlocked dec of
None.gif                                            ; dot.gif CriticalSection
->LockCount
None.gif        stdRET  _RtlLeaveCriticalSection
None.gif
None.gif_RtlLeaveCriticalSection    endp
None.gif
None.gif        page    ,
132
None.gif        subttl  
"RtlTryEnterCriticalSection"
None.gif;
++
None.gif;
None.gif; BOOL
None.gif; RtlTryEnterCriticalSection(
None.gif;    IN PRTL_CRITICAL_SECTION CriticalSection
None.gif;    )
None.gif;
None.gif; Routine Description:
None.gif;
None.gif;    This function attempts to enter a critical section without blocking.
None.gif;
None.gif; Arguments:
None.gif;
None.gif;    CriticalSection (a0) 
- Supplies a pointer to a critical section.
None.gif;
None.gif; Return Value:
None.gif;
None.gif;    If the critical section was successfully entered, then a value of TRUE
None.gif;    
is returned as the function value. Otherwise, a value of FALSE is returned.
None.gif;
None.gif;
--
None.gif
None.gifCriticalSection equ     [esp 
+ 4]
None.gif
None.gifcPublicProc _RtlTryEnterCriticalSection,
1
None.gifcPublicFpo 
1,0
None.gif
None.gif        mov     ecx,CriticalSection         ; interlocked inc of
None.gif        mov     eax, 
-1                     ; set value to compare against
None.gif        mov     edx, 
0                      ; set value to set
None.gifLock4:
None.gif   
lock cmpxchg dword ptr CsLockCount[ecx],edx  ; Attempt to acquire critsect
None.gif        jnz     
short tec10                 ; if nz, critsect already owned
None.gif
None.gif        mov     eax,fs:TbClientId
+4         ; (eax) == NtCurrentTeb()->ClientId.UniqueThread
None.gif        mov     CsOwningThread[ecx],eax
None.gif        mov     dword ptr CsRecursionCount[ecx],
1
None.gif
None.gif
if DBG
None.gif        mov     eax,fs:PcTeb                ; (ecx) 
== NtCurrentTeb()
None.gif        inc     dword ptr TbCountOfOwnedCriticalSections[eax]
None.gifendif ; DBG
None.gif
None.gif        mov     eax, 
1                      ; set successful status
None.gif
None.gif        stdRET  _RtlTryEnterCriticalSection
None.gif
None.giftec10:
None.gif;
None.gif; The critical section 
is already owned. If it is owned by another thread,
None.gif
return FALSE immediately. If it is owned by this thread, we must increment
None.gif; the 
lock count here.
None.gif;
None.gif        mov     eax, fs:TbClientId
+4        ; (eax) == NtCurrentTeb()->ClientId.UniqueThread
None.gif        cmp     CsOwningThread[ecx], eax
None.gif        jz      tec20                       ; 
if eq, this thread is already the owner
None.gif        xor     eax, eax                    ; 
set failure status
None.gif        YIELD
None.gif        stdRET  _RtlTryEnterCriticalSection
None.gif
None.giftec20:
None.gif;
None.gif; This thread 
is already the owner of the critical section. Perform an atomic
None.gif; increment of the LockCount and a normal increment of the RecursionCount and
None.gif
return success.
None.gif;
None.gifLock5:
None.gif   
lock inc     dword ptr CsLockCount[ecx]
None.gif        inc     dword ptr CsRecursionCount[ecx]
None.gif        mov     eax, 
1
None.gif        stdRET  _RtlTryEnterCriticalSection
None.gif
None.gifstdENDP _RtlTryEnterCriticalSection
None.gif
None.gif
None.gif_TEXT   ends
None.gif        end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值