From f1312fb5c65e62b7121b29bfd74dad470784f497 Mon Sep 17 00:00:00 2001 From: Rune Harlyk Date: Tue, 8 Jul 2025 15:20:26 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Adds=20hasSubscribers=20function=20?= =?UTF-8?q?to=20event=20bus?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- esp32/include/event_bus.hpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/esp32/include/event_bus.hpp b/esp32/include/event_bus.hpp index 2de7f4c..6565ce1 100644 --- a/esp32/include/event_bus.hpp +++ b/esp32/include/event_bus.hpp @@ -41,6 +41,7 @@ class EventBus { inline static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED; inline static Msg latest {}; inline static volatile bool has_latest = false; + inline static std::atomic sub_cnt {0}; static void store(const Msg& m) { portENTER_CRITICAL(&mux); @@ -119,7 +120,11 @@ class EventBus { } ~Handle() = default; void unsubscribe() { - if (index < MaxSubs) subs[index].reset(), index = MaxSubs; + if (index < MaxSubs) { + subs[index].reset(); + sub_cnt.fetch_sub(1, std::memory_order_acq_rel); + index = MaxSubs; + } } bool valid() const { return index < MaxSubs; } }; @@ -134,6 +139,7 @@ class EventBus { .last = xTaskGetTickCount(), .mode = mode, .cnt = 0}); + sub_cnt.fetch_add(1, std::memory_order_acq_rel); return Handle(i); } return Handle(MaxSubs); @@ -200,4 +206,6 @@ class EventBus { portEXIT_CRITICAL(&mux); return true; } + + static bool hasSubscribers() { return sub_cnt.load(std::memory_order_acquire) != 0; } }; \ No newline at end of file