mirror of
https://github.com/aziascreations/BakaShrot.git
synced 2026-11-23 20:58:46 +01:00
42bd1f76d9
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
87 lines
2.5 KiB
Plaintext
87 lines
2.5 KiB
Plaintext
;{- Code Header
|
|
; ==- Basic Info -================================
|
|
; Name: Signal.pbi
|
|
; Version: 0.0.1
|
|
; Author: Herwin Bozet (NibblePoker)
|
|
;
|
|
; ==- Compatibility -=============================
|
|
; Tested compiler versions:
|
|
; * PureBasic 5.73 LTS (Windows x64, Linux x64)
|
|
; * PureBasic 6.40 C Backend (Windows x64)
|
|
; * PureBasic 6.41 (Linux x64)
|
|
; * PureBasic 6.41 C Backend (Linux x64)
|
|
;
|
|
; ==- Links -=====================================
|
|
; GitHub: https://github.com/aziascreations/BakaShrot
|
|
;}
|
|
|
|
; ============================================================================
|
|
; BakaShrot Signal
|
|
;
|
|
; Wakes up a thread waiting for something, instead of it polling with Delay():
|
|
; Waiter: BakaShrot_Signal_Wait(Signal, TimeoutMs), then checks for work.
|
|
; Waker: does the work's setup, then BakaShrot_Signal_Set(Signal).
|
|
; Signals don't stack: several Set() before a Wait() wake it once, so the
|
|
; waiter must handle everything that's there once woken. A Set() before the
|
|
; Wait() isn't lost, the Wait() returns right away.
|
|
;
|
|
; Windows: an auto-reset event. Elsewhere: a semaphore polled every ms, until
|
|
; something better is needed there.
|
|
; ============================================================================
|
|
|
|
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
|
|
|
|
; Returns 0 on failure.
|
|
Procedure.i BakaShrot_Signal_Create()
|
|
; Auto-reset, not set.
|
|
ProcedureReturn CreateEvent_(0, #False, #False, 0)
|
|
EndProcedure
|
|
|
|
Procedure BakaShrot_Signal_Set(Signal.i)
|
|
SetEvent_(Signal)
|
|
EndProcedure
|
|
|
|
; Waits until Signal is set, for up to TimeoutMs.
|
|
; Returns #True if it was set, #False if it timed out.
|
|
Procedure.i BakaShrot_Signal_Wait(Signal.i, TimeoutMs.i)
|
|
; WAIT_OBJECT_0
|
|
ProcedureReturn Bool(WaitForSingleObject_(Signal, TimeoutMs) = 0)
|
|
EndProcedure
|
|
|
|
Procedure BakaShrot_Signal_Free(Signal.i)
|
|
CloseHandle_(Signal)
|
|
EndProcedure
|
|
|
|
CompilerElse
|
|
|
|
Procedure.i BakaShrot_Signal_Create()
|
|
ProcedureReturn CreateSemaphore(0)
|
|
EndProcedure
|
|
|
|
Procedure BakaShrot_Signal_Set(Signal.i)
|
|
SignalSemaphore(Signal)
|
|
EndProcedure
|
|
|
|
Procedure.i BakaShrot_Signal_Wait(Signal.i, TimeoutMs.i)
|
|
Protected StartTime.i = ElapsedMilliseconds()
|
|
|
|
Repeat
|
|
If TrySemaphore(Signal)
|
|
; Doesn't stack, like the Windows version.
|
|
While TrySemaphore(Signal)
|
|
Wend
|
|
ProcedureReturn #True
|
|
EndIf
|
|
If ElapsedMilliseconds() - StartTime >= TimeoutMs
|
|
ProcedureReturn #False
|
|
EndIf
|
|
Delay(1)
|
|
ForEver
|
|
EndProcedure
|
|
|
|
Procedure BakaShrot_Signal_Free(Signal.i)
|
|
FreeSemaphore(Signal)
|
|
EndProcedure
|
|
|
|
CompilerEndIf
|