|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Lock Coordinator for Thread-Safe Component Interactions |
| 4 | +
|
| 5 | +This module provides coordination between different components (OrderManager, |
| 6 | +PositionManager, RealtimeClient) to prevent race conditions and ensure |
| 7 | +consistent data access patterns. |
| 8 | +""" |
| 9 | + |
| 10 | +import threading |
| 11 | +from collections.abc import Generator |
| 12 | +from contextlib import contextmanager |
| 13 | + |
| 14 | + |
| 15 | +class LockCoordinator: |
| 16 | + """ |
| 17 | + Coordinates locks between different components to prevent deadlocks and race conditions. |
| 18 | +
|
| 19 | + This class ensures that when multiple components need to access shared data, |
| 20 | + they do so in a consistent order to prevent deadlocks. |
| 21 | + """ |
| 22 | + |
| 23 | + def __init__(self): |
| 24 | + """Initialize the lock coordinator.""" |
| 25 | + # Master lock for coordinating component access |
| 26 | + self._master_lock = threading.RLock() |
| 27 | + |
| 28 | + # Component-specific locks in a consistent order to prevent deadlocks |
| 29 | + self._realtime_lock = threading.RLock() |
| 30 | + self._order_lock = threading.RLock() |
| 31 | + self._position_lock = threading.RLock() |
| 32 | + |
| 33 | + # Lock hierarchy to prevent deadlocks (always acquire in this order) |
| 34 | + self._lock_hierarchy = [ |
| 35 | + self._realtime_lock, # 1. Realtime client (data source) |
| 36 | + self._order_lock, # 2. Order manager |
| 37 | + self._position_lock, # 3. Position manager |
| 38 | + ] |
| 39 | + |
| 40 | + @property |
| 41 | + def realtime_lock(self) -> threading.RLock: |
| 42 | + """Get the realtime client lock.""" |
| 43 | + return self._realtime_lock |
| 44 | + |
| 45 | + @property |
| 46 | + def order_lock(self) -> threading.RLock: |
| 47 | + """Get the order manager lock.""" |
| 48 | + return self._order_lock |
| 49 | + |
| 50 | + @property |
| 51 | + def position_lock(self) -> threading.RLock: |
| 52 | + """Get the position manager lock.""" |
| 53 | + return self._position_lock |
| 54 | + |
| 55 | + @contextmanager |
| 56 | + def coordinated_access(self, *components: str) -> Generator[None, None, None]: |
| 57 | + """ |
| 58 | + Acquire locks for multiple components in a safe order. |
| 59 | +
|
| 60 | + Args: |
| 61 | + *components: Component names ('realtime', 'order', 'position') |
| 62 | +
|
| 63 | + Example: |
| 64 | + >>> with coordinator.coordinated_access("realtime", "order"): |
| 65 | + ... # Access data from both components safely |
| 66 | + ... pass |
| 67 | + """ |
| 68 | + # Map component names to locks |
| 69 | + component_locks = { |
| 70 | + "realtime": self._realtime_lock, |
| 71 | + "order": self._order_lock, |
| 72 | + "position": self._position_lock, |
| 73 | + } |
| 74 | + |
| 75 | + # Get locks in hierarchy order to prevent deadlocks |
| 76 | + requested_locks = [] |
| 77 | + for lock in self._lock_hierarchy: |
| 78 | + for component in components: |
| 79 | + if component_locks.get(component) == lock: |
| 80 | + requested_locks.append(lock) |
| 81 | + break |
| 82 | + |
| 83 | + # Acquire locks in order |
| 84 | + acquired_locks = [] |
| 85 | + try: |
| 86 | + for lock in requested_locks: |
| 87 | + lock.acquire() |
| 88 | + acquired_locks.append(lock) |
| 89 | + yield |
| 90 | + finally: |
| 91 | + # Release locks in reverse order |
| 92 | + for lock in reversed(acquired_locks): |
| 93 | + lock.release() |
| 94 | + |
| 95 | + @contextmanager |
| 96 | + def all_components_locked(self) -> Generator[None, None, None]: |
| 97 | + """ |
| 98 | + Acquire all component locks for major operations. |
| 99 | +
|
| 100 | + Use this for operations that need to modify data across multiple components. |
| 101 | + """ |
| 102 | + with self.coordinated_access("realtime", "order", "position"): |
| 103 | + yield |
| 104 | + |
| 105 | + |
| 106 | +# Global lock coordinator instance (singleton pattern) |
| 107 | +_global_coordinator: LockCoordinator | None = None |
| 108 | +_coordinator_lock = threading.Lock() |
| 109 | + |
| 110 | + |
| 111 | +def get_lock_coordinator() -> LockCoordinator: |
| 112 | + """ |
| 113 | + Get the global lock coordinator instance. |
| 114 | +
|
| 115 | + Returns: |
| 116 | + LockCoordinator: The singleton coordinator instance |
| 117 | + """ |
| 118 | + global _global_coordinator |
| 119 | + |
| 120 | + if _global_coordinator is None: |
| 121 | + with _coordinator_lock: |
| 122 | + if _global_coordinator is None: |
| 123 | + _global_coordinator = LockCoordinator() |
| 124 | + |
| 125 | + return _global_coordinator |
0 commit comments