{"id":22665,"url":"https://patchwork.libcamera.org/api/patches/22665/?format=json","web_url":"https://patchwork.libcamera.org/patch/22665/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20250130115001.1129305-7-pobrn@protonmail.com>","date":"2025-01-30T11:50:32","name":"[RFC,v3,06/21] apps: common: event_loop: Add way to cancel deferred calls","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"0c5bb7db721fa2ff761dd5066822681f1dfde92c","submitter":{"id":133,"url":"https://patchwork.libcamera.org/api/people/133/?format=json","name":"Pőcze Barnabás","email":"pobrn@protonmail.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/22665/mbox/","series":[{"id":4978,"url":"https://patchwork.libcamera.org/api/series/4978/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4978","date":"2025-01-30T11:50:08","name":"apps: lc-compliance: Multi-stream tests","version":3,"mbox":"https://patchwork.libcamera.org/series/4978/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/22665/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/22665/checks/","tags":{},"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 C3362BDB1C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 30 Jan 2025 11:50:39 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 824E868574;\n\tThu, 30 Jan 2025 12:50:39 +0100 (CET)","from mail-40131.protonmail.ch (mail-40131.protonmail.ch\n\t[185.70.40.131])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id D11D76856D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 30 Jan 2025 12:50:37 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=protonmail.com header.i=@protonmail.com\n\theader.b=\"q9LjDeYR\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com;\n\ts=protonmail3; t=1738237837; x=1738497037;\n\tbh=++fnX87jtPIaFW6i7xSF2EgMVTu3VhdXA6LIpPZRbU0=;\n\th=Date:To:From:Subject:Message-ID:In-Reply-To:References:\n\tFeedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID:\n\tMessage-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post;\n\tb=q9LjDeYRW3OWdr8kYRQoS+mvLFnu8KKsQUP9N8xM+ALXieMBIx1zS0NxtbC48lgDw\n\tcjMQQpaOKhAWTNsVwewfnOWo2jA0YQvnmzYyiHGlLcSAYmGh3QZG3qjjdzpURkaycO\n\tudT0OmXgvFFDi4V1qnfjPgbcoyRO2THv9UsK3Oy8a9lRkLgYKVVBf2n0pE9/PGMj7D\n\ttg9oim/NfRlg26vDThvZaa9BwVhD4i1B9CZ+y/bzbunBDeVk/PrhxrSuYJ/aJLsvpr\n\tuCshRfFNpI2F4uMTXHepevfQvhM0LW3zWy5zz89PyOD9O2oZLG7+4lnUx93moeflyM\n\tyx5PbKowaDnOg==","Date":"Thu, 30 Jan 2025 11:50:32 +0000","To":"libcamera-devel@lists.libcamera.org","From":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>","Subject":"[RFC PATCH v3 06/21] apps: common: event_loop: Add way to cancel\n\tdeferred calls","Message-ID":"<20250130115001.1129305-7-pobrn@protonmail.com>","In-Reply-To":"<20250130115001.1129305-1-pobrn@protonmail.com>","References":"<20250130115001.1129305-1-pobrn@protonmail.com>","Feedback-ID":"20568564:user:proton","X-Pm-Message-ID":"54d386a6db609a346f81fd4305c4182dabe8b716","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>"},"content":"Store a cookie value of `std::uintptr_t` with the callback to\nmake later cancellation of the callback possible.\n\nSigned-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(-)","diff":"diff --git a/src/apps/common/event_loop.cpp b/src/apps/common/event_loop.cpp\nindex 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 {\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+\t\t     calls_.end());\n+}\n+\n void EventLoop::addFdEvent(int fd, EventType type,\n \t\t\t   std::function<void()> &&callback)\n {\ndiff --git a/src/apps/common/event_loop.h b/src/apps/common/event_loop.h\nindex 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","prefixes":["RFC","v3","06/21"]}