[{"id":33283,"web_url":"https://patchwork.libcamera.org/comment/33283/","msgid":"<ookxd2fjf6z6ml7y3m2kiq4gmbd365uk5lxy2dapk66lyrvvu6@sk6jbmomo3kw>","date":"2025-02-04T10:00:18","subject":"Re: [RFC PATCH v3 06/21] apps: common: event_loop: Add way to cancel\n\tdeferred calls","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/people/143/","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"content":"Hi Barnabás\n\nOn Thu, Jan 30, 2025 at 11:50:32AM +0000, Barnabás Pőcze wrote:\n> Store a cookie value of `std::uintptr_t` with the callback to\n> make later cancellation of the callback possible.\n>\n> Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>\n> ---\n>  src/apps/common/event_loop.cpp | 18 ++++++++++++++----\n>  src/apps/common/event_loop.h   |  6 ++++--\n>  2 files changed, 18 insertions(+), 6 deletions(-)\n>\n> diff --git a/src/apps/common/event_loop.cpp b/src/apps/common/event_loop.cpp\n> index b6230f4ba..0d7a4a024 100644\n> --- a/src/apps/common/event_loop.cpp\n> +++ b/src/apps/common/event_loop.cpp\n> @@ -10,6 +10,7 @@\n>  #include <assert.h>\n>  #include <event2/event.h>\n>  #include <event2/thread.h>\n> +#include <algorithm>\n>  #include <iostream>\n>\n>  EventLoop *EventLoop::instance_ = nullptr;\n> @@ -33,7 +34,7 @@ EventLoop::EventLoop()\n>  \t\t\t\tif (self->calls_.empty())\n>  \t\t\t\t\tbreak;\n>\n> -\t\t\t\tcall = std::move(self->calls_.front());\n> +\t\t\t\tcall = std::move(self->calls_.front().first);\n>  \t\t\t\tself->calls_.pop_front();\n>  \t\t\t}\n>\n> @@ -73,16 +74,25 @@ void EventLoop::exit(int code)\n>  \tevent_base_loopbreak(base_);\n>  }\n>\n> -void EventLoop::callLater(std::function<void()> &&func)\n> +void EventLoop::callLater(std::function<void()> &&func, std::optional<std::uintptr_t> cookie)\n\nIs this a good idea to make cookie optional ?\nAs far as I can see !cookie means all pending callbacks are deleted\n\n>  {\n>  \t{\n> -\t\tstd::unique_lock<std::mutex> locker(lock_);\n> -\t\tcalls_.push_back(std::move(func));\n> +\t\tstd::lock_guard locker(lock_);\n> +\t\tcalls_.emplace_back(std::move(func), cookie);\n>  \t}\n>\n>  \tevent_active(callsTrigger_, 0, 0);\n>  }\n>\n> +void EventLoop::cancelLater(std::optional<std::uintptr_t> cookie)\n> +{\n> +\tstd::lock_guard locker(lock_);\n> +\n> +\tcalls_.erase(std::remove_if(calls_.begin(), calls_.end(),\n> +\t\t\t\t    [&](const auto &x) { return !cookie || x.second == *cookie; }),\n\nCan x.second be invalid ?\n\nI guess so looking at the call of\n\nsrc/apps/cam/camera_session.cpp:\nEventLoop::instance()->callLater([this, request]() { processRequest(request); });\n\nMaking the cookie (which is a reference to the caller, that's it)\nmandatory would make the code simpler ?\n\nAlso why do you need to dereference cookie here ? Can't you just\ncompare the two optional<> ?\n\nThanks\n  j\n\n\n> +\t\t     calls_.end());\n> +}\n> +\n>  void EventLoop::addFdEvent(int fd, EventType type,\n>  \t\t\t   std::function<void()> &&callback)\n>  {\n> diff --git a/src/apps/common/event_loop.h b/src/apps/common/event_loop.h\n> index 507023996..6d7d0497a 100644\n> --- a/src/apps/common/event_loop.h\n> +++ b/src/apps/common/event_loop.h\n> @@ -13,6 +13,7 @@\n>  #include <list>\n>  #include <memory>\n>  #include <mutex>\n> +#include <optional>\n>\n>  #include <libcamera/base/class.h>\n>\n> @@ -36,7 +37,8 @@ public:\n>  \tint exec();\n>  \tvoid exit(int code = 0);\n>\n> -\tvoid callLater(std::function<void()> &&func);\n> +\tvoid callLater(std::function<void()> &&func, std::optional<std::uintptr_t> cookie = {});\n> +\tvoid cancelLater(std::optional<std::uintptr_t> cookie = {});\n>\n>  \tvoid addFdEvent(int fd, EventType type,\n>  \t\t\tstd::function<void()> &&handler);\n> @@ -63,7 +65,7 @@ private:\n>  \tstruct event_base *base_;\n>  \tint exitCode_;\n>\n> -\tstd::deque<std::function<void()>> calls_;\n> +\tstd::deque<std::pair<std::function<void()>, std::optional<std::uintptr_t>>> calls_;\n>  \t::event *callsTrigger_ = nullptr;\n>\n>  \tstd::list<std::unique_ptr<Event>> events_;\n> --\n> 2.48.1\n>\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 846FAC32F1\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  4 Feb 2025 10:00:25 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 635B9685AE;\n\tTue,  4 Feb 2025 11:00:24 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id D462261870\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  4 Feb 2025 11:00:21 +0100 (CET)","from ideasonboard.com (93-61-96-190.ip145.fastwebnet.it\n\t[93.61.96.190])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 93FDB227;\n\tTue,  4 Feb 2025 10:59:09 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"WyagLn2T\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1738663149;\n\tbh=xVCuk6N0TGdJ+2QVWILzkfZp/3D/enlxHB5b4ApocLc=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=WyagLn2TZYsZ7O4PX3/5LOwalUqCr/zlpAp+RnHH0PbVfc0/DAUYgB8mLPLImQa1B\n\taP2wE5nmXwoOmgEoMdlZ/xHMFueS5DGWNoaXM1W8mNq9c4MCYMUm9JI7XiEAe2i0AP\n\t2yAV+GQtgVeu1a36V4tPZ7qQJjfEn4H1Fm4v/FzQ=","Date":"Tue, 4 Feb 2025 11:00:18 +0100","From":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","To":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [RFC PATCH v3 06/21] apps: common: event_loop: Add way to cancel\n\tdeferred calls","Message-ID":"<ookxd2fjf6z6ml7y3m2kiq4gmbd365uk5lxy2dapk66lyrvvu6@sk6jbmomo3kw>","References":"<20250130115001.1129305-1-pobrn@protonmail.com>\n\t<20250130115001.1129305-7-pobrn@protonmail.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20250130115001.1129305-7-pobrn@protonmail.com>","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>"}}]