|
5 | 5 | import threading |
6 | 6 | import time |
7 | 7 | from collections.abc import Generator |
| 8 | +from contextlib import contextmanager |
8 | 9 | from typing import final |
9 | 10 |
|
10 | 11 | from core.workflow.graph_events import GraphEngineEvent |
@@ -51,43 +52,23 @@ def release_write(self) -> None: |
51 | 52 | """Release a write lock.""" |
52 | 53 | self._read_ready.release() |
53 | 54 |
|
54 | | - def read_lock(self) -> "ReadLockContext": |
| 55 | + @contextmanager |
| 56 | + def read_lock(self): |
55 | 57 | """Return a context manager for read locking.""" |
56 | | - return ReadLockContext(self) |
| 58 | + self.acquire_read() |
| 59 | + try: |
| 60 | + yield |
| 61 | + finally: |
| 62 | + self.release_read() |
57 | 63 |
|
58 | | - def write_lock(self) -> "WriteLockContext": |
| 64 | + @contextmanager |
| 65 | + def write_lock(self): |
59 | 66 | """Return a context manager for write locking.""" |
60 | | - return WriteLockContext(self) |
61 | | - |
62 | | - |
63 | | -@final |
64 | | -class ReadLockContext: |
65 | | - """Context manager for read locks.""" |
66 | | - |
67 | | - def __init__(self, lock: ReadWriteLock) -> None: |
68 | | - self._lock = lock |
69 | | - |
70 | | - def __enter__(self) -> "ReadLockContext": |
71 | | - self._lock.acquire_read() |
72 | | - return self |
73 | | - |
74 | | - def __exit__(self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: object) -> None: |
75 | | - self._lock.release_read() |
76 | | - |
77 | | - |
78 | | -@final |
79 | | -class WriteLockContext: |
80 | | - """Context manager for write locks.""" |
81 | | - |
82 | | - def __init__(self, lock: ReadWriteLock) -> None: |
83 | | - self._lock = lock |
84 | | - |
85 | | - def __enter__(self) -> "WriteLockContext": |
86 | | - self._lock.acquire_write() |
87 | | - return self |
88 | | - |
89 | | - def __exit__(self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: object) -> None: |
90 | | - self._lock.release_write() |
| 67 | + self.acquire_write() |
| 68 | + try: |
| 69 | + yield |
| 70 | + finally: |
| 71 | + self.release_write() |
91 | 72 |
|
92 | 73 |
|
93 | 74 | @final |
|
0 commit comments