mirror of
https://github.com/esphome/esphome.git
synced 2025-10-05 11:23:47 +01:00
ble pool
This commit is contained in:
@@ -18,7 +18,7 @@
|
|||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace esp32_ble {
|
namespace esp32_ble {
|
||||||
|
|
||||||
template<class T, size_t SIZE> class LockFreeQueue {
|
template<class T, uint8_t SIZE> class LockFreeQueue {
|
||||||
public:
|
public:
|
||||||
LockFreeQueue() : head_(0), tail_(0), dropped_count_(0) {}
|
LockFreeQueue() : head_(0), tail_(0), dropped_count_(0) {}
|
||||||
|
|
||||||
@@ -26,8 +26,8 @@ template<class T, size_t SIZE> class LockFreeQueue {
|
|||||||
if (element == nullptr)
|
if (element == nullptr)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
size_t current_tail = tail_.load(std::memory_order_relaxed);
|
uint8_t current_tail = tail_.load(std::memory_order_relaxed);
|
||||||
size_t next_tail = (current_tail + 1) % SIZE;
|
uint8_t next_tail = (current_tail + 1) % SIZE;
|
||||||
|
|
||||||
if (next_tail == head_.load(std::memory_order_acquire)) {
|
if (next_tail == head_.load(std::memory_order_acquire)) {
|
||||||
// Buffer full
|
// Buffer full
|
||||||
@@ -41,7 +41,7 @@ template<class T, size_t SIZE> class LockFreeQueue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
T *pop() {
|
T *pop() {
|
||||||
size_t current_head = head_.load(std::memory_order_relaxed);
|
uint8_t current_head = head_.load(std::memory_order_relaxed);
|
||||||
|
|
||||||
if (current_head == tail_.load(std::memory_order_acquire)) {
|
if (current_head == tail_.load(std::memory_order_acquire)) {
|
||||||
return nullptr; // Empty
|
return nullptr; // Empty
|
||||||
@@ -53,27 +53,27 @@ template<class T, size_t SIZE> class LockFreeQueue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
size_t tail = tail_.load(std::memory_order_acquire);
|
uint8_t tail = tail_.load(std::memory_order_acquire);
|
||||||
size_t head = head_.load(std::memory_order_acquire);
|
uint8_t head = head_.load(std::memory_order_acquire);
|
||||||
return (tail - head + SIZE) % SIZE;
|
return (tail - head + SIZE) % SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t get_and_reset_dropped_count() { return dropped_count_.exchange(0, std::memory_order_relaxed); }
|
uint32_t get_and_reset_dropped_count() { return dropped_count_.exchange(0, std::memory_order_relaxed); }
|
||||||
|
|
||||||
void increment_dropped_count() { dropped_count_.fetch_add(1, std::memory_order_relaxed); }
|
void increment_dropped_count() { dropped_count_.fetch_add(1, std::memory_order_relaxed); }
|
||||||
|
|
||||||
bool empty() const { return head_.load(std::memory_order_acquire) == tail_.load(std::memory_order_acquire); }
|
bool empty() const { return head_.load(std::memory_order_acquire) == tail_.load(std::memory_order_acquire); }
|
||||||
|
|
||||||
bool full() const {
|
bool full() const {
|
||||||
size_t next_tail = (tail_.load(std::memory_order_relaxed) + 1) % SIZE;
|
uint8_t next_tail = (tail_.load(std::memory_order_relaxed) + 1) % SIZE;
|
||||||
return next_tail == head_.load(std::memory_order_acquire);
|
return next_tail == head_.load(std::memory_order_acquire);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
T *buffer_[SIZE];
|
T *buffer_[SIZE];
|
||||||
std::atomic<size_t> head_;
|
std::atomic<uint8_t> head_;
|
||||||
std::atomic<size_t> tail_;
|
std::atomic<uint8_t> tail_;
|
||||||
std::atomic<size_t> dropped_count_;
|
std::atomic<uint32_t> dropped_count_; // Keep this larger for accumulated counts
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace esp32_ble
|
} // namespace esp32_ble
|
||||||
|
Reference in New Issue
Block a user