summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacopo Mondi <jacopo@jmondi.org>2019-07-15 07:59:35 +0200
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2019-07-16 10:55:27 +0300
commitfae053307dcc6807dd8ab127294c1fe5c5bb2d72 (patch)
tree90df0625ef86a0552d382e0211ea18535d687fb5
parent3e29a2d520c9cb8c7b290574bceaadcc107678fa (diff)
libcamera: message: Add user message types
Reserve identifiers for user-defined message types and add an operation to the Message class to register the type identifiers. Signed-off-by: Jacopo Mondi <jacopo@jmondi.org> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
-rw-r--r--src/libcamera/include/message.h7
-rw-r--r--src/libcamera/message.cpp36
-rw-r--r--test/message.cpp11
3 files changed, 54 insertions, 0 deletions
diff --git a/src/libcamera/include/message.h b/src/libcamera/include/message.h
index db17d647..416fe74b 100644
--- a/src/libcamera/include/message.h
+++ b/src/libcamera/include/message.h
@@ -7,6 +7,8 @@
#ifndef __LIBCAMERA_MESSAGE_H__
#define __LIBCAMERA_MESSAGE_H__
+#include <atomic>
+
namespace libcamera {
class Object;
@@ -19,6 +21,7 @@ public:
enum Type {
None = 0,
SignalMessage = 1,
+ UserMessage = 1000,
};
Message(Type type);
@@ -27,11 +30,15 @@ public:
Type type() const { return type_; }
Object *receiver() const { return receiver_; }
+ static Type registerMessageType();
+
private:
friend class Thread;
Type type_;
Object *receiver_;
+
+ static std::atomic_uint nextUserType_;
};
class SignalMessage : public Message
diff --git a/src/libcamera/message.cpp b/src/libcamera/message.cpp
index 9f22ad7f..d44d2a4c 100644
--- a/src/libcamera/message.cpp
+++ b/src/libcamera/message.cpp
@@ -31,6 +31,8 @@ namespace libcamera {
LOG_DEFINE_CATEGORY(Message)
+std::atomic_uint Message::nextUserType_{ Message::UserMessage };
+
/**
* \class Message
* \brief A message that can be posted to a Thread
@@ -43,6 +45,8 @@ LOG_DEFINE_CATEGORY(Message)
* \brief Invalid message type
* \var Message::SignalMessage
* \brief Asynchronous signal delivery across threads
+ * \var Message::UserMessage
+ * \brief First value available for user-defined messages
*/
/**
@@ -71,6 +75,38 @@ Message::~Message()
*/
/**
+ * \brief Reserve and register a custom user-defined message type
+ *
+ * Custom message types use values starting at Message::UserMessage. Assigning
+ * custom types manually may lead to accidental duplicated types. To avoid this
+ * problem, this method reserves and returns the next available user-defined
+ * message type.
+ *
+ * The recommended way to use this method is to subclass Message and provide a
+ * static accessor for the custom message type.
+ *
+ * \code{.cpp}
+ * class MyCustomMessage : public Message
+ * {
+ * public:
+ * MyCustomMessage() : Message(type()) { }
+ *
+ * static Message::Type type()
+ * {
+ * static MessageType type = registerMessageType();
+ * return type;
+ * }
+ * };
+ * \endcode
+ *
+ * \return A new unique message type
+ */
+Message::Type Message::registerMessageType()
+{
+ return static_cast<Message::Type>(nextUserType_++);
+}
+
+/**
* \class SignalMessage
* \brief A message carrying a Signal across threads
*/
diff --git a/test/message.cpp b/test/message.cpp
index de98da3e..3775c30a 100644
--- a/test/message.cpp
+++ b/test/message.cpp
@@ -52,6 +52,17 @@ class MessageTest : public Test
protected:
int run()
{
+ Message::Type msgType[2] = {
+ Message::registerMessageType(),
+ Message::registerMessageType(),
+ };
+
+ if (msgType[0] != Message::UserMessage ||
+ msgType[1] != Message::UserMessage + 1) {
+ cout << "Failed to register message types" << endl;
+ return TestFail;
+ }
+
MessageReceiver receiver;
receiver.moveToThread(&thread_);