diff --git a/src/lib/utility/messaging/MessageQueue.cpp b/src/lib/utility/messaging/MessageQueue.cpp index c9eb52fd..c4299906 100644 --- a/src/lib/utility/messaging/MessageQueue.cpp +++ b/src/lib/utility/messaging/MessageQueue.cpp @@ -70,6 +70,9 @@ void MessageQueue::pushMessage(std::shared_ptr message) void MessageQueue::startMessageLoopThreaded() { std::thread(&MessageQueue::startMessageLoop, this).detach(); + + std::lock_guard lock(m_threadMutex); + m_threadIsRunning = true; } void MessageQueue::startMessageLoop() @@ -88,32 +91,56 @@ void MessageQueue::startMessageLoop() while (true) { + processMessages(); + { std::lock_guard lock(m_loopMutex); if (!m_loopIsRunning) { - return; + break; } } - processMessages(); - const int SLEEP_TIME_MS = 25; std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS)); } + + { + std::lock_guard lock(m_threadMutex); + if (m_threadIsRunning) + { + m_threadIsRunning = false; + } + } } void MessageQueue::stopMessageLoop() { - std::lock_guard lock(m_loopMutex); - - if (!m_loopIsRunning) { - LOG_WARNING("Loop is not running"); + std::lock_guard lock(m_loopMutex); + + if (!m_loopIsRunning) + { + LOG_WARNING("Loop is not running"); + } + + m_loopIsRunning = false; } - m_loopIsRunning = false; + while (true) + { + { + std::lock_guard lock(m_threadMutex); + if (!m_threadIsRunning) + { + break; + } + } + + const int SLEEP_TIME_MS = 25; + std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS)); + } } bool MessageQueue::loopIsRunning() const @@ -135,6 +162,7 @@ MessageQueue::MessageQueue() : m_currentListenerIndex(0) , m_listenersLength(0) , m_loopIsRunning(false) + , m_threadIsRunning(false) { m_frontMessageBuffer = std::make_shared(); m_backMessageBuffer = std::make_shared(); diff --git a/src/lib/utility/messaging/MessageQueue.h b/src/lib/utility/messaging/MessageQueue.h index 0059f02d..12372602 100644 --- a/src/lib/utility/messaging/MessageQueue.h +++ b/src/lib/utility/messaging/MessageQueue.h @@ -43,11 +43,13 @@ private: size_t m_currentListenerIndex; size_t m_listenersLength; bool m_loopIsRunning; + bool m_threadIsRunning; mutable std::mutex m_frontMessageBufferMutex; mutable std::mutex m_backMessageBufferMutex; mutable std::mutex m_listenersMutex; mutable std::mutex m_loopMutex; + mutable std::mutex m_threadMutex; }; #endif // MESSAGE_QUEUE_H diff --git a/src/lib/utility/scheduling/TaskScheduler.cpp b/src/lib/utility/scheduling/TaskScheduler.cpp index 3269d0bb..9b5267ed 100644 --- a/src/lib/utility/scheduling/TaskScheduler.cpp +++ b/src/lib/utility/scheduling/TaskScheduler.cpp @@ -31,6 +31,9 @@ void TaskScheduler::interruptCurrentTask() void TaskScheduler::startSchedulerLoopThreaded() { std::thread(&TaskScheduler::startSchedulerLoop, this).detach(); + + std::lock_guard lock(m_threadMutex); + m_threadIsRunning = true; } void TaskScheduler::startSchedulerLoop() @@ -49,20 +52,28 @@ void TaskScheduler::startSchedulerLoop() while (true) { + updateTasks(); + { std::lock_guard lock(m_loopMutex); if (!m_loopIsRunning) { - return; + break; } } - updateTasks(); - const int SLEEP_TIME_MS = 25; std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS)); } + + { + std::lock_guard lock(m_threadMutex); + if (m_threadIsRunning) + { + m_threadIsRunning = false; + } + } } void TaskScheduler::stopSchedulerLoop() @@ -79,6 +90,20 @@ void TaskScheduler::stopSchedulerLoop() } interruptCurrentTask(); + + while (true) + { + { + std::lock_guard lock(m_threadMutex); + if (!m_threadIsRunning) + { + break; + } + } + + const int SLEEP_TIME_MS = 25; + std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS)); + } } bool TaskScheduler::loopIsRunning() const @@ -91,6 +116,7 @@ std::shared_ptr TaskScheduler::s_instance; TaskScheduler::TaskScheduler() : m_loopIsRunning(false) + , m_threadIsRunning(false) , m_interruptTask(false) { } diff --git a/src/lib/utility/scheduling/TaskScheduler.h b/src/lib/utility/scheduling/TaskScheduler.h index 8957fb11..cb373311 100644 --- a/src/lib/utility/scheduling/TaskScheduler.h +++ b/src/lib/utility/scheduling/TaskScheduler.h @@ -36,12 +36,14 @@ private: virtual void handleMessage(MessageInterruptTasks* message); bool m_loopIsRunning; + bool m_threadIsRunning; std::queue> m_tasks; bool m_interruptTask; mutable std::mutex m_tasksMutex; mutable std::mutex m_loopMutex; + mutable std::mutex m_threadMutex; }; #endif // TASK_SCHEDULER_H