From patchwork Mon Jul 15 09:29:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 1703 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0F48761A96 for ; Mon, 15 Jul 2019 11:30:14 +0200 (CEST) Received: from pendragon.ideasonboard.com (unknown [IPv6:2a00:79e1:abc:3602:59ec:6c:1869:337]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 03751578 for ; Mon, 15 Jul 2019 11:30:12 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1563183013; bh=4cF1EqFn5BNiznsmEdrfS781jNVOaCGcL+RRGLGmRGE=; h=From:To:Subject:Date:In-Reply-To:References:From; b=pUV96HtD0iswgrmjCAm9RykH7ehl8AQp2x/MWSOnAl4QBtGBF0BfXfa4pquJUElXn u1k+tFZnguRP8AFaoSy+cAKSBS9TNQDIeUlIIlXpJouhhl62LkrKPd+mS00xk9O69a ckUiwAuvEXDcNuA1nvh9mi7EHsdTnvZinAmxCsDI= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Mon, 15 Jul 2019 12:29:39 +0300 Message-Id: <20190715092939.15349-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190715092939.15349-1-laurent.pinchart@ideasonboard.com> References: <20190715092939.15349-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 2/2] libcamera: message: Add user message types X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 15 Jul 2019 09:30:14 -0000 From: Jacopo Mondi Reserve identifiers for user-defined message types and add an operation to the Message class to create register the type identifiers. Signed-off-by: Jacopo Mondi Signed-off-by: Laurent Pinchart Reviewed-by: Jacopo Mondi --- Changes since v1: - Replace mutex with atomic - Rework documentation - Rename registerUserMessageType() to registerMessageType() - Extend the Message test Signed-off-by: Laurent Pinchart --- src/libcamera/include/message.h | 7 +++++++ src/libcamera/message.cpp | 36 +++++++++++++++++++++++++++++++++ test/message.cpp | 11 ++++++++++ 3 files changed, 54 insertions(+) diff --git a/src/libcamera/include/message.h b/src/libcamera/include/message.h index db17d647c280..416fe74b10ad 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 + 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 9f22ad7fc0b0..d44d2a4c73a8 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 */ /** @@ -70,6 +74,38 @@ Message::~Message() * \return The message receiver */ +/** + * \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(nextUserType_++); +} + /** * \class SignalMessage * \brief A message carrying a Signal across threads diff --git a/test/message.cpp b/test/message.cpp index de98da3e8754..3775c30a20b3 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_);