二者在 Windows 头文件里都是"单个 PVOID 的结构"(sizeof 8),API 收的是**存储地址**。 原先声明成 VOIDPTR(i8*)⇒ `c.Addr(self.cond_var)` 得到 i8**(多一层),与形参 i8* 层数 不符(指针隐式转换收紧后报 CastError,现场 includes/condition.vp:27)。 - win32sync.vp:新增 class SRWLOCK / class CONDITION_VARIABLE(Ptr: VOIDPTR,8 字节); 5 个 SRWLock extern 与 5 个条件变量 extern 的形参由 VOIDPTR 改为对应结构体指针 - condition.vp:cond_var 由 VOIDPTR 改为 CONDITION_VARIABLE - rwlock.vp:lock 由 VOIDPTR 改为 SRWLOCK 布局与运行期语义均不变(都是 8 字节、对齐 8,传的仍是那块存储的地址)。
54 lines
1.4 KiB
Plaintext
54 lines
1.4 KiB
Plaintext
import t, c
|
|
from stdint import *
|
|
from w32.win32base import *
|
|
from w32.win32sync import (
|
|
CRITICAL_SECTION,
|
|
CONDITION_VARIABLE,
|
|
InitializeCriticalSection, EnterCriticalSection, LeaveCriticalSection, DeleteCriticalSection,
|
|
InitializeConditionVariable, SleepConditionVariableCS,
|
|
WakeConditionVariable, WakeAllConditionVariable
|
|
)
|
|
|
|
|
|
class Condition:
|
|
"""Windows condition variable backed primitive, internally bundled with CRITICAL_SECTION.
|
|
|
|
Usage:
|
|
cond: Condition = Condition()
|
|
with cond:
|
|
while not ready:
|
|
cond.wait()
|
|
cond.notify()
|
|
"""
|
|
cond_var: CONDITION_VARIABLE
|
|
cs: CRITICAL_SECTION
|
|
|
|
def __init__(self):
|
|
InitializeCriticalSection(c.Addr(self.cs))
|
|
InitializeConditionVariable(c.Addr(self.cond_var))
|
|
|
|
def wait(self, timeout_ms: ULONG = INFINITE) -> BOOL:
|
|
return SleepConditionVariableCS(c.Addr(self.cond_var), c.Addr(self.cs), timeout_ms)
|
|
|
|
def notify(self):
|
|
WakeConditionVariable(c.Addr(self.cond_var))
|
|
|
|
def notify_all(self):
|
|
WakeAllConditionVariable(c.Addr(self.cond_var))
|
|
|
|
def acquire(self):
|
|
EnterCriticalSection(c.Addr(self.cs))
|
|
|
|
def release(self):
|
|
LeaveCriticalSection(c.Addr(self.cs))
|
|
|
|
def __enter__(self) -> 'Condition' | t.CPtr:
|
|
self.acquire()
|
|
return self
|
|
|
|
def __exit__(self):
|
|
self.release()
|
|
|
|
def delete(self):
|
|
DeleteCriticalSection(c.Addr(self.cs))
|