[{"id":31513,"web_url":"https://patchwork.libcamera.org/comment/31513/","msgid":"<_BBPglS8k-P_Jb4JPpLJSGi5bAslp9nytXsARdQ3LZSrx6yabq8YO9ttSqe3ymZ39jKY68hAoAjXt0fwOqPQqAU6zAZQqgkQCZ2LJcINJDk=@protonmail.com>","date":"2024-10-01T18:34:58","subject":"Re: [PATCH 1/1] libcamera: Add mailbox template helper","submitter":{"id":133,"url":"https://patchwork.libcamera.org/api/people/133/","name":"Pőcze Barnabás","email":"pobrn@protonmail.com"},"content":"Hi\n\n\n2024. október 1., kedd 10:10 keltezéssel, Harvey Yang <chenghaoyang@chromium.org> írta:\n\n> From: Han-Lin Chen <hanlinchen@chromium.org>\n> \n> MailBox is designed to be used to shares data, usually a frame buffer, among\n                                         ^\n                                    share\n\n> tasks. A Mailbox can store a data which the producer task should set into,\n               ^\n           MailBox\n\n> and multiple consumer tasks could read the data from it. The data could\n> be set only once, i.e., there should be only one producer. All tasks shares\n                                                                            ^\n                                                                       share\n\n> the Mailbox with share_ptr and the data it contains should be recycled when\n          ^            ^\n      MailBox      shared_ptr\n\n> all tasks are finished.\n> \n> Signed-off-by: Hanlin Chen <hanlinchen@google.com>\n> Co-developed-by: Harvey Yang <chenghaoyang@chromium.org>\n> ---\n>  include/libcamera/internal/mailbox.h   | 50 +++++++++++++\n>  include/libcamera/internal/meson.build |  1 +\n>  src/libcamera/mailbox.cpp              | 97 ++++++++++++++++++++++++++\n>  src/libcamera/meson.build              |  1 +\n>  4 files changed, 149 insertions(+)\n>  create mode 100644 include/libcamera/internal/mailbox.h\n>  create mode 100644 src/libcamera/mailbox.cpp\n> \n> diff --git a/include/libcamera/internal/mailbox.h b/include/libcamera/internal/mailbox.h\n> new file mode 100644\n> index 00000000..fb27f5bb\n> --- /dev/null\n> +++ b/include/libcamera/internal/mailbox.h\n> @@ -0,0 +1,50 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2024, Google Inc.\n> + *\n> + * mailbox.h - Template class for generic mailbox\n> + */\n> +\n> +#pragma once\n> +\n> +#include <functional>\n> +#include <memory>\n> +#include <vector>\n> +\n> +namespace libcamera {\n> +\n> +template<class T>\n> +class MailBox\n> +{\n> +public:\n> +\tusing Recycler = std::function<void(T &)>;\n> +\n> +\tMailBox()\n> +\t\t: valid_(false) {}\n> +\t~MailBox();\n> +\n> +\tvoid put(const T &item, Recycler recycler);\n> +\n> +\tconst T &get();\n> +\n> +\tbool valid() { return valid_; }\n> +\n> +private:\n> +\tT item_;\n> +\tbool valid_;\n> +\tstd::function<void(T &)> recycler_;\n> +};\n> +\n> +template<class T>\n> +using SharedMailBox = std::shared_ptr<MailBox<T>>;\n> +\n> +template<class T>\n> +SharedMailBox<T> makeMailBox()\n> +{\n> +\treturn std::make_shared<MailBox<T>>();\n> +}\n> +\n> +template<class T>\n> +std::vector<SharedMailBox<T>> makeMailBoxVector(const unsigned int count);\n\nYou have to put the definitions of templates in the header file, otherwise it won't work.\n\nAlso, this whole concept looks to me to be very similar to a `const std::shared_ptr<T>`\nwith a custom deleter (with the difference that you can only obtain a `const` reference\nhere unlike with an `std::shared_ptr`). So I am wondering if utilizing `std::shared_ptr`\nwith a custom deleter has been explored already.\n\nI am also wondering if it wouldn't make more sense to remove `MailBox::put()`\nand just have the two constructors taking `const T&` and `T&&`, and initializing\n`item_`. This would do away with the need for the `valid_` member. Is there a\nplanned use case where that is not sufficient?\n\n\nRegards,\nBarnabás Pőcze\n\n\n> +\n> +} /* namespace libcamera */\n> [...]","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 434D9C3257\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  1 Oct 2024 18:35:06 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 23F73618D7;\n\tTue,  1 Oct 2024 20:35:05 +0200 (CEST)","from mail-40133.protonmail.ch (mail-40133.protonmail.ch\n\t[185.70.40.133])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 61CBD60553\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  1 Oct 2024 20:35:03 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key;\n\tunprotected) header.d=protonmail.com header.i=@protonmail.com\n\theader.b=\"sHg4Lq0R\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com;\n\ts=protonmail3; t=1727807702; x=1728066902;\n\tbh=grcHZs5XztkrDTb0+TQE6V9r8EdJaUTRIQRfsXqf2BY=;\n\th=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References:\n\tFeedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID:\n\tMessage-ID:BIMI-Selector;\n\tb=sHg4Lq0RcHpmKrFBPKd06yEAO6V1747kSxr/oFn+Udbbfw1CLYAlLZQ//MebpBacV\n\tSybP/sTJEvWrCum5ga+LXzaJo4tmXx8MdS6cbtlxoLirqL5XLywxMoV5lRDt599Q5t\n\t2sQSygvyYY40ohS+DK8CUM5cQEf+yDYTNNVLEWNp8CwlT7EffHwW2WJr4JkoEd0B0F\n\tdn+Yi0CVFJRmFT/sApxthulWtUlxs3GtFTZ7Y5DtwiVDIn0OqlMduovwrTJJVnoPbd\n\tM3EONzvb2qOta0wlOp9fKgiRlz7wGRCuIIhP9Jp1XBZm+v8vbLKNu3oAbgG2prHUEd\n\t92W35ubD6BizQ==","Date":"Tue, 01 Oct 2024 18:34:58 +0000","To":"Harvey Yang <chenghaoyang@chromium.org>","From":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>","Cc":"libcamera-devel@lists.libcamera.org,\n\tHan-Lin Chen <hanlinchen@chromium.org>, \n\tHanlin Chen <hanlinchen@google.com>","Subject":"Re: [PATCH 1/1] libcamera: Add mailbox template helper","Message-ID":"<_BBPglS8k-P_Jb4JPpLJSGi5bAslp9nytXsARdQ3LZSrx6yabq8YO9ttSqe3ymZ39jKY68hAoAjXt0fwOqPQqAU6zAZQqgkQCZ2LJcINJDk=@protonmail.com>","In-Reply-To":"<20241001081824.960009-2-chenghaoyang@google.com>","References":"<20241001081824.960009-1-chenghaoyang@google.com>\n\t<20241001081824.960009-2-chenghaoyang@google.com>","Feedback-ID":"20568564:user:proton","X-Pm-Message-ID":"9af8603168b91a0ce9141d747dfcda60a7b52578","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Transfer-Encoding":"quoted-printable","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]