From patchwork Thu Jan 30 11:50:08 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: 22660 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 22E7EBDB1C for ; Thu, 30 Jan 2025 11:50:16 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4F5DE68563; Thu, 30 Jan 2025 12:50:15 +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="WeJxizlH"; dkim-atps=neutral Received: from mail-40134.protonmail.ch (mail-40134.protonmail.ch [185.70.40.134]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 871876034C for ; Thu, 30 Jan 2025 12:50:13 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1738237812; x=1738497012; bh=nx5skBNCz7i+955ZPFMrLg8EFjHcpon5EJqDadeZ0uw=; h=Date:To:From:Cc: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=WeJxizlHbUecy2jTRitY6EEz57fEE/ZP2a1Tg7+8zgdTNvpyxfi7y3A4Qpmed0/dE xdoldLgtvYosC0FBqrB3Ek70eDx4qtTU0VyFV8M1th6O8uPNbbvAmtrSYxrr5tPecK SexuefZ5uEBmfayuWo3tgd9ihyDTss9joCG0iHr+hdb/y9M3v8y8pny4V249D80LRV qIC4w9sP4Z6rB1k1KuUb5ij7Ya3sLMztMpc0v/Gl7KEcKhSKrVos6C4/89/8kig27S ME+4NwDCV5OVa2BOefNvAGzuFtd18Xw9VRS2TuYmQ0EhdBXHghbHkKQ5VOPRrdQZlN 63B5qn7SyxzRQ== Date: Thu, 30 Jan 2025 11:50:08 +0000 To: libcamera-devel@lists.libcamera.org From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Cc: Jacopo Mondi , Paul Elder Subject: [RFC PATCH v3 01/21] apps: common: event_loop: Take callbacks by rvalue ref Message-ID: <20250130115001.1129305-2-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: 20fb6a77ee7f601afe43b05fb225494ea8a04396 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 Reviewed-by: Jacopo Mondi Reviewed-by: Paul Elder --- 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);