From patchwork Thu Jan 30 11:50:23 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: 22662 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 41E43BDB1C for ; Thu, 30 Jan 2025 11:50:29 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0CBE468567; Thu, 30 Jan 2025 12:50:29 +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="xpmgCBr7"; dkim-atps=neutral Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 61AFD68567 for ; Thu, 30 Jan 2025 12:50:27 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1738237826; x=1738497026; bh=zTJlVF9VecSivZdM7mHggxshRZizFlrVHInQxMvM9Xw=; 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=xpmgCBr7n5o/28Up94qw5sPHZXS0lCHPh4WpU2JWq/D4v+LVEw8sAs5EyknA3fvLW MIgNkp+3O4+6BWUKl/+aNZ2nPRJi6pOE2Jdh4iqcf0Ecz972zNQ2pu0q78aIWLrVrL zQlOnasuCyBBFY3zAEXJdS8F7CHROGh1EjqqV+Kfd/aiYL8iov593GMNpydt5rdVuO 2STanDNmgKw8MGqijhfY8ucKwvWnOfe0e2XYmxL56wDGS0RuBPNEs6sS1tQAiqWpb1 zgJM61wX5xmNoNx/4hYViy2fGM6zTpGDXuFGOwm1b23raoD5OPX8Ff/YWoa3WqI6gh FGPlQKPeFltWw== Date: Thu, 30 Jan 2025 11:50:23 +0000 To: libcamera-devel@lists.libcamera.org From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Subject: [RFC PATCH v3 04/21] apps: common: event_loop: Use single event source for deferred calls Message-ID: <20250130115001.1129305-5-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: ae2c0c9f34f3fac4d10449178f413ab0823d239e 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" Instead of calling `event_base_once()` every time a deferred call is added to the loop, create an event source at construction, and simply trigger that when a new deferred call is scheduled. Signed-off-by: Barnabás Pőcze Reviewed-by: Jacopo Mondi --- src/apps/common/event_loop.cpp | 48 +++++++++++++++++----------------- src/apps/common/event_loop.h | 5 +--- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/apps/common/event_loop.cpp b/src/apps/common/event_loop.cpp index bc8cf17ab..b6230f4ba 100644 --- a/src/apps/common/event_loop.cpp +++ b/src/apps/common/event_loop.cpp @@ -21,12 +21,35 @@ EventLoop::EventLoop() evthread_use_pthreads(); base_ = event_base_new(); instance_ = this; + + callsTrigger_ = event_new(base_, -1, EV_PERSIST, [](evutil_socket_t, short, void *closure) { + auto *self = static_cast(closure); + + for (;;) { + std::function call; + + { + std::lock_guard locker(self->lock_); + if (self->calls_.empty()) + break; + + call = std::move(self->calls_.front()); + self->calls_.pop_front(); + } + + call(); + } + }, this); + assert(callsTrigger_); + event_add(callsTrigger_, nullptr); } EventLoop::~EventLoop() { instance_ = nullptr; + event_free(callsTrigger_); + events_.clear(); event_base_free(base_); libevent_global_shutdown(); @@ -57,7 +80,7 @@ void EventLoop::callLater(std::function &&func) calls_.push_back(std::move(func)); } - event_base_once(base_, -1, EV_TIMEOUT, dispatchCallback, this, nullptr); + event_active(callsTrigger_, 0, 0); } void EventLoop::addFdEvent(int fd, EventType type, @@ -108,29 +131,6 @@ void EventLoop::addTimerEvent(const std::chrono::microseconds period, events_.push_back(std::move(event)); } -void EventLoop::dispatchCallback([[maybe_unused]] evutil_socket_t fd, - [[maybe_unused]] short flags, void *param) -{ - EventLoop *loop = static_cast(param); - loop->dispatchCall(); -} - -void EventLoop::dispatchCall() -{ - std::function call; - - { - std::unique_lock locker(lock_); - if (calls_.empty()) - return; - - call = calls_.front(); - calls_.pop_front(); - } - - call(); -} - 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 760075885..aac5ed3cc 100644 --- a/src/apps/common/event_loop.h +++ b/src/apps/common/event_loop.h @@ -65,11 +65,8 @@ private: int exitCode_; std::deque> calls_; + ::event *callsTrigger_ = nullptr; std::list> events_; std::mutex lock_; - - static void dispatchCallback(evutil_socket_t fd, short flags, - void *param); - void dispatchCall(); };