1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-04 11:02:19 +01:00

fix underflow

This commit is contained in:
J. Nick Koston
2025-09-28 18:41:20 -05:00
parent e07af13bef
commit d65b1fad67

View File

@@ -361,8 +361,15 @@ TransferRequest *USBClient::get_trq_() {
uint16_t mask = this->trq_in_use_.load(std::memory_order_relaxed); uint16_t mask = this->trq_in_use_.load(std::memory_order_relaxed);
// Find first available slot (bit = 0) and try to claim it atomically // Find first available slot (bit = 0) and try to claim it atomically
for (size_t i = 0; i < MAX_REQUESTS; i++) { // We use a while loop to allow retrying the same slot after CAS failure
if (!(mask & (1U << i))) { size_t i = 0;
while (i < MAX_REQUESTS) {
if (mask & (1U << i)) {
// Slot is in use, move to next slot
i++;
continue;
}
// Slot i appears available, try to claim it atomically // Slot i appears available, try to claim it atomically
uint16_t expected = mask; uint16_t expected = mask;
uint16_t desired = mask | (1U << i); // Set bit i to mark as in-use uint16_t desired = mask | (1U << i); // Set bit i to mark as in-use
@@ -376,9 +383,8 @@ TransferRequest *USBClient::get_trq_() {
return trq; return trq;
} }
// Another thread claimed this slot, retry with updated mask // Another thread claimed this slot, retry with updated mask
// Don't increment i - retry the same slot with the updated mask
mask = expected; mask = expected;
i--; // Retry the same index with new mask value
}
} }
ESP_LOGE(TAG, "Too many requests queued (all %d slots in use)", MAX_REQUESTS); ESP_LOGE(TAG, "Too many requests queued (all %d slots in use)", MAX_REQUESTS);