mirror of
https://github.com/esphome/esphome.git
synced 2025-10-04 19:03:47 +01:00
Merge branch 'guard_trq_allocation' into integration
This commit is contained in:
@@ -363,7 +363,7 @@ TransferRequest *USBClient::get_trq_() {
|
|||||||
// Find first available slot (bit = 0) and try to claim it atomically
|
// Find first available slot (bit = 0) and try to claim it atomically
|
||||||
// We use a while loop to allow retrying the same slot after CAS failure
|
// We use a while loop to allow retrying the same slot after CAS failure
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
while (i < MAX_REQUESTS) {
|
while (i != MAX_REQUESTS) {
|
||||||
if (mask & (1U << i)) {
|
if (mask & (1U << i)) {
|
||||||
// Slot is in use, move to next slot
|
// Slot is in use, move to next slot
|
||||||
i++;
|
i++;
|
||||||
@@ -371,11 +371,9 @@ TransferRequest *USBClient::get_trq_() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Slot i appears available, try to claim it atomically
|
// Slot i appears available, try to claim it atomically
|
||||||
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
|
||||||
|
|
||||||
if (this->trq_in_use_.compare_exchange_weak(expected, desired, std::memory_order_acquire,
|
if (this->trq_in_use_.compare_exchange_weak(mask, desired, std::memory_order_acquire, std::memory_order_relaxed)) {
|
||||||
std::memory_order_relaxed)) {
|
|
||||||
// Successfully claimed slot i - prepare the TransferRequest
|
// Successfully claimed slot i - prepare the TransferRequest
|
||||||
auto *trq = &this->requests_[i];
|
auto *trq = &this->requests_[i];
|
||||||
trq->transfer->context = trq;
|
trq->transfer->context = trq;
|
||||||
@@ -383,8 +381,8 @@ TransferRequest *USBClient::get_trq_() {
|
|||||||
return trq;
|
return trq;
|
||||||
}
|
}
|
||||||
// CAS failed - another thread modified the bitmask
|
// CAS failed - another thread modified the bitmask
|
||||||
// Restart search from the beginning with the updated mask
|
// mask was already updated by compare_exchange_weak with the current value
|
||||||
mask = this->trq_in_use_.load(std::memory_order_relaxed);
|
// No need to reload - the CAS already did that for us
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user