From 76ca3e6a3c4c2f2695d96bdb0ee2db66d3f0f32c Mon Sep 17 00:00:00 2001 From: Hamish McIntyre-Bhatty Date: Thu, 31 Oct 2019 14:38:40 +0000 Subject: [PATCH] Add a lock for thread safety when the I2C class is used in a with statement --- src/busio.py | 6 ++++++ 1 file changed, 6 insertions(+) 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): -- 2.49.0