From patchwork Tue Jan 14 18:21:51 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: 22562 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 14C95C3301 for ; Tue, 14 Jan 2025 18:21:58 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C5A1F68543; Tue, 14 Jan 2025 19:21:57 +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="G3Q3044Q"; dkim-atps=neutral Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 85EC1607D6 for ; Tue, 14 Jan 2025 19:21:56 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1736878916; x=1737138116; bh=NLjxsPX3DNGK0sbo8UlmfLPb+Cw+1t5rXtAnv/p/uTE=; 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=G3Q3044QE4bAjYuWrDxkQe0BCCU4KGrfLQN1f9z/aEWBEkzj5pV9/nNAcmKD4SpAz 5WszKuu80A/K0oyeUZ+YECYODsQHcziUOABHoVaBwlV+wvT8dcos7wbfrnoG4qPlMG RkZta06YkO8HS9fM939aNy1VdX5YMRErCAZSkgsbbYeKhVTWOVOmcM01lOEgOGGrD2 9jCrmqpznglN3dnyJvVzsBfsyKOahAMVvx+j5gs/TntRyVv9dODyt8L9cjxwd60dR6 xr6/lCgTUicSPaoWtb+qgIywwoRJdHjxTSudUwA1+6E3NjKUc3ieIgjMaaf2rR+PQ6 HBjuiKkUrjWwA== Date: Tue, 14 Jan 2025 18:21:51 +0000 To: libcamera-devel@lists.libcamera.org From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Subject: [RFC PATCH v2 01/16] apps: common: event_loop: Take callbacks by rvalue ref Message-ID: <20250114182143.1773762-2-pobrn@protonmail.com> In-Reply-To: <20250114182143.1773762-1-pobrn@protonmail.com> References: <20250114182143.1773762-1-pobrn@protonmail.com> Feedback-ID: 20568564:user:proton X-Pm-Message-ID: e11c87e32cb03fc8f9e9dbdb4dd0fc53c1898d08 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" Using a const lvalue reference to `std::function<>` is not ideal because it forces a copy to happen. Use an rvalue reference and `std::move()` to avoid that. Signed-off-by: Barnabás Pőcze --- src/apps/common/event_loop.cpp | 16 ++++++++-------- src/apps/common/event_loop.h | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/apps/common/event_loop.cpp b/src/apps/common/event_loop.cpp index f7f9afa0c..bc8cf17ab 100644 --- a/src/apps/common/event_loop.cpp +++ b/src/apps/common/event_loop.cpp @@ -50,20 +50,20 @@ void EventLoop::exit(int code) event_base_loopbreak(base_); } -void EventLoop::callLater(const std::function &func) +void EventLoop::callLater(std::function &&func) { { std::unique_lock locker(lock_); - calls_.push_back(func); + calls_.push_back(std::move(func)); } event_base_once(base_, -1, EV_TIMEOUT, dispatchCallback, this, nullptr); } void EventLoop::addFdEvent(int fd, EventType type, - const std::function &callback) + std::function &&callback) { - std::unique_ptr event = std::make_unique(callback); + std::unique_ptr event = std::make_unique(std::move(callback)); short events = (type & Read ? EV_READ : 0) | (type & Write ? EV_WRITE : 0) | EV_PERSIST; @@ -85,9 +85,9 @@ void EventLoop::addFdEvent(int fd, EventType type, } void EventLoop::addTimerEvent(const std::chrono::microseconds period, - const std::function &callback) + std::function &&callback) { - std::unique_ptr event = std::make_unique(callback); + std::unique_ptr event = std::make_unique(std::move(callback)); event->event_ = event_new(base_, -1, EV_PERSIST, &EventLoop::Event::dispatch, event.get()); if (!event->event_) { @@ -131,8 +131,8 @@ void EventLoop::dispatchCall() call(); } -EventLoop::Event::Event(const std::function &callback) - : callback_(callback), event_(nullptr) +EventLoop::Event::Event(std::function &&callback) + : callback_(std::move(callback)), event_(nullptr) { } diff --git a/src/apps/common/event_loop.h b/src/apps/common/event_loop.h index ef129b9aa..d7d012c76 100644 --- a/src/apps/common/event_loop.h +++ b/src/apps/common/event_loop.h @@ -33,18 +33,18 @@ public: int exec(); void exit(int code = 0); - void callLater(const std::function &func); + void callLater(std::function &&func); void addFdEvent(int fd, EventType type, - const std::function &handler); + std::function &&handler); using duration = std::chrono::steady_clock::duration; void addTimerEvent(const std::chrono::microseconds period, - const std::function &handler); + std::function &&handler); private: struct Event { - Event(const std::function &callback); + Event(std::function &&callback); ~Event(); static void dispatch(int fd, short events, void *arg);