utility: Sending Messages via TaskScheduler by default

This change makes the MessageQueue use Tasks for sending a Message to each MessageListener. Messages can define their
behavior by setting setSendAsTask(), the default is true. Only MessageStatus and MessageInterruptTasks are still sent on
the Messaging thread to allow for immediate effect.

This change also introduced the class SimpleTask which holds only a single perform() callback to override, for Tasks
that are finished in a single step. The class LambdaTask derives from SimpleTask and allows for passing a lambda as the
perform() callback.
This commit is contained in:
Eberhard Graether
2015-04-26 22:21:01 +02:00
parent d1db054d98
commit e0ddc67597
21 changed files with 233 additions and 146 deletions
+1 -1
View File
@@ -21,7 +21,7 @@ public:
return MessageType::getStaticType();
}
void dispatch()
virtual void dispatch()
{
std::shared_ptr<MessageBase> message = std::make_shared<MessageType>(*dynamic_cast<MessageType*>(this));
MessageQueue::getInstance()->pushMessage(message);
+20 -2
View File
@@ -10,9 +10,15 @@ public:
{
UndoType_Normal,
UndoType_Redo,
UndoType_Undo,
UndoType_Undo
};
MessageBase() : UndoRedoType(UndoType_Normal){};
MessageBase()
: UndoRedoType(UndoType_Normal)
, m_sendAsTask(true)
{
}
virtual ~MessageBase()
{
}
@@ -20,8 +26,20 @@ public:
virtual std::string getType() const = 0;
virtual void dispatch() = 0;
bool sendAsTask() const
{
return m_sendAsTask;
}
void setSendAsTask(bool sendAsTask)
{
m_sendAsTask = sendAsTask;
}
UndoType UndoRedoType;
private:
bool m_sendAsTask;
};
#endif // MESSAGE_BASE_H
+1 -2
View File
@@ -11,8 +11,7 @@ template<typename MessageType>
class MessageListener: public MessageListenerBase
{
public:
MessageListener(bool toFront = false)
: MessageListenerBase(toFront)
MessageListener()
{
}
@@ -9,9 +9,9 @@
class MessageListenerBase
{
public:
MessageListenerBase(bool toFront)
MessageListenerBase()
{
MessageQueue::getInstance()->registerListener(this, toFront);
MessageQueue::getInstance()->registerListener(this);
}
virtual ~MessageListenerBase()
+63 -30
View File
@@ -6,6 +6,8 @@
#include "utility/logging/logging.h"
#include "utility/messaging/MessageBase.h"
#include "utility/messaging/MessageListenerBase.h"
#include "utility/scheduling/LambdaTask.h"
#include "utility/scheduling/TaskGroupSequential.h"
std::shared_ptr<MessageQueue> MessageQueue::getInstance()
{
@@ -17,20 +19,10 @@ std::shared_ptr<MessageQueue> MessageQueue::getInstance()
return s_instance;
}
void MessageQueue::registerListener(MessageListenerBase* listener, bool toFront)
void MessageQueue::registerListener(MessageListenerBase* listener)
{
std::lock_guard<std::mutex> lock(m_listenersMutex);
if (toFront)
{
m_listeners.insert(m_listeners.begin(), listener);
m_listenersLength++;
m_currentListenerIndex++;
}
else
{
m_listeners.push_back(listener);
}
m_listeners.push_back(listener);
}
void MessageQueue::unregisterListener(MessageListenerBase* listener)
@@ -156,6 +148,11 @@ bool MessageQueue::hasMessagesQueued() const
return m_backMessageBuffer->size() + m_frontMessageBuffer->size() > 0;
}
void MessageQueue::setSendMessagesAsTasks(bool sendMessagesAsTasks)
{
m_sendMessagesAsTasks = sendMessagesAsTasks;
}
std::shared_ptr<MessageQueue> MessageQueue::s_instance;
MessageQueue::MessageQueue()
@@ -163,6 +160,7 @@ MessageQueue::MessageQueue()
, m_listenersLength(0)
, m_loopIsRunning(false)
, m_threadIsRunning(false)
, m_sendMessagesAsTasks(false)
{
m_frontMessageBuffer = std::make_shared<MessageBufferType>();
m_backMessageBuffer = std::make_shared<MessageBufferType>();
@@ -191,25 +189,60 @@ void MessageQueue::processMessages()
m_frontMessageBuffer->pop();
}
std::lock_guard<std::mutex> lock(m_listenersMutex);
// m_listenersLength is saved, so that new listeners registered whithin message handling don't get the
// current message and the length can be reduced when a listener gets unregistered.
m_listenersLength = m_listeners.size();
// The currentListenerIndex holds the index of the current listener being handled, so it can be changed when a
// listener gets removed while message handling.
for (m_currentListenerIndex = 0; m_currentListenerIndex < m_listenersLength; m_currentListenerIndex++)
if (m_sendMessagesAsTasks && message->sendAsTask())
{
MessageListenerBase* listener = m_listeners[m_currentListenerIndex];
if (listener->getType() == message->getType())
{
// The listenersMutex gets unlocked so changes to listeners are possible while message handling.
m_listenersMutex.unlock();
listener->handleMessageBase(message.get());
m_listenersMutex.lock();
}
sendMessageAsTask(message);
}
else
{
sendMessage(message);
}
}
}
void MessageQueue::sendMessage(std::shared_ptr<MessageBase> message)
{
std::lock_guard<std::mutex> lock(m_listenersMutex);
// m_listenersLength is saved, so that new listeners registered whithin message handling don't get the
// current message and the length can be reduced when a listener gets unregistered.
m_listenersLength = m_listeners.size();
// The currentListenerIndex holds the index of the current listener being handled, so it can be changed when a
// listener gets removed while message handling.
for (m_currentListenerIndex = 0; m_currentListenerIndex < m_listenersLength; m_currentListenerIndex++)
{
MessageListenerBase* listener = m_listeners[m_currentListenerIndex];
if (listener->getType() == message->getType())
{
// The listenersMutex gets unlocked so changes to listeners are possible while message handling.
m_listenersMutex.unlock();
listener->handleMessageBase(message.get());
m_listenersMutex.lock();
}
}
}
void MessageQueue::sendMessageAsTask(std::shared_ptr<MessageBase> message) const
{
std::shared_ptr<TaskGroupSequential> taskGroup = std::make_shared<TaskGroupSequential>();
std::lock_guard<std::mutex> lock(m_listenersMutex);
for (size_t i = 0; i < m_listeners.size(); i++)
{
MessageListenerBase* listener = m_listeners[i];
if (listener->getType() == message->getType())
{
taskGroup->addTask(std::make_shared<LambdaTask>(
[listener, message]()
{
listener->handleMessageBase(message.get());
}
));
}
}
Task::dispatch(taskGroup);
}
+8 -1
View File
@@ -13,7 +13,7 @@ class MessageQueue
public:
static std::shared_ptr<MessageQueue> getInstance();
void registerListener(MessageListenerBase* listener, bool toFront = false);
void registerListener(MessageListenerBase* listener);
void unregisterListener(MessageListenerBase* listener);
void pushMessage(std::shared_ptr<MessageBase> message);
@@ -25,6 +25,8 @@ public:
bool loopIsRunning() const;
bool hasMessagesQueued() const;
void setSendMessagesAsTasks(bool sendMessagesAsTasks);
private:
typedef std::queue<std::shared_ptr<MessageBase>> MessageBufferType;
@@ -35,6 +37,8 @@ private:
void operator=(const MessageQueue&);
void processMessages();
void sendMessage(std::shared_ptr<MessageBase> message);
void sendMessageAsTask(std::shared_ptr<MessageBase> message) const;
std::shared_ptr<MessageBufferType> m_frontMessageBuffer;
std::shared_ptr<MessageBufferType> m_backMessageBuffer;
@@ -42,6 +46,7 @@ private:
size_t m_currentListenerIndex;
size_t m_listenersLength;
bool m_loopIsRunning;
bool m_threadIsRunning;
@@ -50,6 +55,8 @@ private:
mutable std::mutex m_listenersMutex;
mutable std::mutex m_loopMutex;
mutable std::mutex m_threadMutex;
bool m_sendMessagesAsTasks;
};
#endif // MESSAGE_QUEUE_H
@@ -1,26 +0,0 @@
#ifndef MESSAGE_ERROR_H
#define MESSAGE_ERROR_H
#include "utility/messaging/Message.h"
#include "utility/types.h"
class MessageError: public Message<MessageError>
{
public:
MessageError(
const std::string& error
)
: error(error)
{
}
static const std::string getStaticType()
{
return "MessageError";
}
const std::string error;
};
#endif // MESSAGE_ERROR_H
@@ -1,7 +1,11 @@
#ifndef MESSAGE_FINISHED_PARSING_H
#define MESSAGE_FINISHED_PARSING_H
#include <sstream>
#include <iomanip>
#include "utility/messaging/Message.h"
#include "utility/messaging/type/MessageStatus.h"
class MessageFinishedParsing: public Message<MessageFinishedParsing>
{
@@ -19,6 +23,23 @@ public:
return "MessageFinishedParsing";
}
virtual void dispatch()
{
MessageStatus(getStatusStr(), errorCount > 0).dispatch();
Message<MessageFinishedParsing>::dispatch();
}
std::string getStatusStr() const
{
std::stringstream ss;
ss << "Parsing Finished: ";
ss << fileCount << "/" << totalFileCount << " files, ";
ss << std::setprecision(2) << std::fixed << parseTime << " seconds, ";
ss << errorCount << " error(s)";
return ss.str();
}
size_t fileCount;
size_t totalFileCount;
float parseTime;
@@ -9,6 +9,7 @@ class MessageInterruptTasks:
public:
MessageInterruptTasks()
{
setSendAsTask(false);
}
static const std::string getStaticType()
@@ -2,17 +2,16 @@
#define MESSAGE_STATUS_H
#include "utility/messaging/Message.h"
#include "utility/types.h"
class MessageStatus: public Message<MessageStatus>
class MessageStatus
: public Message<MessageStatus>
{
public:
MessageStatus(
const std::string& status
)
MessageStatus(const std::string& status, bool isError = false)
: status(status)
, isError(isError)
{
setSendAsTask(false);
}
static const std::string getStaticType()
@@ -21,6 +20,7 @@ public:
}
const std::string status;
const bool isError;
};
#endif // MESSAGE_STATUS_H