二者在 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,传的仍是那块存储的地址)。
47 lines
1.1 KiB
Plaintext
47 lines
1.1 KiB
Plaintext
import t, c
|
|
from stdint import *
|
|
from w32.win32base import *
|
|
from w32.win32sync import (
|
|
SRWLOCK,
|
|
InitializeSRWLock,
|
|
AcquireSRWLockExclusive, ReleaseSRWLockExclusive,
|
|
AcquireSRWLockShared, ReleaseSRWLockShared
|
|
)
|
|
|
|
|
|
class RWLock:
|
|
"""SRWLock-based read-write lock that supports multiple readers and single writer, but does not support recursion.
|
|
|
|
Usage:
|
|
rwlock: RWLock = RWLock()
|
|
rwlock.acquire_read()
|
|
# ... read shared data
|
|
rwlock.release_read()
|
|
rwlock.acquire_write()
|
|
# ... modify shared data
|
|
rwlock.release_write()
|
|
"""
|
|
lock: SRWLOCK
|
|
|
|
def __init__(self):
|
|
InitializeSRWLock(c.Addr(self.lock))
|
|
|
|
def acquire_read(self):
|
|
AcquireSRWLockShared(c.Addr(self.lock))
|
|
|
|
def release_read(self):
|
|
ReleaseSRWLockShared(c.Addr(self.lock))
|
|
|
|
def acquire_write(self):
|
|
AcquireSRWLockExclusive(c.Addr(self.lock))
|
|
|
|
def release_write(self):
|
|
ReleaseSRWLockExclusive(c.Addr(self.lock))
|
|
|
|
def __enter__(self) -> 'RWLock' | t.CPtr:
|
|
self.acquire_write()
|
|
return self
|
|
|
|
def __exit__(self):
|
|
self.release_write()
|