#include-once ; #INDEX# ======================================================================================================================= ; Title .........: Semaphore UDF ; AutoIt Version : 3.3.6.1 ; Description ...: Kernel libraries calls that have been translated to AutoIt functions, in order to manage semaphores and mutex. ; Author(s) .....: Aurélien A. (ZDS) ; Dll ...........: kernel32.dll ; =============================================================================================================================== ; #CURRENT# ===================================================================================================================== ; _Semaphore_Init ; _Semaphore_MutexInit ; _Semaphore_BlockerInit ; _Semaphore_P ; _Semaphore_V ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _Semaphore_Init ; Description ...: Create a defined semaphore and return the SemID (or get the SemID of an existing semaphore) ; Syntax.........: _Semaphore_Init($nom, $valeur, $valeurMax) ; Parameters ....: $nom - Name of the semaphore. ; $valeur - Value to set in the semaphore counter. ; + $valeur must be is greater than or equal to 0. ; $valeurMax - Value to set as the maximum value for the semaphore counter. ; + $valeurMax must be is greater than or equal to $valeur, and greater than 0. ; Return values .: Success - Handle to the semaphore ; Failure - 0 and @error is set ; Author ........: Aurélien A. (ZDS) ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: @@MsdnLink@@ CreateSemaphoreW ; Example .......: ; =============================================================================================================================== Func _Semaphore_Init($nom, $valeur, $valeurMax) If $valeur>$valeurMax Then SetError(1, 0, 0) Local $resultat = DllCall("kernel32.dll", "ptr", "CreateSemaphoreW", _ "ptr", DllStructGetPtr(0), _ "int", $valeur, _ "int", $valeurMax, _ "wstr", $nom _ ) If @error Then Return SetError(1, 0, 0) $resultat = $resultat[0] If Not $resultat Then Return SetError(1, 0, 0) Return $resultat EndFunc ;==>_Semaphore_Init ; #FUNCTION# ==================================================================================================================== ; Name...........: _Semaphore_MutexInit ; Description ...: Create a defined mutex and return the SemID (or get the SemID of an existing mutex) ; Syntax.........: _Semaphore_MutexInit($nom) ; Parameters ....: $nom - Name of the mutex ; Return values .: Success - Handle to the mutex ; Failure - 0 and @error is set ; Author ........: Aurélien A. (ZDS) ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: @@MsdnLink@@ CreateSemaphoreW ; Example .......: ; =============================================================================================================================== Func _Semaphore_MutexInit($nom) Local $resultat = _Semaphore_Init($nom,1,1) If @error Then Return SetError(1, 0, 0) Return $resultat EndFunc ;==>_Semaphore_MutexInit ; #FUNCTION# ==================================================================================================================== ; Name...........: _Semaphore_BlockerInit ; Description ...: Create a defined blocking mutex and return the SemID (or get the SemID of an existing blocking mutex) ; Syntax.........: _Semaphore_BlockerInit($nom) ; Parameters ....: $nom - Name of the blocking mutex ; Return values .: Success - Handle to the blocking mutex ; Failure - 0 and @error is set ; Author ........: Aurélien A. (ZDS) ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: @@MsdnLink@@ CreateSemaphoreW ; Example .......: ; =============================================================================================================================== Func _Semaphore_BlockerInit($nom) Local $resultat = _Semaphore_Init($nom,0,1) If @error Then Return SetError(1, 0, 0) Return $resultat EndFunc ;==>_Semaphore_BlockerInit ; #FUNCTION# ==================================================================================================================== ; Name...........: _Semaphore_P ; Description ...: Call a semaphore with a P operation ("take"/"wait") ; Syntax.........: _Semaphore_P($semaphore) ; Parameters ....: $semaphore - Handle to the semaphore ; Return values .: Success - Not 0 ; Failure - 0 and @error is set ; Author ........: Aurélien A. (ZDS) ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: @@MsdnLink@@ WaitForSingleObject ; Example .......: ; =============================================================================================================================== Func _Semaphore_P($semaphore) Local $resultat = DllCall("kernel32.dll", "int", "WaitForSingleObject", _ "handle", $semaphore, _ "dword", -1 _ ) If @error Then Return SetError(1, 0, 0) $resultat = $resultat[0] Return $resultat EndFunc ;==>_Semaphore_P ; #FUNCTION# ==================================================================================================================== ; Name...........: _Semaphore_V ; Description ...: Call a semaphore with a V operation ("release"/"signal") ; Syntax.........: _Semaphore_V($semaphore) ; Parameters ....: $semaphore - Handle to the semaphore ; Return values .: Success - Not 0 ; Failure - 0 and @error is set ; Author ........: Aurélien A. (ZDS) ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: @@MsdnLink@@ ReleaseSemaphore ; Example .......: ; =============================================================================================================================== Func _Semaphore_V($semaphore) Local $resultat = DllCall("kernel32.dll", "int", "ReleaseSemaphore", _ "ptr", $semaphore, _ "int", 1, _ "int*", 0 _ ) If @error Then Return SetError(1, 0, 0) $resultat = $resultat[0] If Not $resultat Then Return SetError(1, 0, 0) Return $resultat EndFunc ;==>_Semaphore_V