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;