From 9bd534afa41fdd2266b1e425aa79fcd72f37abba Mon Sep 17 00:00:00 2001 From: John Date: Sat, 23 Sep 2023 09:09:05 +0200 Subject: [PATCH] allow notifying queue and improve shutdown duration --- src/ebusd/mainloop.cpp | 7 +++++++ src/ebusd/mainloop.h | 2 +- src/lib/utils/queue.h | 14 ++++++-------- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/ebusd/mainloop.cpp b/src/ebusd/mainloop.cpp index ddcda4eb..3d90cc4a 100644 --- a/src/ebusd/mainloop.cpp +++ b/src/ebusd/mainloop.cpp @@ -205,6 +205,13 @@ MainLoop::~MainLoop() { } } +void MainLoop::shutdown() { + m_shutdown = true; + if (m_requestQueue != nullptr) { + m_requestQueue->push(nullptr); // just to notify potentially waiting thread + } +} + /** the delay for running the update check. */ #define CHECK_DELAY (24*3600) diff --git a/src/ebusd/mainloop.h b/src/ebusd/mainloop.h index b1e8c234..ae099db5 100644 --- a/src/ebusd/mainloop.h +++ b/src/ebusd/mainloop.h @@ -120,7 +120,7 @@ class MainLoop : public Thread, DeviceListener { /** * Shutdown the main loop. */ - void shutdown() { m_shutdown = true; } + void shutdown(); /** * Get the @a BusHandler instance. diff --git a/src/lib/utils/queue.h b/src/lib/utils/queue.h index 94eb54a7..b41bad26 100755 --- a/src/lib/utils/queue.h +++ b/src/lib/utils/queue.h @@ -65,11 +65,13 @@ class Queue { public: /** * Add an item to the end of queue. - * @param item the item to add. + * @param item the item to add, or nullptr for notifying only. */ void push(T item) { pthread_mutex_lock(&m_mutex); - m_queue.push_back(item); + if (item) { + m_queue.push_back(item); + } pthread_cond_broadcast(&m_cond); pthread_mutex_unlock(&m_mutex); } @@ -82,15 +84,11 @@ class Queue { T pop(int timeout = 0) { T item; pthread_mutex_lock(&m_mutex); - if (timeout > 0) { + if (timeout > 0 && m_queue.empty()) { struct timespec t; clockGettime(&t); t.tv_sec += timeout; - while (m_queue.empty()) { - if (pthread_cond_timedwait(&m_cond, &m_mutex, &t) != 0) { - break; - } - } + pthread_cond_timedwait(&m_cond, &m_mutex, &t); } if (m_queue.empty()) { item = nullptr;