device: lock querying all unqueried keys to prevent two threads from doing the same work

This commit is contained in:
Peter F. Patel-Schneider 2022-01-13 11:26:09 -05:00
parent 22b99ecce7
commit 26aa1ee15f
1 changed files with 8 additions and 4 deletions

View File

@ -18,6 +18,8 @@
# Logitech Unifying Receiver API. # Logitech Unifying Receiver API.
import threading as _threading
from logging import DEBUG as _DEBUG from logging import DEBUG as _DEBUG
from logging import ERROR as _ERROR from logging import ERROR as _ERROR
from logging import INFO as _INFO from logging import INFO as _INFO
@ -560,11 +562,12 @@ class ReprogrammableKeyV4(ReprogrammableKey):
class KeysArray: class KeysArray:
"""A sequence of key mappings supported by a HID++ 2.0 device.""" """A sequence of key mappings supported by a HID++ 2.0 device."""
__slots__ = ('device', 'keys', 'keyversion', 'cid_to_tid', 'group_cids') __slots__ = ('device', 'keys', 'keyversion', 'cid_to_tid', 'group_cids', 'lock')
def __init__(self, device, count): def __init__(self, device, count):
assert device is not None assert device is not None
self.device = device self.device = device
self.lock = _threading.Lock()
if FEATURE.REPROG_CONTROLS_V4 in self.device.features: if FEATURE.REPROG_CONTROLS_V4 in self.device.features:
self.keyversion = FEATURE.REPROG_CONTROLS_V4 self.keyversion = FEATURE.REPROG_CONTROLS_V4
elif FEATURE.REPROG_CONTROLS_V2 in self.device.features: elif FEATURE.REPROG_CONTROLS_V2 in self.device.features:
@ -613,9 +616,10 @@ class KeysArray:
def _ensure_all_keys_queried(self): def _ensure_all_keys_queried(self):
"""The retrieval of key information is lazy, but for certain functionality """The retrieval of key information is lazy, but for certain functionality
we need to know all keys. This function makes sure that's the case.""" we need to know all keys. This function makes sure that's the case."""
for (i, k) in enumerate(self.keys): with self.lock: # don't want two threads doing this
if k is None: for (i, k) in enumerate(self.keys):
self._query_key(i) if k is None:
self._query_key(i)
def __getitem__(self, index): def __getitem__(self, index):
if isinstance(index, int): if isinstance(index, int):