5#ifdef USE_ESPHOME_TASK_LOG_BUFFER
15 this->slots_ = std::make_unique<LogMessage[]>(slot_count);
22int TaskLogBufferHost::acquire_write_slot_() {
24 size_t current_reserve = this->reserve_index_.load(std::memory_order_relaxed);
28 size_t next_reserve = (current_reserve + 1) % this->slot_count_;
32 size_t current_read = this->read_index_.load(std::memory_order_acquire);
33 if (next_reserve == current_read) {
38 if (this->reserve_index_.compare_exchange_weak(current_reserve, next_reserve, std::memory_order_acq_rel,
39 std::memory_order_relaxed)) {
40 return static_cast<int>(current_reserve);
46void TaskLogBufferHost::commit_write_slot_(
int slot_index) {
48 this->slots_[slot_index].ready.store(
true, std::memory_order_release);
52 size_t expected = slot_index;
53 size_t next = (slot_index + 1) % this->slot_count_;
58 if (!this->write_index_.compare_exchange_weak(expected, next, std::memory_order_release,
59 std::memory_order_relaxed)) {
66 next = (next + 1) % this->slot_count_;
67 if (!this->slots_[expected].ready.load(std::memory_order_acquire)) {
76 int slot_index = this->acquire_write_slot_();
91 if (pthread_getname_np(pthread_self(), thread_name_buf,
sizeof(thread_name_buf)) == 0) {
99 int ret = vsnprintf(msg.
text,
sizeof(msg.
text), format, args);
105 msg.
text_length =
static_cast<uint16_t
>(std::min(
static_cast<size_t>(ret),
sizeof(msg.
text) - 1));
115 this->commit_write_slot_(slot_index);
125 size_t current_read = this->read_index_.load(std::memory_order_relaxed);
126 size_t current_write = this->write_index_.load(std::memory_order_acquire);
129 if (current_read == current_write) {
135 if (!msg.
ready.load(std::memory_order_acquire)) {
144 size_t current_read = this->read_index_.load(std::memory_order_relaxed);
147 this->slots_[current_read].ready.store(
false, std::memory_order_release);
150 size_t next_read = (current_read + 1) % this->slot_count_;
151 this->read_index_.store(next_read, std::memory_order_release);
bool send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, const char *format, va_list args)
TaskLogBufferHost(size_t slot_count)
Constructor that takes the number of message slots.
void release_message_main_loop()
bool get_message_main_loop(LogMessage **message)
static constexpr size_t MAX_THREAD_NAME_SIZE
char text[MAX_TEXT_SIZE+1]
char thread_name[MAX_THREAD_NAME_SIZE]
std::atomic< bool > ready