From patchwork Tue Jul 28 10:57:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 9042 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 62448BD878 for ; Tue, 28 Jul 2020 10:57:27 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 32CA161759; Tue, 28 Jul 2020 12:57:27 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=uajain.com header.i=@uajain.com header.b="Nwar5nty"; dkim-atps=neutral Received: from o1.f.az.sendgrid.net (o1.f.az.sendgrid.net [208.117.55.132]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E575A6039B for ; Tue, 28 Jul 2020 12:57:25 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uajain.com; h=from:subject:in-reply-to:references:mime-version:to:cc: content-transfer-encoding:content-type; s=s1; bh=VeeHnwew9gS/uG5mNf1I1wZ/euvkn7rIzWzPRgxZQ3A=; b=Nwar5ntymOSRnFj1BlfSxWQE22w2/XMHidnNOZ1kId51O8rqplhcwhSmJf5siK3Httfb FVqZWhHTFQbCW0typYnVKfXAAzIjI3+2FFxwm9UyRW3j0PWP/WpbVqAEO/8G4BCVEW9Yhg D4LuvQjw5w920eV+yZ+hWtBeVsFWp6yEY= Received: by filterdrecv-p3iad2-5b55dcd864-84x54 with SMTP id filterdrecv-p3iad2-5b55dcd864-84x54-18-5F200494-18 2020-07-28 10:57:24.479978629 +0000 UTC m=+2742475.743905900 Received: from mail.uajain.com (unknown) by ismtpd0001p1maa1.sendgrid.net (SG) with ESMTP id vP320OecRi6MsKsG2oGnqA for ; Tue, 28 Jul 2020 10:57:23.770 +0000 (UTC) From: Umang Jain Date: Tue, 28 Jul 2020 10:57:24 +0000 (UTC) Message-Id: <20200728105541.13326-2-email@uajain.com> In-Reply-To: <20200728105541.13326-1-email@uajain.com> References: <20200728105541.13326-1-email@uajain.com> Mime-Version: 1.0 X-SG-EID: 1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcHFp8snzxo7RIC2IEYmqCh5+cgNe1vrAZATSbtvOdAd8W9VO/L9q/pYMJ+oKs4itKpcNTySbNdT0iiOjLJzsiN+2dZHSVReLIxAagA4UM+19AKMWUe57r9SPa00XVsIXCvbUHwobq2r7rbU91slABW5zDdv26hWSRcmfacKrEGfeYVy1DsWrhrGy72Oj9sFLbEKs+RBQNgHVRq07ohBxjgA== To: libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [PATCH 1/2] libcamera: object: Add deleteLater() 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" This commit adds support to schedule the deletion of an Object to the thread it is bound to (similar to [1]). An Object getting destroyed by a different thread, is considered as a violation as per the threading model. This will be useful for an Object where its ownership is shared via shared pointers in different threads. If the thread which drops the last reference of the Object is a different thread, the destructors get called in that particular thread, not the one Object is bound to. Hence, in order to resolve this kind of situation, the creation of shared pointer can be accompanied by a custom deleter which in turns use deleteLater() to ensure the Object is destroyed in its own thread. [1] https://doc.qt.io/qt-5/qobject.html#deleteLater Signed-off-by: Umang Jain Reviewed-by: Laurent Pinchart --- include/libcamera/internal/message.h | 1 + include/libcamera/object.h | 2 ++ src/libcamera/object.cpp | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/include/libcamera/internal/message.h b/include/libcamera/internal/message.h index 92ea64a..f1b133b 100644 --- a/include/libcamera/internal/message.h +++ b/include/libcamera/internal/message.h @@ -25,6 +25,7 @@ public: None = 0, InvokeMessage = 1, ThreadMoveMessage = 2, + DeferredDelete = 3, UserMessage = 1000, }; diff --git a/include/libcamera/object.h b/include/libcamera/object.h index 9a3dd07..a1882f0 100644 --- a/include/libcamera/object.h +++ b/include/libcamera/object.h @@ -27,6 +27,8 @@ public: Object(Object *parent = nullptr); virtual ~Object(); + void deleteLater(); + void postMessage(std::unique_ptr msg); templateparent_ = nullptr; } +/** + * \brief Defer the deletion of Object instance to their thread + * + * Schedule deletion of the Object in the thread to which they are bound. + * This will prevent destroying the Object from a different thread, which is + * not allowed by the threading model. + * + * It will typically be used with a custom deleter when creating a shared + * pointer. It can then be ensured that the Object is deleted in its + * own thread even if its last reference is dropped in a different thread. + */ +void Object::deleteLater() +{ + postMessage(std::make_unique(Message::DeferredDelete)); +} + /** * \brief Post a message to the object's thread * \param[in] msg The message @@ -143,6 +159,10 @@ void Object::message(Message *msg) break; } + case Message::DeferredDelete: { + delete this; + break; + } default: break; From patchwork Tue Jul 28 10:57:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 9043 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 E48B8BD878 for ; Tue, 28 Jul 2020 10:57:29 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id AE1D8613C6; Tue, 28 Jul 2020 12:57:29 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=uajain.com header.i=@uajain.com header.b="bkXCQ4FK"; dkim-atps=neutral Received: from o1.f.az.sendgrid.net (o1.f.az.sendgrid.net [208.117.55.132]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2DCAF616FF for ; Tue, 28 Jul 2020 12:57:27 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uajain.com; h=from:subject:in-reply-to:references:mime-version:to:cc: content-transfer-encoding:content-type; s=s1; bh=LKCN+mo/nQECRcf0uO/mA1y+PjqUsP1EpSA93LxFroY=; b=bkXCQ4FKggoC8whK4vzVIagKwJLbEFHPSGdTwcz1aV4hf8KCC2HeSUaag/8QdZr3Nv0g 0pW3Zrfx8rrr6iCqKQVyYG9glZbBc2vOc0evkk5nK0n8elU+kvMM2G9wNjT5bSsZPDxeht kEBzh9+USozgbRm6ZqaTqp4sFy5I6QP8I= Received: by filterdrecv-p3mdw1-75c584b9c6-q9wps with SMTP id filterdrecv-p3mdw1-75c584b9c6-q9wps-21-5F200495-4C 2020-07-28 10:57:25.902086811 +0000 UTC m=+2742468.798483178 Received: from mail.uajain.com (unknown) by ismtpd0005p1maa1.sendgrid.net (SG) with ESMTP id V5L735IlQI6Vdnx2fPbSrQ for ; Tue, 28 Jul 2020 10:57:25.496 +0000 (UTC) From: Umang Jain Date: Tue, 28 Jul 2020 10:57:25 +0000 (UTC) Message-Id: <20200728105541.13326-3-email@uajain.com> In-Reply-To: <20200728105541.13326-1-email@uajain.com> References: <20200728105541.13326-1-email@uajain.com> Mime-Version: 1.0 X-SG-EID: 1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPc/qk5sBUbMs3SDc71Zia+EPPl8QFrE3sWnDV6QCaTwNApz2N86mNxUJ0CEM2nVAO4uPigtjdaq0fQErk0X4IuyMqCQpDBXU/HGO3jBYfp1Zs0F9smtLq97JRdtgwGWI20x5BKhpUTfRAvx0oqlewpBqgUQ16jvxVOwMoDNEwQ4J38YV7ceXs/XYYRNITyMD9WQg5g3fcFl1/1X756JpnDSw== To: libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [PATCH 2/2] libcamera: camera: Ensure deletion via deleteLater() 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" Object::deleteLater() ensures that the deletion of the Object takes place in a thread it is bound to. Deleting the Object in a different thread is a violation according to the threading model. On hot-unplug of a currently streaming camera, the last reference of Camera when dropped from the application thread (for e.g. QCam's thread), the destructor is then called from this thread. This is not allowed by the threading model. Camera is meant to be deleted in the thread it is bound to - in this case the CameraManager's thread. Signed-off-by: Umang Jain Reviewed-by: Laurent Pinchart Reviewed-by: Laurent Pinchart --- include/libcamera/camera.h | 2 +- src/libcamera/camera.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/libcamera/camera.h b/include/libcamera/camera.h index 4d1a4a9..beb87e6 100644 --- a/include/libcamera/camera.h +++ b/include/libcamera/camera.h @@ -66,7 +66,7 @@ protected: std::vector config_; }; -class Camera final : public std::enable_shared_from_this +class Camera final : public Object, public std::enable_shared_from_this { public: static std::shared_ptr create(PipelineHandler *pipe, diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp index 69a1b44..034f341 100644 --- a/src/libcamera/camera.cpp +++ b/src/libcamera/camera.cpp @@ -464,7 +464,7 @@ std::shared_ptr Camera::create(PipelineHandler *pipe, struct Deleter : std::default_delete { void operator()(Camera *camera) { - delete camera; + camera->deleteLater(); } };