修复了大量存在的问题,增加了假鸭子类型等等机制

This commit is contained in:
2026-06-25 14:49:46 +08:00
parent 19f2787db0
commit d88d11b646
827 changed files with 32617 additions and 18316 deletions

46
includes/rwlock.py Normal file
View File

@@ -0,0 +1,46 @@
import t, c
from stdint import *
from w32.win32base import *
from w32.win32sync import (
InitializeSRWLock,
AcquireSRWLockExclusive, ReleaseSRWLockExclusive,
AcquireSRWLockShared, ReleaseSRWLockShared
)
@t.Object
class RWLock:
"""基于 SRWLock 的读写锁,支持多读单写,不支持递归。
用法:
rwlock: RWLock = RWLock()
rwlock.acquire_read()
# ... 读取共享数据
rwlock.release_read()
rwlock.acquire_write()
# ... 修改共享数据
rwlock.release_write()
"""
lock: VOIDPTR
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()