-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared_state.py
More file actions
115 lines (100 loc) · 3.56 KB
/
shared_state.py
File metadata and controls
115 lines (100 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import threading
import copy
import time
from collections import deque
class SharedGameState:
"""
Thread-safe shared state optimized for ultra-low latency real-time processing.
Minimizes lock contention and eliminates unnecessary memory copies.
"""
def __init__(self):
self._lock = threading.RLock()
self._frame_buffer = None
self._frame_id = 0
self._frame_timestamp = 0.0
self._last_processed_id = -1
self._tracked_objects = []
self._tracking_status = "READY"
self._command_queue = deque(maxlen=5)
self.is_running = True
self._fps = 0.0
self._dropped_frames = 0
self._lock_contention_count = 0
self._display_state_cache = {
"frame": None,
"objects": [],
"status": "READY",
"fps": 0.0,
}
@property
def lock(self):
return self._lock
def update_frame(self, frame):
if frame is None:
return
with self._lock:
self._frame_buffer = frame
self._frame_id += 1
self._frame_timestamp = time.time()
def get_latest_frame(self, mark_processed=True):
"""
Get latest frame optimized for vision thread.
Returns None if no new frame available (prevents duplicate processing).
Returns a copy to prevent mutation of the shared buffer.
"""
with self._lock:
if self._frame_buffer is None:
return None
if self._frame_id <= self._last_processed_id:
self._dropped_frames += 1
return None
frame = self._frame_buffer.copy()
if mark_processed:
self._last_processed_id = self._frame_id
return frame
def peek_frame(self):
with self._lock:
return self._frame_buffer
def update_tracking(self, objects, status):
with self._lock:
self._tracked_objects = list(objects)
self._tracking_status = status
def get_tracking_state(self):
with self._lock:
return list(self._tracked_objects), self._tracking_status
def get_display_state(self):
with self._lock:
self._display_state_cache["frame"] = self._frame_buffer
self._display_state_cache["objects"] = copy.deepcopy(self._tracked_objects)
self._display_state_cache["status"] = self._tracking_status
self._display_state_cache["fps"] = self._fps
return self._display_state_cache.copy()
def add_command(self, command):
with self._lock:
if len(self._command_queue) >= 5:
try:
self._command_queue.popleft()
except IndexError:
pass
self._command_queue.append(command)
def get_next_command(self):
with self._lock:
if self._command_queue:
return self._command_queue.popleft()
return None
def has_commands(self):
with self._lock:
return len(self._command_queue) > 0
def set_fps(self, fps):
with self._lock:
self._fps = fps
def get_stats(self):
with self._lock:
return {
"frame_id": self._frame_id,
"last_processed": self._last_processed_id,
"dropped_frames": self._dropped_frames,
"pending_commands": len(self._command_queue),
"tracked_objects": len(self._tracked_objects),
"fps": self._fps,
}