diff --git a/components/usb_host/include/usb_host.hpp b/components/usb_host/include/usb_host.hpp index d138dcd867..ca57bbf061 100644 --- a/components/usb_host/include/usb_host.hpp +++ b/components/usb_host/include/usb_host.hpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -425,6 +424,13 @@ class UsbHost : public BaseComponent { // An event queued by the HID driver task for the dispatch task. Input // reports are stored inline (no heap traffic on the driver task) unless a // larger Config::max_input_report_size was requested. + // Capacity reserved past Config::max_queued_events for the Disconnected events + // the queue keeps even when full (one per open device). Input reports never + // take the queue past max_queued_events, so the report path never allocates + // whatever this is; only a burst of more than this many disconnects queued on + // top of a full queue can grow a buffer, on that lifecycle event, and the + // buffer then keeps the larger capacity. + static constexpr size_t kLifecycleHeadroom = 8; struct Event { static constexpr size_t kInlineBytes = 64; // full-speed HID interrupt max packet enum class Type { NewDevice, Input, Disconnected } type; @@ -513,7 +519,11 @@ class UsbHost : public BaseComponent { // Event queue (driver task -> dispatch task) + dispatch task. std::mutex queue_mutex_; std::condition_variable queue_cv_; - std::deque queue_; + // The queue and the dispatch task's batch swap buffers each wake, and both + // keep their capacity (reserved before the dispatch task starts): a steady + // stream of Input reports does no heap work on either task. + std::vector queue_; + std::vector batch_; // the dispatch task's only uint32_t dropped_inputs_{0}; // guarded by queue_mutex_; rate-limits the drop log // Recycled Event::overflow buffers (only used when max_input_report_size > // Event::kInlineBytes) so large reports don't allocate per report either. diff --git a/components/usb_host/src/usb_host.cpp b/components/usb_host/src/usb_host.cpp index 6842947563..bf5bb4dd12 100644 --- a/components/usb_host/src/usb_host.cpp +++ b/components/usb_host/src/usb_host.cpp @@ -442,6 +442,14 @@ bool UsbHost::initialize(std::error_code &ec) { // 3) Start the dispatch task (runs device open/close + all user callbacks off // the driver task, so callbacks may issue control transfers). + { + // the queue's bound (see Config::max_queued_events) plus room for the + // disconnects that are always kept, so neither buffer grows per report + const size_t capacity = config_.max_queued_events + kLifecycleHeadroom; + std::lock_guard lk(queue_mutex_); + queue_.reserve(capacity); + batch_.reserve(capacity); + } dispatch_run_.store(true); dispatch_task_ = espp::Task::make_unique({ .callback = [this](std::mutex &m, @@ -1075,15 +1083,16 @@ void UsbHost::on_interface_event(hid_host_device_handle_t handle, // Dispatch task side: the real work + user callbacks // --------------------------------------------------------------------------- bool UsbHost::dispatch_task_fn(std::mutex & /*m*/, std::condition_variable & /*cv*/) { - std::deque batch; { std::unique_lock lk(queue_mutex_); // Bounded wait so Task::stop() is never held up for long; the stop path // also notifies queue_cv_ directly. queue_cv_.wait_for(lk, 100ms, [this] { return !queue_.empty() || !dispatch_run_.load(); }); - batch.swap(queue_); + // take the queued events, leaving the (empty, reserved) batch buffer as the + // queue: no allocation either side + queue_.swap(batch_); } - for (auto &ev : batch) { + for (auto &ev : batch_) { if (!dispatch_run_.load()) { break; } @@ -1105,6 +1114,7 @@ bool UsbHost::dispatch_task_fn(std::mutex & /*m*/, std::condition_variable & /*c break; } } + batch_.clear(); // keeps its capacity for the next wake return !dispatch_run_.load(); // true = stop the task } @@ -1116,6 +1126,8 @@ void UsbHost::stop_dispatch_task() { dispatch_task_->stop(); dispatch_task_.reset(); } + // (batch_ needs no clearing: the dispatch task empties it at the end of every + // pass, its last one included) std::lock_guard lk(queue_mutex_); queue_.clear(); overflow_pool_.clear();