Merge origin/main (561b053f79) into simp/forwardport: forward-port 220 main commits into the simplified tree

This commit is contained in:
Teknium
2026-09-03 03:31:03 -07:00
336 changed files with 22683 additions and 1704 deletions

View File

@@ -64,16 +64,29 @@ class FileStateRegistry:
self._reads: Dict[str, Dict[str, ReadStamp]] = defaultdict(dict)
self._last_writer: Dict[str, Tuple[str, float]] = {}
self._path_locks: Dict[str, threading.Lock] = {}
self._path_lock_users: Dict[str, int] = {}
self._meta_lock = threading.Lock() # guards _path_locks
self._state_lock = threading.Lock() # guards _reads + _last_writer
@contextmanager
def lock_path(self, resolved: str):
"""Per-path lock: threads on the same path serialize, different paths proceed."""
"""Per-path lock: threads on the same path serialize, different paths proceed.
The lock entry is dropped once the last holder/waiter exits."""
with self._meta_lock:
lock = self._path_locks.setdefault(resolved, threading.Lock())
with lock:
self._path_lock_users[resolved] = self._path_lock_users.get(resolved, 0) + 1
lock.acquire()
try:
yield
finally:
lock.release()
with self._meta_lock:
users = self._path_lock_users[resolved] - 1
if users:
self._path_lock_users[resolved] = users
else:
self._path_lock_users.pop(resolved, None)
self._path_locks.pop(resolved, None)
def _stamp(self, task_id: str, resolved: str, mtime: float, now: float, partial: bool) -> None:
"""Caller holds ``_state_lock``."""
@@ -177,6 +190,11 @@ class FileStateRegistry:
with self._state_lock:
return list(self._reads.get(task_id, {}).keys())
def forget_task(self, task_id: str) -> None:
"""Release read stamps owned by a task after its lifecycle ends."""
with self._state_lock:
self._reads.pop(task_id, None)
def clear(self) -> None:
"""Reset all state. Intended for tests only."""
with self._state_lock:
@@ -184,6 +202,7 @@ class FileStateRegistry:
self._last_writer.clear()
with self._meta_lock:
self._path_locks.clear()
self._path_lock_users.clear()
_registry = FileStateRegistry()