allow notifying queue and improve shutdown duration

This commit is contained in:
John
2023-09-23 09:09:05 +02:00
parent 689c38610b
commit 9bd534afa4
3 changed files with 14 additions and 9 deletions
+7
View File
@@ -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. */ /** the delay for running the update check. */
#define CHECK_DELAY (24*3600) #define CHECK_DELAY (24*3600)
+1 -1
View File
@@ -120,7 +120,7 @@ class MainLoop : public Thread, DeviceListener {
/** /**
* Shutdown the main loop. * Shutdown the main loop.
*/ */
void shutdown() { m_shutdown = true; } void shutdown();
/** /**
* Get the @a BusHandler instance. * Get the @a BusHandler instance.
+6 -8
View File
@@ -65,11 +65,13 @@ class Queue {
public: public:
/** /**
* Add an item to the end of queue. * 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) { void push(T item) {
pthread_mutex_lock(&m_mutex); pthread_mutex_lock(&m_mutex);
m_queue.push_back(item); if (item) {
m_queue.push_back(item);
}
pthread_cond_broadcast(&m_cond); pthread_cond_broadcast(&m_cond);
pthread_mutex_unlock(&m_mutex); pthread_mutex_unlock(&m_mutex);
} }
@@ -82,15 +84,11 @@ class Queue {
T pop(int timeout = 0) { T pop(int timeout = 0) {
T item; T item;
pthread_mutex_lock(&m_mutex); pthread_mutex_lock(&m_mutex);
if (timeout > 0) { if (timeout > 0 && m_queue.empty()) {
struct timespec t; struct timespec t;
clockGettime(&t); clockGettime(&t);
t.tv_sec += timeout; t.tv_sec += timeout;
while (m_queue.empty()) { pthread_cond_timedwait(&m_cond, &m_mutex, &t);
if (pthread_cond_timedwait(&m_cond, &m_mutex, &t) != 0) {
break;
}
}
} }
if (m_queue.empty()) { if (m_queue.empty()) {
item = nullptr; item = nullptr;