]> Repositories - Adafruit_Blinka-hackapet.git/commitdiff
Merge pull request #170 from hamishmb/fix-thread-safety 3.0.2
authorMelissa LeBlanc-Williams <melissa@adafruit.com>
Mon, 25 Nov 2019 22:42:33 +0000 (14:42 -0800)
committerGitHub <noreply@github.com>
Mon, 25 Nov 2019 22:42:33 +0000 (14:42 -0800)
Add a lock for thread safety when the I2C class is used in a with statement

1  2 
src/busio.py

diff --combined src/busio.py
index ddf290d979af53a9b5563c19e7ed428d12fd9044,73c88ef6d57c0f6708f778f2bd8ae281c6d73549..f71871b675be1af1cbe9148c90930e299463b8f0
@@@ -7,6 -7,8 +7,8 @@@ See `CircuitPython:busio` in CircuitPyt
  * 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
@@@ -31,10 -33,12 +33,12 @@@ class I2C(Lockable)
                  self._i2c = _I2C(portId, mode=_I2C.MASTER, baudrate=frequency)
                  break
          else:
 -            raise NotImplementedError(
 +            raise ValueError(
                  "No Hardware I2C on (scl,sda)={}\nValid I2C ports: {}".format((scl, sda), i2cPorts)
              )
  
+         self._lock = threading.RLock()
      def deinit(self):
          try:
              del self._i2c
              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):
@@@ -95,7 -101,7 +101,7 @@@ class SPI(Lockable)
                  self._pins = (portSck, portMosi, portMiso)
                  break
          else:
 -            raise NotImplementedError(
 +            raise ValueError(
                  "No Hardware SPI on (SCLK, MOSI, MISO)={}\nValid SPI ports:{}".
                  format((clock, MOSI, MISO), spiPorts))
  
@@@ -230,7 -236,7 +236,7 @@@ class UART(Lockable)
                  )
                  break
          else:
 -            raise NotImplementedError(
 +            raise ValueError(
                  "No Hardware UART on (tx,rx)={}\nValid UART ports: {}".format((tx, rx), uartPorts)
              )