;~ __Struct_CriticalSection ;~ __InitializeCriticalSection ;~ __EnterCriticalSection ;~ __TryEnterCriticalSection ;~ __LeaveCriticalSection ;~ __DeleteCriticalSection ;~ __InitializeCriticalSectionAndSpinCount ;~ __SetCriticalSectionSpinCount #include-once ; #FUNCTION# ============================================================================= ; Name...........: __Struct_CriticalSection ; ======================================================================================== Func __Struct_CriticalSection($pCriticalSection = Null) Local Const $typedef_CriticalSection = "PTR DebugInfo;LONG LockCount;LONG RecursionCount;handle OwningThread;handle LockSemaphore;DWORD SpinCount" If IsPtr($pCriticalSection) Then Return DllStructCreate($typedef_CriticalSection, $pCriticalSection) Return DllStructCreate($typedef_CriticalSection) ;~ Return value: None EndFunc ; #FUNCTION# ============================================================================= ; Name...........: __InitializeCriticalSection ; Initializes a critical section object. ; @jugador ; ======================================================================================== Func __InitializeCriticalSection($lpCriticalSection) If Not IsDllStruct($lpCriticalSection) Then Return SetError(-1, 0, 0) DllCall("kernel32.dll", "none", "InitializeCriticalSection", "struct*", ($lpCriticalSection)) If @error Then Return SetError(-2, 0, 0) EndFunc ; #FUNCTION# ============================================================================= ; Name...........: __EnterCriticalSection ; Waits for ownership of the specified critical section object. The function returns when the calling thread is granted ownership. ; ======================================================================================== Func __EnterCriticalSection($lpCriticalSection) DllCall("kernel32.dll", "none", "EnterCriticalSection", "ptr", $lpCriticalSection) If @error Then Return SetError(-1, 0, 0) EndFunc ; #FUNCTION# ============================================================================= ; Name...........: __TryEnterCriticalSection ; Attempts to enter a critical section without blocking. If the call is successful, the calling thread takes ownership of the critical section. ; ======================================================================================== Func __TryEnterCriticalSection($lpCriticalSection) Local $aCall = DllCall("kernel32.dll", "BOOL", "TryEnterCriticalSection", "ptr", $lpCriticalSection) If @error Then Return SetError(-1, 0, 0) Return $aCall[0] ;~ If another thread already owns the critical section, the return value is zero else nonzero. EndFunc ; #FUNCTION# ============================================================================= ; Name...........: __LeaveCriticalSection ; To release its ownership, the thread must call LeaveCriticalSection once for each time that it entered the critical section. ; ======================================================================================== Func __LeaveCriticalSection($lpCriticalSection) DllCall("kernel32.dll", "none", "LeaveCriticalSection", "ptr", $lpCriticalSection) If @error Then Return SetError(-1, 0, 0) EndFunc ; #FUNCTION# ============================================================================= ; Name...........: __DeleteCriticalSection ; Releases all resources used by an unowned critical section object. {The object that is initialized with the InitializeCriticalSection} ; ======================================================================================== Func __DeleteCriticalSection($lpCriticalSection) DllCall("kernel32.dll", "none", "DeleteCriticalSection", "ptr", $lpCriticalSection) If @error Then Return SetError(-1, 0, 0) EndFunc ; #FUNCTION# ============================================================================= ; Name...........: __InitializeCriticalSectionAndSpinCount ; Initializes a critical section object and sets the spin count for the critical section. ; When a thread tries to acquire a critical section that is locked, the thread spins: it enters a loop which iterates spin count times, ; checking to see if the lock is released. If the lock is not released before the loop finishes, ; the thread goes to sleep to wait for the lock to be released. ; ======================================================================================== Func __InitializeCriticalSectionAndSpinCount($lpCriticalSection, $dwSpinCount = 0) Local $aCall = DllCall("kernel32.dll", "BOOL", "InitializeCriticalSectionAndSpinCount", "ptr", $lpCriticalSection, "DWORD", $dwSpinCount) If @error Then Return SetError(-1, 0, 0) Return $aCall[0] ;~ This function always succeeds and returns a nonzero value. EndFunc ; #FUNCTION# ============================================================================= ; Name...........: __SetCriticalSectionSpinCount ; Sets the spin count for the specified critical section. Spinning means that when a thread tries to acquire a critical section that is locked, ; the thread enters a loop, checks to see if the lock is released, and if the lock is not released, the thread goes to sleep. ; ======================================================================================== Func __SetCriticalSectionSpinCount($lpCriticalSection, $dwSpinCount = 0) Local $aCall = DllCall("kernel32.dll", "DWORD", "SetCriticalSectionSpinCount", "ptr", $lpCriticalSection, "DWORD", $dwSpinCount) If @error Then Return SetError(-1, 0, 0) Return $aCall[0] ;~ The function returns the previous spin count for the critical section. EndFunc