mirror of
https://github.com/esphome/esphome.git
synced 2026-03-24 15:06:08 +08:00
[usb_host] Fix EventPool/LockFreeQueue sizing off-by-one (#14896)
This commit is contained in:
@@ -144,7 +144,10 @@ class USBClient : public Component {
|
||||
// Lock-free event queue and pool for USB task to main loop communication
|
||||
// Must be public for access from static callbacks
|
||||
LockFreeQueue<UsbEvent, USB_EVENT_QUEUE_SIZE> event_queue;
|
||||
EventPool<UsbEvent, USB_EVENT_QUEUE_SIZE> event_pool;
|
||||
// Pool sized to queue capacity (SIZE-1) because LockFreeQueue<T,N> is a ring
|
||||
// buffer that holds N-1 elements. This guarantees allocate() returns nullptr
|
||||
// before push() can fail, preventing a pool slot leak.
|
||||
EventPool<UsbEvent, USB_EVENT_QUEUE_SIZE - 1> event_pool;
|
||||
|
||||
protected:
|
||||
// Process USB events from the queue. Returns true if any work was done.
|
||||
|
||||
@@ -193,7 +193,8 @@ static void client_event_cb(const usb_host_client_event_msg_t *event_msg, void *
|
||||
return;
|
||||
}
|
||||
|
||||
// Push to lock-free queue (always succeeds since pool size == queue size)
|
||||
// Push always succeeds: pool is sized to queue capacity (SIZE-1), so if
|
||||
// allocate() returned non-null, the queue cannot be full.
|
||||
client->event_queue.push(event);
|
||||
|
||||
// Re-enable component loop to process the queued event
|
||||
|
||||
Reference in New Issue
Block a user