From patchwork Thu Jan 30 11:50:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 22665 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id C3362BDB1C for ; Thu, 30 Jan 2025 11:50:39 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 824E868574; Thu, 30 Jan 2025 12:50:39 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=protonmail.com header.i=@protonmail.com header.b="q9LjDeYR"; dkim-atps=neutral Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D11D76856D for ; Thu, 30 Jan 2025 12:50:37 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1738237837; x=1738497037; bh=++fnX87jtPIaFW6i7xSF2EgMVTu3VhdXA6LIpPZRbU0=; h=Date:To:From:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post; b=q9LjDeYRW3OWdr8kYRQoS+mvLFnu8KKsQUP9N8xM+ALXieMBIx1zS0NxtbC48lgDw cjMQQpaOKhAWTNsVwewfnOWo2jA0YQvnmzYyiHGlLcSAYmGh3QZG3qjjdzpURkaycO udT0OmXgvFFDi4V1qnfjPgbcoyRO2THv9UsK3Oy8a9lRkLgYKVVBf2n0pE9/PGMj7D tg9oim/NfRlg26vDThvZaa9BwVhD4i1B9CZ+y/bzbunBDeVk/PrhxrSuYJ/aJLsvpr uCshRfFNpI2F4uMTXHepevfQvhM0LW3zWy5zz89PyOD9O2oZLG7+4lnUx93moeflyM yx5PbKowaDnOg== 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?= Subject: [RFC PATCH v3 06/21] apps: common: event_loop: Add way to cancel deferred 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 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Store a cookie value of `std::uintptr_t` with the callback to make later cancellation of the callback possible. Signed-off-by: Barnabás Pőcze --- src/apps/common/event_loop.cpp | 18 ++++++++++++++---- src/apps/common/event_loop.h | 6 ++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/apps/common/event_loop.cpp b/src/apps/common/event_loop.cpp index b6230f4ba..0d7a4a024 100644 --- a/src/apps/common/event_loop.cpp +++ b/src/apps/common/event_loop.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include EventLoop *EventLoop::instance_ = nullptr; @@ -33,7 +34,7 @@ EventLoop::EventLoop() if (self->calls_.empty()) break; - call = std::move(self->calls_.front()); + call = std::move(self->calls_.front().first); self->calls_.pop_front(); } @@ -73,16 +74,25 @@ void EventLoop::exit(int code) event_base_loopbreak(base_); } -void EventLoop::callLater(std::function &&func) +void EventLoop::callLater(std::function &&func, std::optional cookie) { { - std::unique_lock locker(lock_); - calls_.push_back(std::move(func)); + std::lock_guard locker(lock_); + calls_.emplace_back(std::move(func), cookie); } event_active(callsTrigger_, 0, 0); } +void EventLoop::cancelLater(std::optional cookie) +{ + std::lock_guard locker(lock_); + + calls_.erase(std::remove_if(calls_.begin(), calls_.end(), + [&](const auto &x) { return !cookie || x.second == *cookie; }), + calls_.end()); +} + void EventLoop::addFdEvent(int fd, EventType type, std::function &&callback) { diff --git a/src/apps/common/event_loop.h b/src/apps/common/event_loop.h index 507023996..6d7d0497a 100644 --- a/src/apps/common/event_loop.h +++ b/src/apps/common/event_loop.h @@ -13,6 +13,7 @@ #include #include #include +#include #include @@ -36,7 +37,8 @@ public: int exec(); void exit(int code = 0); - void callLater(std::function &&func); + void callLater(std::function &&func, std::optional cookie = {}); + void cancelLater(std::optional cookie = {}); void addFdEvent(int fd, EventType type, std::function &&handler); @@ -63,7 +65,7 @@ private: struct event_base *base_; int exitCode_; - std::deque> calls_; + std::deque, std::optional>> calls_; ::event *callsTrigger_ = nullptr; std::list> events_;