From: Hamish McIntyre-Bhatty Date: Thu, 31 Oct 2019 14:38:40 +0000 (+0000) Subject: Add a lock for thread safety when the I2C class is used in a with statement X-Git-Tag: 3.0.2^2 X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/commitdiff_plain/76ca3e6a3c4c2f2695d96bdb0ee2db66d3f0f32c?hp=-c Add a lock for thread safety when the I2C class is used in a with statement --- 76ca3e6a3c4c2f2695d96bdb0ee2db66d3f0f32c diff --git a/src/busio.py b/src/busio.py index 9702848..73c88ef 100755 --- a/src/busio.py +++ b/src/busio.py @@ -7,6 +7,8 @@ See `CircuitPython:busio` in CircuitPython for more details. * Author(s): cefn """ +import threading + from adafruit_blinka import Enum, Lockable, agnostic from adafruit_blinka.agnostic import board_id, detector import adafruit_platformdetect.board as ap_board @@ -35,6 +37,8 @@ class I2C(Lockable): "No Hardware I2C on (scl,sda)={}\nValid I2C ports: {}".format((scl, sda), i2cPorts) ) + self._lock = threading.RLock() + def deinit(self): try: del self._i2c @@ -42,9 +46,11 @@ class I2C(Lockable): pass def __enter__(self): + self._lock.acquire() return self def __exit__(self, exc_type, exc_value, traceback): + self._lock.release() self.deinit() def scan(self):