From patchwork Fri Nov 13 06:38:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 10416 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 3347DBE087 for ; Fri, 13 Nov 2020 06:38:26 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D777163149; Fri, 13 Nov 2020 07:38:25 +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="UK8S4QuH"; 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 56A3D63149 for ; Fri, 13 Nov 2020 07:38:24 +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 E9D0631A for ; Fri, 13 Nov 2020 07:38:23 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1605249504; bh=HT3CDBwjNQs1AYkzp9kXeF/Yrwx/R8PDIGaQPmyePpE=; h=From:To:Subject:Date:In-Reply-To:References:From; b=UK8S4QuHFBwnO4Hb3ltekY2AK/0sufD6Rv5MWIpGt3b/XHNaCwBL0zm+KU2C8M/9I sl+1KT1OgvXSYqD5zpcV4CYkfrOM8+SxomVGSb5jC7QIVz4G06vYpKx+JCypt4LA5D IcOY5nNXbXJ+S4tVi1wcM1pL1GUMcC//h4PwQnm0= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Fri, 13 Nov 2020 08:38:11 +0200 Message-Id: <20201113063815.10288-2-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 1/5] test: Get event dispatcher from current thread 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" Get the event dispatcher from the current thread instead of the camera manager. This prepares for the removal of CameraManager::eventDispatcher(). Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Niklas Söderlund --- test/camera/buffer_import.cpp | 3 ++- test/camera/capture.cpp | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/test/camera/buffer_import.cpp b/test/camera/buffer_import.cpp index 4b626dffa575..ccf532d7b7fc 100644 --- a/test/camera/buffer_import.cpp +++ b/test/camera/buffer_import.cpp @@ -17,6 +17,7 @@ #include "libcamera/internal/device_enumerator.h" #include "libcamera/internal/media_device.h" +#include "libcamera/internal/thread.h" #include "libcamera/internal/v4l2_videodevice.h" #include "buffer_source.h" @@ -131,7 +132,7 @@ protected: } } - EventDispatcher *dispatcher = cm_->eventDispatcher(); + EventDispatcher *dispatcher = Thread::current()->eventDispatcher(); Timer timer; timer.start(1000); diff --git a/test/camera/capture.cpp b/test/camera/capture.cpp index f13ca53b8cdf..fd5292b7c8c9 100644 --- a/test/camera/capture.cpp +++ b/test/camera/capture.cpp @@ -11,6 +11,8 @@ #include #include +#include "libcamera/internal/thread.h" + #include "camera_test.h" #include "test.h" @@ -131,7 +133,7 @@ protected: } } - EventDispatcher *dispatcher = cm_->eventDispatcher(); + EventDispatcher *dispatcher = Thread::current()->eventDispatcher(); Timer timer; timer.start(1000); From patchwork Fri Nov 13 06:38:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 10417 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 01CBDBE086 for ; Fri, 13 Nov 2020 06:38:27 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 30015631C4; Fri, 13 Nov 2020 07:38:26 +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="vzyN5uxk"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A91D7631AF for ; Fri, 13 Nov 2020 07:38:24 +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 45ADFA1B 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=1605249504; bh=14Ycuh7MxS3qAE+FZCnYtm0w6OPq3HBKUDkkJNWhlPo=; h=From:To:Subject:Date:In-Reply-To:References:From; b=vzyN5uxkfNCT/HSONJUCfvOro9GK8TRtVVEr2QFinZghLodp7Exh53YJ+pqabWMYB i4GzCV1pW467sIAgU2ZYS9QL0s0CCMHazOJWe+C0qFCEvugJeZ80YxluZNPJgHISF2 cEK9sJDoPWRNQAt7YFPIu40GlKj97lqqmFWPxPHw= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Fri, 13 Nov 2020 08:38:12 +0200 Message-Id: <20201113063815.10288-3-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 2/5] cam: Use libevent to implement event loop 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" To prepare for removal of the EventDispatcher from the libcamera public API, switch to libevent to handle the event loop. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Kieran Bingham Reviewed-by: Niklas Söderlund Reported-by: Coverity CID=305971 --- src/cam/event_loop.cpp | 34 +++++++++++++++++++++++++++------- src/cam/event_loop.h | 15 ++++++++------- src/cam/main.cpp | 16 +++++----------- src/cam/meson.build | 13 ++++++++++++- 4 files changed, 52 insertions(+), 26 deletions(-) diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp index e8ab861790ed..13f2583da0a1 100644 --- a/src/cam/event_loop.cpp +++ b/src/cam/event_loop.cpp @@ -5,19 +5,34 @@ * event_loop.cpp - cam - Event loop */ -#include - #include "event_loop.h" -using namespace libcamera; +#include +#include +#include -EventLoop::EventLoop(EventDispatcher *dispatcher) - : dispatcher_(dispatcher) +EventLoop *EventLoop::instance_ = nullptr; + +EventLoop::EventLoop() { + assert(!instance_); + + evthread_use_pthreads(); + event_ = event_base_new(); + instance_ = this; } EventLoop::~EventLoop() { + instance_ = nullptr; + + event_base_free(event_); + libevent_global_shutdown(); +} + +EventLoop *EventLoop::instance() +{ + return instance_; } int EventLoop::exec() @@ -26,7 +41,7 @@ int EventLoop::exec() exit_.store(false, std::memory_order_release); while (!exit_.load(std::memory_order_acquire)) - dispatcher_->processEvents(); + event_base_loop(event_, EVLOOP_NO_EXIT_ON_EMPTY); return exitCode_; } @@ -35,5 +50,10 @@ void EventLoop::exit(int code) { exitCode_ = code; exit_.store(true, std::memory_order_release); - dispatcher_->interrupt(); + interrupt(); +} + +void EventLoop::interrupt() +{ + event_base_loopbreak(event_); } diff --git a/src/cam/event_loop.h b/src/cam/event_loop.h index 581c7cba2fc4..b1c6bd103080 100644 --- a/src/cam/event_loop.h +++ b/src/cam/event_loop.h @@ -9,26 +9,27 @@ #include -#include - -namespace libcamera { -class EventDispatcher; -} +struct event_base; class EventLoop { public: - EventLoop(libcamera::EventDispatcher *dispatcher); + EventLoop(); ~EventLoop(); + static EventLoop *instance(); + int exec(); void exit(int code = 0); private: - libcamera::EventDispatcher *dispatcher_; + static EventLoop *instance_; + struct event_base *event_; std::atomic exit_; int exitCode_; + + void interrupt(); }; #endif /* __CAM_EVENT_LOOP_H__ */ diff --git a/src/cam/main.cpp b/src/cam/main.cpp index b3a8d94f5739..e01be63a7058 100644 --- a/src/cam/main.cpp +++ b/src/cam/main.cpp @@ -52,7 +52,7 @@ private: CameraManager *cm_; std::shared_ptr camera_; std::unique_ptr config_; - EventLoop *loop_; + EventLoop loop_; bool strictFormats_; }; @@ -60,7 +60,7 @@ private: CamApp *CamApp::app_ = nullptr; CamApp::CamApp() - : cm_(nullptr), camera_(nullptr), config_(nullptr), loop_(nullptr), + : cm_(nullptr), camera_(nullptr), config_(nullptr), strictFormats_(false) { CamApp::app_ = this; @@ -134,16 +134,11 @@ int CamApp::init(int argc, char **argv) std::cout << "Monitoring new hotplug and unplug events" << std::endl; } - loop_ = new EventLoop(cm_->eventDispatcher()); - return 0; } void CamApp::cleanup() { - delete loop_; - loop_ = nullptr; - if (camera_) { camera_->release(); camera_.reset(); @@ -166,8 +161,7 @@ int CamApp::exec() void CamApp::quit() { - if (loop_) - loop_->exit(); + loop_.exit(); } int CamApp::parseOptions(int argc, char *argv[]) @@ -366,13 +360,13 @@ int CamApp::run() } if (options_.isSet(OptCapture)) { - Capture capture(camera_, config_.get(), loop_); + Capture capture(camera_, config_.get(), &loop_); return capture.run(options_); } if (options_.isSet(OptMonitor)) { std::cout << "Press Ctrl-C to interrupt" << std::endl; - ret = loop_->exec(); + ret = loop_.exec(); if (ret) std::cout << "Failed to run monitor loop" << std::endl; } diff --git a/src/cam/meson.build b/src/cam/meson.build index 89e124fbae2a..62003c58dad0 100644 --- a/src/cam/meson.build +++ b/src/cam/meson.build @@ -1,5 +1,12 @@ # SPDX-License-Identifier: CC0-1.0 +libevent = dependency('libevent_pthreads', required : false) + +if not libevent.found() + warning('libevent_pthreads not found,\'cam\' application will not be compiled') + subdir_done() +endif + cam_sources = files([ 'buffer_writer.cpp', 'capture.cpp', @@ -10,5 +17,9 @@ cam_sources = files([ ]) cam = executable('cam', cam_sources, - dependencies : [ libatomic, libcamera_dep ], + dependencies : [ + libatomic, + libcamera_dep, + libevent, + ], install : true) From patchwork Fri Nov 13 06:38:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 10418 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 5975ABE087 for ; Fri, 13 Nov 2020 06:38:27 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id DC06D631BA; Fri, 13 Nov 2020 07:38:26 +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="exliK8Hr"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 15E4B63149 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 A3F7F31A 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=1605249504; bh=hIZTCPyRR/uYyXkmN/uzw+Ae/aloEStCMJEuVE6JqCw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=exliK8HrUD+0Hl/ipMeYIiYcjigN0ahm0qYVxLDpBWX/Kjh5ajVPLFPw36VGfy/q+ IJwtlWI7CuvYRnhE8r1mgWv8Pq+FlfUekWwo1BsKovHlJiBC407ffZbDRvqLXb/Hve b7A0Fdzv5aYnn16d3OEy1D8AjugAkKFN2CMXPHd8= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Fri, 13 Nov 2020 08:38:13 +0200 Message-Id: <20201113063815.10288-4-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 3/5] libcamera: Move EventDispatcher to internal API 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" There's no user of the EventDispatcher (and the related EventNotifier and Timer classes) outside of libcamera. Move those classes to the internal API. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- include/libcamera/camera_manager.h | 4 -- .../{ => internal}/event_dispatcher.h | 0 .../internal/event_dispatcher_poll.h | 2 +- .../libcamera/{ => internal}/event_notifier.h | 0 include/libcamera/internal/meson.build | 3 ++ include/libcamera/{ => internal}/timer.h | 0 include/libcamera/meson.build | 3 -- src/libcamera/camera_manager.cpp | 44 +------------------ src/libcamera/device_enumerator_udev.cpp | 3 +- src/libcamera/event_dispatcher.cpp | 2 +- src/libcamera/event_dispatcher_poll.cpp | 5 +-- src/libcamera/event_notifier.cpp | 4 +- src/libcamera/ipc_unixsocket.cpp | 3 +- src/libcamera/pipeline/rkisp1/timeline.h | 3 +- src/libcamera/process.cpp | 3 +- .../proxy/worker/ipa_proxy_linux_worker.cpp | 2 +- src/libcamera/thread.cpp | 3 +- src/libcamera/timer.cpp | 4 +- src/libcamera/v4l2_device.cpp | 3 +- src/libcamera/v4l2_videodevice.cpp | 2 +- test/camera/buffer_import.cpp | 5 +-- test/camera/capture.cpp | 5 ++- test/event-dispatcher.cpp | 5 +-- test/event-thread.cpp | 5 +-- test/event.cpp | 7 ++- test/hotplug-cameras.cpp | 4 +- test/ipa/ipa_interface_test.cpp | 6 +-- test/ipc/unixsocket.cpp | 5 +-- test/log/log_process.cpp | 4 +- test/object-invoke.cpp | 2 +- test/process/process_test.cpp | 5 +-- test/timer-thread.cpp | 5 +-- test/timer.cpp | 5 +-- test/v4l2_videodevice/buffer_sharing.cpp | 4 +- test/v4l2_videodevice/capture_async.cpp | 4 +- test/v4l2_videodevice/v4l2_m2mdevice.cpp | 4 +- 36 files changed, 55 insertions(+), 113 deletions(-) rename include/libcamera/{ => internal}/event_dispatcher.h (100%) rename include/libcamera/{ => internal}/event_notifier.h (100%) rename include/libcamera/{ => internal}/timer.h (100%) diff --git a/include/libcamera/camera_manager.h b/include/libcamera/camera_manager.h index 6d5341c76412..8c8830e7ff1d 100644 --- a/include/libcamera/camera_manager.h +++ b/include/libcamera/camera_manager.h @@ -19,7 +19,6 @@ namespace libcamera { class Camera; -class EventDispatcher; class CameraManager : public Object, public Extensible { @@ -43,9 +42,6 @@ public: static const std::string &version() { return version_; } - void setEventDispatcher(std::unique_ptr dispatcher); - EventDispatcher *eventDispatcher(); - Signal> cameraAdded; Signal> cameraRemoved; diff --git a/include/libcamera/event_dispatcher.h b/include/libcamera/internal/event_dispatcher.h similarity index 100% rename from include/libcamera/event_dispatcher.h rename to include/libcamera/internal/event_dispatcher.h diff --git a/include/libcamera/internal/event_dispatcher_poll.h b/include/libcamera/internal/event_dispatcher_poll.h index 39d9be525dab..33de051de261 100644 --- a/include/libcamera/internal/event_dispatcher_poll.h +++ b/include/libcamera/internal/event_dispatcher_poll.h @@ -11,7 +11,7 @@ #include #include -#include +#include "libcamera/internal/event_dispatcher.h" struct pollfd; diff --git a/include/libcamera/event_notifier.h b/include/libcamera/internal/event_notifier.h similarity index 100% rename from include/libcamera/event_notifier.h rename to include/libcamera/internal/event_notifier.h diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build index 6500fe2a82fb..7cde023f7c3a 100644 --- a/include/libcamera/internal/meson.build +++ b/include/libcamera/internal/meson.build @@ -18,7 +18,9 @@ libcamera_internal_headers = files([ 'device_enumerator.h', 'device_enumerator_sysfs.h', 'device_enumerator_udev.h', + 'event_dispatcher.h', 'event_dispatcher_poll.h', + 'event_notifier.h', 'file.h', 'formats.h', 'ipa_context_wrapper.h', @@ -36,6 +38,7 @@ libcamera_internal_headers = files([ 'semaphore.h', 'sysfs.h', 'thread.h', + 'timer.h', 'utils.h', 'v4l2_controls.h', 'v4l2_device.h', diff --git a/include/libcamera/timer.h b/include/libcamera/internal/timer.h similarity index 100% rename from include/libcamera/timer.h rename to include/libcamera/internal/timer.h diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index 0b891a8f4f9b..cf2935f1ee95 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -6,8 +6,6 @@ libcamera_public_headers = files([ 'camera.h', 'camera_manager.h', 'controls.h', - 'event_dispatcher.h', - 'event_notifier.h', 'extensible.h', 'file_descriptor.h', 'framebuffer_allocator.h', @@ -19,7 +17,6 @@ libcamera_public_headers = files([ 'signal.h', 'span.h', 'stream.h', - 'timer.h', 'transform.h', ]) diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp index 427ea5daf671..67641a555e1e 100644 --- a/src/libcamera/camera_manager.cpp +++ b/src/libcamera/camera_manager.cpp @@ -11,10 +11,8 @@ #include #include -#include #include "libcamera/internal/device_enumerator.h" -#include "libcamera/internal/event_dispatcher_poll.h" #include "libcamera/internal/ipa_manager.h" #include "libcamera/internal/log.h" #include "libcamera/internal/pipeline_handler.h" @@ -244,12 +242,8 @@ void CameraManager::Private::removeCamera(Camera *camera) * a time. Attempting to create a second instance without first deleting the * existing instance results in undefined behaviour. * - * The manager is initially stopped, and shall be configured before being - * started. In particular a custom event dispatcher shall be installed if - * needed with CameraManager::setEventDispatcher(). - * - * Once the camera manager is configured, it shall be started with start(). - * This will enumerate all the cameras present in the system, which can then be + * The manager is initially stopped, and shall be started with start(). This + * will enumerate all the cameras present in the system, which can then be * listed with list() and retrieved with get(). * * Cameras are shared through std::shared_ptr<>, ensuring that a camera will @@ -477,38 +471,4 @@ void CameraManager::removeCamera(std::shared_ptr camera) * \return The libcamera version string */ -/** - * \brief Set the event dispatcher - * \param[in] dispatcher Pointer to the event dispatcher - * - * libcamera requires an event dispatcher to integrate event notification and - * timers with the application event loop. Applications that want to provide - * their own event dispatcher shall call this function once and only once before - * the camera manager is started with start(). If no event dispatcher is - * provided, a default poll-based implementation will be used. - * - * The CameraManager takes ownership of the event dispatcher and will delete it - * when the application terminates. - */ -void CameraManager::setEventDispatcher(std::unique_ptr dispatcher) -{ - thread()->setEventDispatcher(std::move(dispatcher)); -} - -/** - * \brief Retrieve the event dispatcher - * - * This function retrieves the event dispatcher set with setEventDispatcher(). - * If no dispatcher has been set, a default poll-based implementation is created - * and returned, and no custom event dispatcher may be installed anymore. - * - * The returned event dispatcher is valid until the camera manager is destroyed. - * - * \return Pointer to the event dispatcher - */ -EventDispatcher *CameraManager::eventDispatcher() -{ - return thread()->eventDispatcher(); -} - } /* namespace libcamera */ diff --git a/src/libcamera/device_enumerator_udev.cpp b/src/libcamera/device_enumerator_udev.cpp index c6e23a1a7a18..d26fcf10b3c4 100644 --- a/src/libcamera/device_enumerator_udev.cpp +++ b/src/libcamera/device_enumerator_udev.cpp @@ -17,8 +17,7 @@ #include #include -#include - +#include "libcamera/internal/event_notifier.h" #include "libcamera/internal/log.h" #include "libcamera/internal/media_device.h" diff --git a/src/libcamera/event_dispatcher.cpp b/src/libcamera/event_dispatcher.cpp index 90bd5daf0612..e0ce1eb3cc04 100644 --- a/src/libcamera/event_dispatcher.cpp +++ b/src/libcamera/event_dispatcher.cpp @@ -5,7 +5,7 @@ * event_dispatcher.cpp - Event dispatcher */ -#include +#include "libcamera/internal/event_dispatcher.h" #include "libcamera/internal/log.h" diff --git a/src/libcamera/event_dispatcher_poll.cpp b/src/libcamera/event_dispatcher_poll.cpp index 9ab85da7d75d..456c6def075b 100644 --- a/src/libcamera/event_dispatcher_poll.cpp +++ b/src/libcamera/event_dispatcher_poll.cpp @@ -16,11 +16,10 @@ #include #include -#include -#include - +#include "libcamera/internal/event_notifier.h" #include "libcamera/internal/log.h" #include "libcamera/internal/thread.h" +#include "libcamera/internal/timer.h" #include "libcamera/internal/utils.h" /** diff --git a/src/libcamera/event_notifier.cpp b/src/libcamera/event_notifier.cpp index 21c07faf0963..6b0575c0db77 100644 --- a/src/libcamera/event_notifier.cpp +++ b/src/libcamera/event_notifier.cpp @@ -5,11 +5,11 @@ * event_notifier.cpp - File descriptor event notifier */ -#include +#include "libcamera/internal/event_notifier.h" #include -#include +#include "libcamera/internal/event_dispatcher.h" #include "libcamera/internal/message.h" #include "libcamera/internal/thread.h" diff --git a/src/libcamera/ipc_unixsocket.cpp b/src/libcamera/ipc_unixsocket.cpp index 5c8cce16200e..fdb359f7fcd1 100644 --- a/src/libcamera/ipc_unixsocket.cpp +++ b/src/libcamera/ipc_unixsocket.cpp @@ -12,8 +12,7 @@ #include #include -#include - +#include "libcamera/internal/event_notifier.h" #include "libcamera/internal/log.h" /** diff --git a/src/libcamera/pipeline/rkisp1/timeline.h b/src/libcamera/pipeline/rkisp1/timeline.h index 0c37b06fa6df..35a085159b8f 100644 --- a/src/libcamera/pipeline/rkisp1/timeline.h +++ b/src/libcamera/pipeline/rkisp1/timeline.h @@ -10,8 +10,7 @@ #include #include -#include - +#include "libcamera/internal/timer.h" #include "libcamera/internal/utils.h" namespace libcamera { diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp index 72b5afe2f9c0..40a434a6b0c2 100644 --- a/src/libcamera/process.cpp +++ b/src/libcamera/process.cpp @@ -20,8 +20,7 @@ #include #include -#include - +#include "libcamera/internal/event_notifier.h" #include "libcamera/internal/log.h" #include "libcamera/internal/utils.h" diff --git a/src/libcamera/proxy/worker/ipa_proxy_linux_worker.cpp b/src/libcamera/proxy/worker/ipa_proxy_linux_worker.cpp index 0c4687f737a3..bdbac988e057 100644 --- a/src/libcamera/proxy/worker/ipa_proxy_linux_worker.cpp +++ b/src/libcamera/proxy/worker/ipa_proxy_linux_worker.cpp @@ -9,10 +9,10 @@ #include #include -#include #include #include +#include "libcamera/internal/event_dispatcher.h" #include "libcamera/internal/ipa_module.h" #include "libcamera/internal/ipc_unixsocket.h" #include "libcamera/internal/log.h" diff --git a/src/libcamera/thread.cpp b/src/libcamera/thread.cpp index b5d7103ad341..f339dab165b6 100644 --- a/src/libcamera/thread.cpp +++ b/src/libcamera/thread.cpp @@ -14,8 +14,7 @@ #include #include -#include - +#include "libcamera/internal/event_dispatcher.h" #include "libcamera/internal/event_dispatcher_poll.h" #include "libcamera/internal/log.h" #include "libcamera/internal/message.h" diff --git a/src/libcamera/timer.cpp b/src/libcamera/timer.cpp index 24e452eda49e..c242113a782e 100644 --- a/src/libcamera/timer.cpp +++ b/src/libcamera/timer.cpp @@ -5,13 +5,13 @@ * timer.cpp - Generic timer */ -#include +#include "libcamera/internal/timer.h" #include #include -#include +#include "libcamera/internal/event_dispatcher.h" #include "libcamera/internal/log.h" #include "libcamera/internal/message.h" #include "libcamera/internal/thread.h" diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp index fd0b140f8ce7..decd19ef5281 100644 --- a/src/libcamera/v4l2_device.cpp +++ b/src/libcamera/v4l2_device.cpp @@ -16,8 +16,7 @@ #include #include -#include - +#include "libcamera/internal/event_notifier.h" #include "libcamera/internal/log.h" #include "libcamera/internal/sysfs.h" #include "libcamera/internal/utils.h" diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 5ee1b479b513..e76fe2dd1f3a 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -20,9 +20,9 @@ #include -#include #include +#include "libcamera/internal/event_notifier.h" #include "libcamera/internal/log.h" #include "libcamera/internal/media_device.h" #include "libcamera/internal/media_object.h" diff --git a/test/camera/buffer_import.cpp b/test/camera/buffer_import.cpp index ccf532d7b7fc..7ff628269c47 100644 --- a/test/camera/buffer_import.cpp +++ b/test/camera/buffer_import.cpp @@ -12,12 +12,11 @@ #include #include -#include -#include - #include "libcamera/internal/device_enumerator.h" +#include "libcamera/internal/event_dispatcher.h" #include "libcamera/internal/media_device.h" #include "libcamera/internal/thread.h" +#include "libcamera/internal/timer.h" #include "libcamera/internal/v4l2_videodevice.h" #include "buffer_source.h" diff --git a/test/camera/capture.cpp b/test/camera/capture.cpp index fd5292b7c8c9..ec978068c977 100644 --- a/test/camera/capture.cpp +++ b/test/camera/capture.cpp @@ -7,9 +7,10 @@ #include -#include #include -#include + +#include "libcamera/internal/event_dispatcher.h" +#include "libcamera/internal/timer.h" #include "libcamera/internal/thread.h" diff --git a/test/event-dispatcher.cpp b/test/event-dispatcher.cpp index 1b61778699ed..8ae05ac62985 100644 --- a/test/event-dispatcher.cpp +++ b/test/event-dispatcher.cpp @@ -10,10 +10,9 @@ #include #include -#include -#include - +#include "libcamera/internal/event_dispatcher.h" #include "libcamera/internal/thread.h" +#include "libcamera/internal/timer.h" #include "test.h" diff --git a/test/event-thread.cpp b/test/event-thread.cpp index c90e6ac04dd3..aeb8b3bf4bac 100644 --- a/test/event-thread.cpp +++ b/test/event-thread.cpp @@ -10,10 +10,9 @@ #include #include -#include -#include - +#include "libcamera/internal/event_notifier.h" #include "libcamera/internal/thread.h" +#include "libcamera/internal/timer.h" #include "test.h" diff --git a/test/event.cpp b/test/event.cpp index c865092c7a8a..1f16c97bbab5 100644 --- a/test/event.cpp +++ b/test/event.cpp @@ -9,11 +9,10 @@ #include #include -#include -#include -#include - +#include "libcamera/internal/event_dispatcher.h" +#include "libcamera/internal/event_notifier.h" #include "libcamera/internal/thread.h" +#include "libcamera/internal/timer.h" #include "test.h" diff --git a/test/hotplug-cameras.cpp b/test/hotplug-cameras.cpp index 7d551eeb900d..94cb859352ec 100644 --- a/test/hotplug-cameras.cpp +++ b/test/hotplug-cameras.cpp @@ -13,11 +13,11 @@ #include #include -#include -#include +#include "libcamera/internal/event_dispatcher.h" #include "libcamera/internal/file.h" #include "libcamera/internal/thread.h" +#include "libcamera/internal/timer.h" #include "test.h" diff --git a/test/ipa/ipa_interface_test.cpp b/test/ipa/ipa_interface_test.cpp index 67488409e7ff..9f575f9350ec 100644 --- a/test/ipa/ipa_interface_test.cpp +++ b/test/ipa/ipa_interface_test.cpp @@ -12,16 +12,16 @@ #include #include -#include -#include #include -#include #include "libcamera/internal/device_enumerator.h" +#include "libcamera/internal/event_dispatcher.h" +#include "libcamera/internal/event_notifier.h" #include "libcamera/internal/ipa_manager.h" #include "libcamera/internal/ipa_module.h" #include "libcamera/internal/pipeline_handler.h" #include "libcamera/internal/thread.h" +#include "libcamera/internal/timer.h" #include "test.h" diff --git a/test/ipc/unixsocket.cpp b/test/ipc/unixsocket.cpp index 9cd07ce8d415..19a1d7dd8a2d 100644 --- a/test/ipc/unixsocket.cpp +++ b/test/ipc/unixsocket.cpp @@ -15,11 +15,10 @@ #include #include -#include -#include - +#include "libcamera/internal/event_dispatcher.h" #include "libcamera/internal/ipc_unixsocket.h" #include "libcamera/internal/thread.h" +#include "libcamera/internal/timer.h" #include "libcamera/internal/utils.h" #include "test.h" diff --git a/test/log/log_process.cpp b/test/log/log_process.cpp index 2a82622236b4..c0a1a012d366 100644 --- a/test/log/log_process.cpp +++ b/test/log/log_process.cpp @@ -14,13 +14,13 @@ #include #include -#include #include -#include +#include "libcamera/internal/event_dispatcher.h" #include "libcamera/internal/log.h" #include "libcamera/internal/process.h" #include "libcamera/internal/thread.h" +#include "libcamera/internal/timer.h" #include "libcamera/internal/utils.h" #include "test.h" diff --git a/test/object-invoke.cpp b/test/object-invoke.cpp index f3fd23283239..a6f816f395eb 100644 --- a/test/object-invoke.cpp +++ b/test/object-invoke.cpp @@ -8,9 +8,9 @@ #include #include -#include #include +#include "libcamera/internal/event_dispatcher.h" #include "libcamera/internal/thread.h" #include "test.h" diff --git a/test/process/process_test.cpp b/test/process/process_test.cpp index a3eaef80b2a2..1279d8c17598 100644 --- a/test/process/process_test.cpp +++ b/test/process/process_test.cpp @@ -9,11 +9,10 @@ #include #include -#include -#include - +#include "libcamera/internal/event_dispatcher.h" #include "libcamera/internal/process.h" #include "libcamera/internal/thread.h" +#include "libcamera/internal/timer.h" #include "libcamera/internal/utils.h" #include "test.h" diff --git a/test/timer-thread.cpp b/test/timer-thread.cpp index f794d8e7dd4e..be29eee1bdb6 100644 --- a/test/timer-thread.cpp +++ b/test/timer-thread.cpp @@ -8,10 +8,9 @@ #include #include -#include -#include - +#include "libcamera/internal/event_dispatcher.h" #include "libcamera/internal/thread.h" +#include "libcamera/internal/timer.h" #include "test.h" diff --git a/test/timer.cpp b/test/timer.cpp index 537489d736ad..fc90b11040d8 100644 --- a/test/timer.cpp +++ b/test/timer.cpp @@ -8,10 +8,9 @@ #include #include -#include -#include - +#include "libcamera/internal/event_dispatcher.h" #include "libcamera/internal/thread.h" +#include "libcamera/internal/timer.h" #include "test.h" diff --git a/test/v4l2_videodevice/buffer_sharing.cpp b/test/v4l2_videodevice/buffer_sharing.cpp index 5e7e2d97f011..c75259be0d10 100644 --- a/test/v4l2_videodevice/buffer_sharing.cpp +++ b/test/v4l2_videodevice/buffer_sharing.cpp @@ -13,10 +13,10 @@ #include #include -#include -#include +#include "libcamera/internal/event_dispatcher.h" #include "libcamera/internal/thread.h" +#include "libcamera/internal/timer.h" #include "v4l2_videodevice_test.h" diff --git a/test/v4l2_videodevice/capture_async.cpp b/test/v4l2_videodevice/capture_async.cpp index 2b38102e9cad..accdb34a699c 100644 --- a/test/v4l2_videodevice/capture_async.cpp +++ b/test/v4l2_videodevice/capture_async.cpp @@ -8,10 +8,10 @@ #include #include -#include -#include +#include "libcamera/internal/event_dispatcher.h" #include "libcamera/internal/thread.h" +#include "libcamera/internal/timer.h" #include "v4l2_videodevice_test.h" diff --git a/test/v4l2_videodevice/v4l2_m2mdevice.cpp b/test/v4l2_videodevice/v4l2_m2mdevice.cpp index 616261e11c71..e0f068082c01 100644 --- a/test/v4l2_videodevice/v4l2_m2mdevice.cpp +++ b/test/v4l2_videodevice/v4l2_m2mdevice.cpp @@ -8,12 +8,12 @@ #include #include -#include -#include #include "libcamera/internal/device_enumerator.h" +#include "libcamera/internal/event_dispatcher.h" #include "libcamera/internal/media_device.h" #include "libcamera/internal/thread.h" +#include "libcamera/internal/timer.h" #include "libcamera/internal/v4l2_videodevice.h" #include "test.h" 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__ */ From patchwork Fri Nov 13 06:38:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 10420 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 0DB70BE087 for ; Fri, 13 Nov 2020 06:38:29 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id CEDA1631BB; 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="bZOpT2nl"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BBDD0631AF 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 564E931A for ; Fri, 13 Nov 2020 07:38:25 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1605249505; bh=OT5kCj+F5K1amOrwit6pzf8qkWfAK4e28PKcTJzGQlg=; h=From:To:Subject:Date:In-Reply-To:References:From; b=bZOpT2nlhrbtvTppjqR4dRp0BUcVurBxnmHRIIdpsK8N0PfGPXqAH2wpT20p+0PxG V5zksVfAqd16/pelzWsROaeAkyQbm96HeU+LfwLcaRvCCLRp2h7SOruDpa9/9/1BTm xYOycrbBN1dad73Pxdnc9m9+whhkD/bcvhZtFipM= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Fri, 13 Nov 2020 08:38:15 +0200 Message-Id: <20201113063815.10288-6-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 5/5] cam: Move request processing to main thread 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" The request completion handler is invoked in the camera manager thread, which shouldn't be blocked for large amounts of time. As writing the frames to disk can be a time-consuming process, move request processing to the main thread by queueing an event to the event loop. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Niklas Söderlund --- src/cam/capture.cpp | 9 +++++++++ src/cam/capture.h | 1 + 2 files changed, 10 insertions(+) diff --git a/src/cam/capture.cpp b/src/cam/capture.cpp index 7580f798288c..43b109d099f6 100644 --- a/src/cam/capture.cpp +++ b/src/cam/capture.cpp @@ -157,6 +157,15 @@ void Capture::requestComplete(Request *request) if (request->status() == Request::RequestCancelled) return; + /* + * Defer processing of the completed request to the event loop, to avoid + * blocking the camera manager thread. + */ + loop_->callLater(std::bind(&Capture::processRequest, this, request)); +} + +void Capture::processRequest(Request *request) +{ const Request::BufferMap &buffers = request->buffers(); /* diff --git a/src/cam/capture.h b/src/cam/capture.h index 45e5e8a9ba27..d21c95a26ce7 100644 --- a/src/cam/capture.h +++ b/src/cam/capture.h @@ -33,6 +33,7 @@ private: int capture(libcamera::FrameBufferAllocator *allocator); void requestComplete(libcamera::Request *request); + void processRequest(libcamera::Request *request); std::shared_ptr camera_; libcamera::CameraConfiguration *config_;