Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions components/usb_host/include/usb_host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <chrono>
#include <condition_variable>
#include <cstdint>
#include <deque>
#include <functional>
#include <map>
#include <memory>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<Event> 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<Event> queue_;
std::vector<Event> 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.
Expand Down
18 changes: 15 additions & 3 deletions components/usb_host/src/usb_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> lk(queue_mutex_);
queue_.reserve(capacity);
batch_.reserve(capacity);
}
dispatch_run_.store(true);
dispatch_task_ = espp::Task::make_unique({
.callback = [this](std::mutex &m,
Expand Down Expand Up @@ -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<Event> batch;
{
std::unique_lock<std::mutex> 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;
}
Expand All @@ -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
}

Expand All @@ -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<std::mutex> lk(queue_mutex_);
queue_.clear();
overflow_pool_.clear();
Expand Down
Loading