Skip to content

Commit 7a86e2c

Browse files
committed
usb: Add critical section to the USB Buffer write path.
This isn't needed if the same thread is always the writer, but is needed if multiple threads (or main thread plus callback irq) try to write the buffer. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
1 parent fbf7e12 commit 7a86e2c

File tree

1 file changed

+7
-5
lines changed
  • micropython/usb/usb-device/usb/device

1 file changed

+7
-5
lines changed

micropython/usb/usb-device/usb/device/core.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -780,11 +780,13 @@ def pend_write(self, wmax=None):
780780
#
781781
# If wmax is set then the memoryview is pre-sliced to be at most
782782
# this many bytes long.
783-
#
784-
# (No critical section needed as self._w is only updated by the producer.)
785-
self._w = self._n
786-
end = (self._w + wmax) if wmax else len(self._b)
787-
return self._b[self._w : end]
783+
ist = machine.disable_irq()
784+
try:
785+
self._w = self._n
786+
end = (self._w + wmax) if wmax else len(self._b)
787+
return self._b[self._w : end]
788+
finally:
789+
machine.enable_irq(ist)
788790

789791
def finish_write(self, nbytes):
790792
# Called by the producer to indicate it wrote nbytes into the buffer.

0 commit comments

Comments
 (0)