Use smaller atomic types for ESP32 BLE Tracker ring buffer indices (#9106)

This commit is contained in:
J. Nick Koston 2025-06-19 02:10:50 +02:00 committed by GitHub
parent a08d021f77
commit 95544e489d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 8 deletions

View File

@ -122,10 +122,10 @@ void ESP32BLETracker::loop() {
// Consumer side: This runs in the main loop thread // Consumer side: This runs in the main loop thread
if (this->scanner_state_ == ScannerState::RUNNING) { if (this->scanner_state_ == ScannerState::RUNNING) {
// Load our own index with relaxed ordering (we're the only writer) // Load our own index with relaxed ordering (we're the only writer)
size_t read_idx = this->ring_read_index_.load(std::memory_order_relaxed); uint8_t read_idx = this->ring_read_index_.load(std::memory_order_relaxed);
// Load producer's index with acquire to see their latest writes // Load producer's index with acquire to see their latest writes
size_t write_idx = this->ring_write_index_.load(std::memory_order_acquire); uint8_t write_idx = this->ring_write_index_.load(std::memory_order_acquire);
while (read_idx != write_idx) { while (read_idx != write_idx) {
// Process one result at a time directly from ring buffer // Process one result at a time directly from ring buffer
@ -409,11 +409,11 @@ void ESP32BLETracker::gap_scan_event_handler(const BLEScanResult &scan_result) {
// IMPORTANT: Only this thread writes to ring_write_index_ // IMPORTANT: Only this thread writes to ring_write_index_
// Load our own index with relaxed ordering (we're the only writer) // Load our own index with relaxed ordering (we're the only writer)
size_t write_idx = this->ring_write_index_.load(std::memory_order_relaxed); uint8_t write_idx = this->ring_write_index_.load(std::memory_order_relaxed);
size_t next_write_idx = (write_idx + 1) % SCAN_RESULT_BUFFER_SIZE; uint8_t next_write_idx = (write_idx + 1) % SCAN_RESULT_BUFFER_SIZE;
// Load consumer's index with acquire to see their latest updates // Load consumer's index with acquire to see their latest updates
size_t read_idx = this->ring_read_index_.load(std::memory_order_acquire); uint8_t read_idx = this->ring_read_index_.load(std::memory_order_acquire);
// Check if buffer is full // Check if buffer is full
if (next_write_idx != read_idx) { if (next_write_idx != read_idx) {

View File

@ -289,9 +289,9 @@ class ESP32BLETracker : public Component,
// Consumer: ESPHome main loop (loop() method) // Consumer: ESPHome main loop (loop() method)
// This design ensures zero blocking in the BT callback and prevents scan result loss // This design ensures zero blocking in the BT callback and prevents scan result loss
BLEScanResult *scan_ring_buffer_; BLEScanResult *scan_ring_buffer_;
std::atomic<size_t> ring_write_index_{0}; // Written only by BT callback (producer) std::atomic<uint8_t> ring_write_index_{0}; // Written only by BT callback (producer)
std::atomic<size_t> ring_read_index_{0}; // Written only by main loop (consumer) std::atomic<uint8_t> ring_read_index_{0}; // Written only by main loop (consumer)
std::atomic<size_t> scan_results_dropped_{0}; // Tracks buffer overflow events std::atomic<uint16_t> scan_results_dropped_{0}; // Tracks buffer overflow events
esp_bt_status_t scan_start_failed_{ESP_BT_STATUS_SUCCESS}; esp_bt_status_t scan_start_failed_{ESP_BT_STATUS_SUCCESS};
esp_bt_status_t scan_set_param_failed_{ESP_BT_STATUS_SUCCESS}; esp_bt_status_t scan_set_param_failed_{ESP_BT_STATUS_SUCCESS};