From patchwork Fri Nov 13 06:38:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 10419 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 A7E17BE086 for ; Fri, 13 Nov 2020 06:38:28 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 75541631B1; Fri, 13 Nov 2020 07:38:28 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="NhgeRl+I"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6619863149 for ; Fri, 13 Nov 2020 07:38:25 +0100 (CET) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 06B3FA1B for ; Fri, 13 Nov 2020 07:38:24 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1605249505; bh=kayCqxYadOhi7xZmSDg7d3vUdi+NuYPicv0jnngLwH8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=NhgeRl+IJQpz6FfeRxeXH0RdqyC8ZGWnneIx5hCQ+ZZDGKZnLkbKhJWkXu90dXbc9 8y13sdu37Sr5i8T5zHIfkM3Lx2M91DorLOPzWgWX/9Kcj+WhpeqkJfdLpCnytv6fES EoA0J5dFXOhKZDgq0CUWJYdZA51ULapSzLuFYh/I= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Fri, 13 Nov 2020 08:38:14 +0200 Message-Id: <20201113063815.10288-5-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201113063815.10288-1-laurent.pinchart@ideasonboard.com> References: <20201113063815.10288-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v1 4/5] cam: event_loop: Add deferred calls support 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" Add a deferred cals queue to the EventLoop class to support queuing calls from a different thread and processing them in the event loop's thread. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Niklas Söderlund --- src/cam/event_loop.cpp | 29 ++++++++++++++++++++++++++++- src/cam/event_loop.h | 9 +++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp index 13f2583da0a1..94c5d1d36245 100644 --- a/src/cam/event_loop.cpp +++ b/src/cam/event_loop.cpp @@ -40,8 +40,10 @@ int EventLoop::exec() exitCode_ = -1; exit_.store(false, std::memory_order_release); - while (!exit_.load(std::memory_order_acquire)) + while (!exit_.load(std::memory_order_acquire)) { + dispatchCalls(); event_base_loop(event_, EVLOOP_NO_EXIT_ON_EMPTY); + } return exitCode_; } @@ -57,3 +59,28 @@ void EventLoop::interrupt() { event_base_loopbreak(event_); } + +void EventLoop::callLater(const std::function &func) +{ + { + std::unique_lock locker(lock_); + calls_.push_back(func); + } + + interrupt(); +} + +void EventLoop::dispatchCalls() +{ + std::unique_lock locker(lock_); + + for (auto iter = calls_.begin(); iter != calls_.end(); ) { + std::function call = std::move(*iter); + + iter = calls_.erase(iter); + + locker.unlock(); + call(); + locker.lock(); + } +} diff --git a/src/cam/event_loop.h b/src/cam/event_loop.h index b1c6bd103080..408073c50594 100644 --- a/src/cam/event_loop.h +++ b/src/cam/event_loop.h @@ -8,6 +8,9 @@ #define __CAM_EVENT_LOOP_H__ #include +#include +#include +#include struct event_base; @@ -22,6 +25,8 @@ public: int exec(); void exit(int code = 0); + void callLater(const std::function &func); + private: static EventLoop *instance_; @@ -29,7 +34,11 @@ private: std::atomic exit_; int exitCode_; + std::list> calls_; + std::mutex lock_; + void interrupt(); + void dispatchCalls(); }; #endif /* __CAM_EVENT_LOOP_H__ */