[{"id":2275,"web_url":"https://patchwork.libcamera.org/comment/2275/","msgid":"<20190715085944.GA11178@pendragon.ideasonboard.com>","date":"2019-07-15T08:59:44","subject":"Re: [libcamera-devel] [PATCH 3/3] libcamera: message: Add user\n\tmessage types","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Jacopo,\n\nThank you for the patch.\n\nOn Mon, Jul 15, 2019 at 07:59:35AM +0200, Jacopo Mondi wrote:\n> Add an operation to the Message class to create unique message type to\n> be used by derived classes to uniquely identify the messages they\n> handle.\n> \n> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>\n> ---\n>  src/libcamera/include/message.h |  8 ++++++++\n>  src/libcamera/message.cpp       | 20 ++++++++++++++++++++\n>  2 files changed, 28 insertions(+)\n> \n> diff --git a/src/libcamera/include/message.h b/src/libcamera/include/message.h\n> index db17d647c280..e7dd85a606a3 100644\n> --- a/src/libcamera/include/message.h\n> +++ b/src/libcamera/include/message.h\n> @@ -7,6 +7,8 @@\n>  #ifndef __LIBCAMERA_MESSAGE_H__\n>  #define __LIBCAMERA_MESSAGE_H__\n>  \n> +#include <mutex>\n> +\n>  namespace libcamera {\n>  \n>  class Object;\n> @@ -19,6 +21,7 @@ public:\n>  \tenum Type {\n>  \t\tNone = 0,\n>  \t\tSignalMessage = 1,\n> +\t\tUserMessage = 1000,\n\nThis should be documented, and it may make sense to split it to a\nseparate patch.\n\n>  \t};\n>  \n>  \tMessage(Type type);\n> @@ -27,11 +30,16 @@ public:\n>  \tType type() const { return type_; }\n>  \tObject *receiver() const { return receiver_; }\n>  \n> +\tstatic Type registerUserMessageType();\n\nI think we can shorten the name to registerMessageType().\n\n> +\n>  private:\n>  \tfriend class Thread;\n>  \n>  \tType type_;\n>  \tObject *receiver_;\n> +\n> +\tstatic std::mutex mutex_;\n> +\tstatic Type nextUserType_;\n\nI wonder if we could use an atomic variable instead and remove the\nmutex.\n\n\tstatic std::atomic_uint nextUserType_;\n\n>  };\n>  \n>  class SignalMessage : public Message\n> diff --git a/src/libcamera/message.cpp b/src/libcamera/message.cpp\n> index 0580c1051622..f82a3115595d 100644\n> --- a/src/libcamera/message.cpp\n> +++ b/src/libcamera/message.cpp\n> @@ -6,6 +6,7 @@\n>   */\n>  \n>  #include \"message.h\"\n> +#include \"thread.h\"\n>  \n>  #include \"log.h\"\n>  \n> @@ -31,6 +32,9 @@ namespace libcamera {\n>  \n>  LOG_DEFINE_CATEGORY(Message)\n>  \n> +std::mutex Message::mutex_;\n> +Message::Type Message::nextUserType_ = Message::UserMessage;\n> +\n>  /**\n>   * \\class Message\n>   * \\brief A message that can be posted to a Thread\n> @@ -68,6 +72,22 @@ Message::~Message()\n>   * \\return The message receiver\n>   */\n>  \n> +/**\n> + * \\brief Retrieve a unique identifier for user defined message types\n\nReserve and register a custom user-defined message type\n\n> + *\n> + * Derived classes can define their own message types and this method provides\n> + * them a unique identifier to use for that purpose.\n\nHow about this ?\n\n * Custom message types use values starting at Type::UserMessage. Assigning\n * custom types manually may lead to accidental duplicated types. To avoid this\n * problem, this method reserves and returns the next available user-defined\n * message type.\n *\n * The recommended way to use this method is to subclass Message and provide a\n * static accessor for the custom message type.\n *\n * \\code{.cpp}\n * class MyCustomMessage : public Message\n * {\n * public:\n *      MyCustomMessage() : Message(type()) { }\n *\n *      static Message::Type type()\n *      {\n *              static MessageType type = registerMessageType();\n *              return type;\n *      }\n * };\n * \\endcode\n\n> + *\n> + * \\return A new unique message type\n> + */\n> +Message::Type Message::registerUserMessageType()\n> +{\n> +\tMutexLocker locker(mutex_);\n> +\n> +\treturn nextUserType_ = static_cast<Type>\n> +\t\t\t      (static_cast<unsigned int>(nextUserType_) + 1);\n\n\treturn static_cast<Message::Type>(nextUserType_++);\n\n> +}\n> +\n>  /**\n>   * \\class SignalMessage\n>   * \\brief A message carrying a Signal across threads","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 827B861572\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 15 Jul 2019 11:00:13 +0200 (CEST)","from pendragon.ideasonboard.com (unknown\n\t[IPv6:2a00:79e1:abc:3602:59ec:6c:1869:337])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id BD28F578;\n\tMon, 15 Jul 2019 11:00:11 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1563181212;\n\tbh=Ql3Xddopi+JL2j7sQkZVofR5eIq5UbI/Gng0XQ7OerI=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=FIrSPutoTBxeNqwzADsrMrzqHwARnoU//PhziNeb2QjuG2NxXjZU8cZ7kbzAlqbTa\n\tqq6jUNezSBYNzi/ZFVto9HXZS56fKletrPT+5afjvFGwEtY6XojmkrFybSO1IrWrfz\n\tN7yLEdQOTb67LURlBX9DOXvjRDUT/djp4Y8ExTUY=","Date":"Mon, 15 Jul 2019 11:59:44 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jacopo Mondi <jacopo@jmondi.org>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190715085944.GA11178@pendragon.ideasonboard.com>","References":"<20190715055935.21233-1-jacopo@jmondi.org>\n\t<20190715055935.21233-4-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20190715055935.21233-4-jacopo@jmondi.org>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH 3/3] libcamera: message: Add user\n\tmessage types","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Mon, 15 Jul 2019 09:00:13 -0000"}},{"id":2276,"web_url":"https://patchwork.libcamera.org/comment/2276/","msgid":"<20190715091726.GB11178@pendragon.ideasonboard.com>","date":"2019-07-15T09:17:26","subject":"Re: [libcamera-devel] [PATCH 3/3] libcamera: message: Add user\n\tmessage types","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Jacopo,\n\nOn Mon, Jul 15, 2019 at 11:59:44AM +0300, Laurent Pinchart wrote:\n> On Mon, Jul 15, 2019 at 07:59:35AM +0200, Jacopo Mondi wrote:\n> > Add an operation to the Message class to create unique message type to\n> > be used by derived classes to uniquely identify the messages they\n> > handle.\n> > \n> > Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>\n> > ---\n> >  src/libcamera/include/message.h |  8 ++++++++\n> >  src/libcamera/message.cpp       | 20 ++++++++++++++++++++\n> >  2 files changed, 28 insertions(+)\n> > \n> > diff --git a/src/libcamera/include/message.h b/src/libcamera/include/message.h\n> > index db17d647c280..e7dd85a606a3 100644\n> > --- a/src/libcamera/include/message.h\n> > +++ b/src/libcamera/include/message.h\n> > @@ -7,6 +7,8 @@\n> >  #ifndef __LIBCAMERA_MESSAGE_H__\n> >  #define __LIBCAMERA_MESSAGE_H__\n> >  \n> > +#include <mutex>\n> > +\n> >  namespace libcamera {\n> >  \n> >  class Object;\n> > @@ -19,6 +21,7 @@ public:\n> >  \tenum Type {\n> >  \t\tNone = 0,\n> >  \t\tSignalMessage = 1,\n> > +\t\tUserMessage = 1000,\n> \n> This should be documented, and it may make sense to split it to a\n> separate patch.\n\nNevermind splitting it to another patch, it's fine to combine them, but\nit should still be documented.\n\n> >  \t};\n> >  \n> >  \tMessage(Type type);\n> > @@ -27,11 +30,16 @@ public:\n> >  \tType type() const { return type_; }\n> >  \tObject *receiver() const { return receiver_; }\n> >  \n> > +\tstatic Type registerUserMessageType();\n> \n> I think we can shorten the name to registerMessageType().\n> \n> > +\n> >  private:\n> >  \tfriend class Thread;\n> >  \n> >  \tType type_;\n> >  \tObject *receiver_;\n> > +\n> > +\tstatic std::mutex mutex_;\n> > +\tstatic Type nextUserType_;\n> \n> I wonder if we could use an atomic variable instead and remove the\n> mutex.\n> \n> \tstatic std::atomic_uint nextUserType_;\n> \n> >  };\n> >  \n> >  class SignalMessage : public Message\n> > diff --git a/src/libcamera/message.cpp b/src/libcamera/message.cpp\n> > index 0580c1051622..f82a3115595d 100644\n> > --- a/src/libcamera/message.cpp\n> > +++ b/src/libcamera/message.cpp\n> > @@ -6,6 +6,7 @@\n> >   */\n> >  \n> >  #include \"message.h\"\n> > +#include \"thread.h\"\n> >  \n> >  #include \"log.h\"\n> >  \n> > @@ -31,6 +32,9 @@ namespace libcamera {\n> >  \n> >  LOG_DEFINE_CATEGORY(Message)\n> >  \n> > +std::mutex Message::mutex_;\n> > +Message::Type Message::nextUserType_ = Message::UserMessage;\n> > +\n> >  /**\n> >   * \\class Message\n> >   * \\brief A message that can be posted to a Thread\n> > @@ -68,6 +72,22 @@ Message::~Message()\n> >   * \\return The message receiver\n> >   */\n> >  \n> > +/**\n> > + * \\brief Retrieve a unique identifier for user defined message types\n> \n> Reserve and register a custom user-defined message type\n> \n> > + *\n> > + * Derived classes can define their own message types and this method provides\n> > + * them a unique identifier to use for that purpose.\n> \n> How about this ?\n> \n>  * Custom message types use values starting at Type::UserMessage. Assigning\n>  * custom types manually may lead to accidental duplicated types. To avoid this\n>  * problem, this method reserves and returns the next available user-defined\n>  * message type.\n>  *\n>  * The recommended way to use this method is to subclass Message and provide a\n>  * static accessor for the custom message type.\n>  *\n>  * \\code{.cpp}\n>  * class MyCustomMessage : public Message\n>  * {\n>  * public:\n>  *      MyCustomMessage() : Message(type()) { }\n>  *\n>  *      static Message::Type type()\n>  *      {\n>  *              static MessageType type = registerMessageType();\n>  *              return type;\n>  *      }\n>  * };\n>  * \\endcode\n> \n> > + *\n> > + * \\return A new unique message type\n> > + */\n> > +Message::Type Message::registerUserMessageType()\n> > +{\n> > +\tMutexLocker locker(mutex_);\n> > +\n> > +\treturn nextUserType_ = static_cast<Type>\n> > +\t\t\t      (static_cast<unsigned int>(nextUserType_) + 1);\n> \n> \treturn static_cast<Message::Type>(nextUserType_++);\n> \n> > +}\n> > +\n> >  /**\n> >   * \\class SignalMessage\n> >   * \\brief A message carrying a Signal across threads\n>","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E6F6B61572\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 15 Jul 2019 11:17:54 +0200 (CEST)","from pendragon.ideasonboard.com (unknown\n\t[IPv6:2a00:79e1:abc:3602:59ec:6c:1869:337])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id C4363578;\n\tMon, 15 Jul 2019 11:17:53 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1563182274;\n\tbh=vsIxJ/PcumTqX5vC7AvDvRPj4CSy6TzTRyacTyIc1v4=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=Bn8MDCWz28zhxPD+ilVcYmq34xmAmU5vp9riMKdEiCkfQYntxfJp3nt1QZiniJtzH\n\tAvXvPMKIHawhf3/eqggDfyjFpjPeNML7Kd++ffYjMAxCYj9jBq3/TDehWdQOqjgeYw\n\tfOrV0AqhItMiT0AG0OpSG5L17L7rScIkKvrsmwyU=","Date":"Mon, 15 Jul 2019 12:17:26 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jacopo Mondi <jacopo@jmondi.org>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190715091726.GB11178@pendragon.ideasonboard.com>","References":"<20190715055935.21233-1-jacopo@jmondi.org>\n\t<20190715055935.21233-4-jacopo@jmondi.org>\n\t<20190715085944.GA11178@pendragon.ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20190715085944.GA11178@pendragon.ideasonboard.com>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH 3/3] libcamera: message: Add user\n\tmessage types","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Mon, 15 Jul 2019 09:17:55 -0000"}}]