[{"id":3366,"web_url":"https://patchwork.libcamera.org/comment/3366/","msgid":"<20200107191658.GK533370@oden.dyn.berto.se>","date":"2020-01-07T19:16:58","subject":"Re: [libcamera-devel] [PATCH 09/14] libcamera: bound_method: Manage\n\tBoundMethodPack through std::shared_ptr","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"content":"Hi Laurent,\n\nThanks for your work.\n\nOn 2020-01-04 07:09:42 +0200, Laurent Pinchart wrote:\n> The bound method arguments pack will need to be accessed by the method\n> invoker in order to retrieve the method return value when using a\n> blocking connection type. We thus can't delete the pack unconditionally\n> in the bound method target thread. We also can't delete it\n> unconditionally in the invoker's thread, as for queued connections the\n> pack will be used in the target thread after the invoker completes.\n> \n> This shows that ownership of the arguments pack is shared between two\n> contexts. As a result, manage it using std::shared_ptr<>.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n\n> ---\n>  Documentation/Doxyfile.in        |  1 +\n>  include/libcamera/bound_method.h | 54 +++++++++++++++++++-------------\n>  src/libcamera/bound_method.cpp   |  5 +--\n>  src/libcamera/include/message.h  |  5 +--\n>  src/libcamera/message.cpp        |  5 +--\n>  5 files changed, 43 insertions(+), 27 deletions(-)\n> \n> diff --git a/Documentation/Doxyfile.in b/Documentation/Doxyfile.in\n> index 9e5efae33919..5ae8773bd3ad 100644\n> --- a/Documentation/Doxyfile.in\n> +++ b/Documentation/Doxyfile.in\n> @@ -874,6 +874,7 @@ EXCLUDE_SYMBOLS        = libcamera::BoundMemberMethod \\\n>                           libcamera::BoundMethodArgs \\\n>                           libcamera::BoundMethodBase \\\n>                           libcamera::BoundMethodPack \\\n> +                         libcamera::BoundMethodPackBase \\\n>                           libcamera::BoundStaticMethod \\\n>                           libcamera::SignalBase \\\n>                           std::*\n> diff --git a/include/libcamera/bound_method.h b/include/libcamera/bound_method.h\n> index b50072ffc098..a74d2c503cdb 100644\n> --- a/include/libcamera/bound_method.h\n> +++ b/include/libcamera/bound_method.h\n> @@ -7,6 +7,7 @@\n>  #ifndef __LIBCAMERA_BOUND_METHOD_H__\n>  #define __LIBCAMERA_BOUND_METHOD_H__\n>  \n> +#include <memory>\n>  #include <tuple>\n>  #include <type_traits>\n>  \n> @@ -21,6 +22,24 @@ enum ConnectionType {\n>  \tConnectionTypeBlocking,\n>  };\n>  \n> +class BoundMethodPackBase\n> +{\n> +public:\n> +\tvirtual ~BoundMethodPackBase() {}\n> +};\n> +\n> +template<typename... Args>\n> +class BoundMethodPack : public BoundMethodPackBase\n> +{\n> +public:\n> +\tBoundMethodPack(const Args &... args)\n> +\t\t: args_(args...)\n> +\t{\n> +\t}\n> +\n> +\tstd::tuple<typename std::remove_reference<Args>::type...> args_;\n> +};\n> +\n>  class BoundMethodBase\n>  {\n>  public:\n> @@ -36,7 +55,7 @@ public:\n>  \n>  \tObject *object() const { return object_; }\n>  \n> -\tvirtual void invokePack(void *pack) = 0;\n> +\tvirtual void invokePack(BoundMethodPackBase *pack) = 0;\n>  \n>  protected:\n>  #ifndef __DOXYGEN__\n> @@ -58,7 +77,8 @@ protected:\n>  \t};\n>  #endif\n>  \n> -\tvoid activatePack(void *pack, bool deleteMethod);\n> +\tvoid activatePack(std::shared_ptr<BoundMethodPackBase> pack,\n> +\t\t\t  bool deleteMethod);\n>  \n>  \tvoid *obj_;\n>  \tObject *object_;\n> @@ -67,18 +87,6 @@ private:\n>  \tConnectionType connectionType_;\n>  };\n>  \n> -template<typename... Args>\n> -class BoundMethodPack\n> -{\n> -public:\n> -\tBoundMethodPack(const Args &... args)\n> -\t\t: args_(args...)\n> -\t{\n> -\t}\n> -\n> -\tstd::tuple<typename std::remove_reference<Args>::type...> args_;\n> -};\n> -\n>  template<typename R, typename... Args>\n>  class BoundMethodArgs : public BoundMethodBase\n>  {\n> @@ -87,18 +95,18 @@ public:\n>  \n>  private:\n>  \ttemplate<int... S>\n> -\tvoid invokePack(void *pack, BoundMethodBase::sequence<S...>)\n> +\tvoid invokePack(BoundMethodPackBase *pack, BoundMethodBase::sequence<S...>)\n>  \t{\n> -\t\tPackType *args = static_cast<PackType *>(pack);\n> +\t\t/* args is effectively unused when the sequence S is empty. */\n> +\t\tPackType *args [[gnu::unused]] = static_cast<PackType *>(pack);\n>  \t\tinvoke(std::get<S>(args->args_)...);\n> -\t\tdelete args;\n>  \t}\n>  \n>  public:\n>  \tBoundMethodArgs(void *obj, Object *object, ConnectionType type)\n>  \t\t: BoundMethodBase(obj, object, type) {}\n>  \n> -\tvoid invokePack(void *pack) override\n> +\tvoid invokePack(BoundMethodPackBase *pack) override\n>  \t{\n>  \t\tinvokePack(pack, typename BoundMethodBase::generator<sizeof...(Args)>::type());\n>  \t}\n> @@ -123,10 +131,14 @@ public:\n>  \n>  \tvoid activate(Args... args, bool deleteMethod = false) override\n>  \t{\n> -\t\tif (this->object_)\n> -\t\t\tBoundMethodBase::activatePack(new PackType{ args... }, deleteMethod);\n> -\t\telse\n> +\t\tif (!this->object_) {\n>  \t\t\t(static_cast<T *>(this->obj_)->*func_)(args...);\n> +\t\t\treturn;\n> +\t\t}\n> +\n> +\t\tstd::shared_ptr<BoundMethodPackBase> pack =\n> +\t\t\tstd::make_shared<typename BoundMemberMethod<T, R, Args...>::PackType>(args...);\n> +\t\tBoundMethodBase::activatePack(pack, deleteMethod);\n>  \t}\n>  \n>  \tvoid invoke(Args... args) override\n> diff --git a/src/libcamera/bound_method.cpp b/src/libcamera/bound_method.cpp\n> index 45c765774801..82339b7e9c95 100644\n> --- a/src/libcamera/bound_method.cpp\n> +++ b/src/libcamera/bound_method.cpp\n> @@ -48,7 +48,8 @@ namespace libcamera {\n>   * deadlock will occur.\n>   */\n>  \n> -void BoundMethodBase::activatePack(void *pack, bool deleteMethod)\n> +void BoundMethodBase::activatePack(std::shared_ptr<BoundMethodPackBase> pack,\n> +\t\t\t\t   bool deleteMethod)\n>  {\n>  \tConnectionType type = connectionType_;\n>  \tif (type == ConnectionTypeAuto) {\n> @@ -61,7 +62,7 @@ void BoundMethodBase::activatePack(void *pack, bool deleteMethod)\n>  \tswitch (type) {\n>  \tcase ConnectionTypeDirect:\n>  \tdefault:\n> -\t\tinvokePack(pack);\n> +\t\tinvokePack(pack.get());\n>  \t\tif (deleteMethod)\n>  \t\t\tdelete this;\n>  \t\tbreak;\n> diff --git a/src/libcamera/include/message.h b/src/libcamera/include/message.h\n> index 311755cc60fe..8e8b013dcd18 100644\n> --- a/src/libcamera/include/message.h\n> +++ b/src/libcamera/include/message.h\n> @@ -48,7 +48,8 @@ private:\n>  class InvokeMessage : public Message\n>  {\n>  public:\n> -\tInvokeMessage(BoundMethodBase *method, void *pack,\n> +\tInvokeMessage(BoundMethodBase *method,\n> +\t\t      std::shared_ptr<BoundMethodPackBase> pack,\n>  \t\t      Semaphore *semaphore = nullptr,\n>  \t\t      bool deleteMethod = false);\n>  \t~InvokeMessage();\n> @@ -59,7 +60,7 @@ public:\n>  \n>  private:\n>  \tBoundMethodBase *method_;\n> -\tvoid *pack_;\n> +\tstd::shared_ptr<BoundMethodPackBase> pack_;\n>  \tSemaphore *semaphore_;\n>  \tbool deleteMethod_;\n>  };\n> diff --git a/src/libcamera/message.cpp b/src/libcamera/message.cpp\n> index c35bb33dfeda..77f2bdd5fbac 100644\n> --- a/src/libcamera/message.cpp\n> +++ b/src/libcamera/message.cpp\n> @@ -123,7 +123,8 @@ Message::Type Message::registerMessageType()\n>   * \\param[in] deleteMethod True to delete the \\a method when the message is\n>   * destroyed\n>   */\n> -InvokeMessage::InvokeMessage(BoundMethodBase *method, void *pack,\n> +InvokeMessage::InvokeMessage(BoundMethodBase *method,\n> +\t\t\t     std::shared_ptr<BoundMethodPackBase> pack,\n>  \t\t\t     Semaphore *semaphore, bool deleteMethod)\n>  \t: Message(Message::InvokeMessage), method_(method), pack_(pack),\n>  \t  semaphore_(semaphore), deleteMethod_(deleteMethod)\n> @@ -148,7 +149,7 @@ InvokeMessage::~InvokeMessage()\n>   */\n>  void InvokeMessage::invoke()\n>  {\n> -\tmethod_->invokePack(pack_);\n> +\tmethod_->invokePack(pack_.get());\n>  }\n>  \n>  /**\n> -- \n> Regards,\n> \n> Laurent Pinchart\n> \n> _______________________________________________\n> libcamera-devel mailing list\n> libcamera-devel@lists.libcamera.org\n> https://lists.libcamera.org/listinfo/libcamera-devel","headers":{"Return-Path":"<niklas.soderlund@ragnatech.se>","Received":["from mail-lj1-x235.google.com (mail-lj1-x235.google.com\n\t[IPv6:2a00:1450:4864:20::235])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 5F4176063B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  7 Jan 2020 20:17:00 +0100 (CET)","by mail-lj1-x235.google.com with SMTP id m26so680939ljc.13\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 07 Jan 2020 11:17:00 -0800 (PST)","from localhost (h-93-159.A463.priv.bahnhof.se. [46.59.93.159])\n\tby smtp.gmail.com with ESMTPSA id\n\tq186sm230481ljq.14.2020.01.07.11.16.59\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tTue, 07 Jan 2020 11:16:59 -0800 (PST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=ragnatech-se.20150623.gappssmtp.com; s=20150623;\n\th=date:from:to:cc:subject:message-id:references:mime-version\n\t:content-disposition:content-transfer-encoding:in-reply-to;\n\tbh=gFFsz8H2/1j6ZAnZUF+bhyBdr2O4p/1CjEfKvX+TZTs=;\n\tb=FJkMAfL74lEnr44KkJsPcxa+WNvw5u4EZY/UQGMM8nrEiFiV7RX0omficuGzPeejQu\n\t8bT5v7tbbwrHixwB7tTyf/dfZffdfGPhCwo2PpBWfYv1xH45cTd7BumZGPk3DG1omENZ\n\ttm3FwEndXD/ccVd28BwCO4ACbPk+BEg7tvWl6gHmQ6kXYspR02uAWNEp4pGLGfXw/Rcj\n\th98Q9EAks4X9UjVH4KUkKx5sX2EQ1tq9xZ8AWKVIhzjsdA1ppa6nMyDAounTKT5KlOmc\n\t9TMXFeItYhm4le+535cSIfXumPRh1vpGTSLYY76JHWczB+hTyBpHXC3NHitR3ftAQi0p\n\ty9NQ==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:date:from:to:cc:subject:message-id:references\n\t:mime-version:content-disposition:content-transfer-encoding\n\t:in-reply-to;\n\tbh=gFFsz8H2/1j6ZAnZUF+bhyBdr2O4p/1CjEfKvX+TZTs=;\n\tb=m/pjiLZ304YZkQnnCTbBTgybL2ZCBBNaHa/g9kcHMtZnMUwP3k8eFCBSARVna857VQ\n\ttFojjaN/JxqhIFW7VJ2zxQdyLgiovp/nDI8wHdZfgm9xuz6EykfmP1Pe0TYz3l6k1c81\n\tYKWlJNEtCv+97ec/ufxo0j4tyRMUwBUQMIl4nwNnX97uyRIjPWm4VinIAOxu63uHZ0cz\n\tJ51vAnEMPjCm9U21be2wXtuGIVXqZKmUCkgw4dNI0PMHVNwIx6KQGCMKrLqsAy7TY4iA\n\tkcjbRhqZdF4EDrME1UBpM4IFVTtKOWRIpuEd1T79nJntIoqyMzKSoNpJGoDSoImF7KYi\n\t1Ldg==","X-Gm-Message-State":"APjAAAUAZeJmcgWMjeJYlQVzcQQ0ENlMDAU88RpT2PGdh+odLV9DMhWd\n\t3C77vfPApFmBSfcBJkkN/YR/alnYF0nSqQ==","X-Google-Smtp-Source":"APXvYqzIEYKKiNc41JhJWcYFPaABjd4N2CWCdylnbvQ9itxd8pRW4i6BMdRMr2RSBkx5flfD9RZvGQ==","X-Received":"by 2002:a05:651c:8f:: with SMTP id\n\t15mr562800ljq.109.1578424619746; \n\tTue, 07 Jan 2020 11:16:59 -0800 (PST)","Date":"Tue, 7 Jan 2020 20:16:58 +0100","From":"Niklas =?iso-8859-1?q?S=F6derlund?= <niklas.soderlund@ragnatech.se>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20200107191658.GK533370@oden.dyn.berto.se>","References":"<20200104050947.7673-1-laurent.pinchart@ideasonboard.com>\n\t<20200104050947.7673-10-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=iso-8859-1","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20200104050947.7673-10-laurent.pinchart@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH 09/14] libcamera: bound_method: Manage\n\tBoundMethodPack through std::shared_ptr","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","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":"Tue, 07 Jan 2020 19:17:00 -0000"}}]