Your question is Thread-Safe UART Circular Buffer. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Autonomous Solutions vehicle controllers receive UART bytes from interrupt-driven producers while another thread consumes them. Implement a bounded, thread-safe circular buffer that preserves FIFO order without shifting stored data.
Use the function below to process operations against one buffer. Each operation is a dictionary with an op field:
write: data is a list of byte values from 0 through 255; write as many bytes as available and return the number written.read: count is a nonnegative integer; remove and return up to count oldest bytes.peek: return up to count oldest bytes without removing them.size: return the number of stored bytes.Every public operation must be safe when called concurrently. Operations must be linearizable, meaning each operation appears to occur at one instant while holding the buffer lock. The buffer must use exactly capacity storage slots and must distinguish empty from full using an explicit size or equivalent state.
Return one result per operation in the same order.
def process_uart_buffer(capacity, operations):