From patchwork Fri Jul 31 13:39:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 9088 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 EB7BABD878 for ; Fri, 31 Jul 2020 13:39:57 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B9ACA61EAB; Fri, 31 Jul 2020 15:39:57 +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="Lc1MhkH1"; 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 E0220611A2 for ; Fri, 31 Jul 2020 15:39:54 +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=rZO56G1jUXqkj9NaKsVMgBkSiSxVvWJQQydjtgOydGI=; b=Lc1MhkH1Xa+u7CXv8UYhENYgruO8TwtBvliP3PVjf7CF4aWxy/gDArdF3OEDPyfsCNTO hdhYWAOfRHccfMS6C61yLKw041PKh9uWBLymGsQ2g4q1BDWPVRlMGH1oUyEhdm4GZVbLVN Co6mZVF2wq46bu1WZ3ESUgrvZiEoTBt7A= Received: by filterdrecv-p3iad2-d8cc6d457-vqmvz with SMTP id filterdrecv-p3iad2-d8cc6d457-vqmvz-18-5F241F29-33 2020-07-31 13:39:53.496836441 +0000 UTC m=+156699.076970626 Received: from mail.uajain.com (unknown) by ismtpd0001p1hnd1.sendgrid.net (SG) with ESMTP id p9ZLO7eTSYKmzjEb5kIrCg for ; Fri, 31 Jul 2020 13:39:53.166 +0000 (UTC) From: Umang Jain Date: Fri, 31 Jul 2020 13:39:53 +0000 (UTC) Message-Id: <20200731133947.84258-2-email@uajain.com> In-Reply-To: <20200731133947.84258-1-email@uajain.com> References: <20200731133947.84258-1-email@uajain.com> Mime-Version: 1.0 X-SG-EID: 1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPc/mSirh79xX0l6HRHZb2eYOh3j8GimY8FU2cDIu5BK9uo/u0UKfCGuWra26U2h3bDlEbUIjGFk2ET8Ge+vVnCPS/P0zdlLL4BRggwPsGX7JzaF9gLM5pHujKJmnaCPEm9lvOwwwjLj+pfMnEEpk9BllyIZyptbccBfCh6TWaAvcZdKKXREpniq0kMeouKIgKbjvAfd8lOmWANZB2biby2TQ== To: libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [PATCH v3 1/3] libcamera: thread: Support selective message dispatch to 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" Extend the current dispatchMessages() to support dispatching of selective messsages according to the Message::Type passed in the function argument. dispatchMessages() can now be called explicitly to force deliver selected type's message to the thread for processing (typically when event loop is not running). Signed-off-by: Umang Jain Reviewed-by: Laurent Pinchart --- include/libcamera/internal/thread.h | 3 ++- src/libcamera/thread.cpp | 30 ++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/include/libcamera/internal/thread.h b/include/libcamera/internal/thread.h index 7b59e58..f6367a8 100644 --- a/include/libcamera/internal/thread.h +++ b/include/libcamera/internal/thread.h @@ -14,6 +14,7 @@ #include +#include "libcamera/internal/message.h" #include "libcamera/internal/utils.h" namespace libcamera { @@ -47,7 +48,7 @@ public: EventDispatcher *eventDispatcher(); void setEventDispatcher(std::unique_ptr dispatcher); - void dispatchMessages(); + void dispatchMessages(Message::Type type = Message::Type::None); protected: int exec(); diff --git a/src/libcamera/thread.cpp b/src/libcamera/thread.cpp index d1750d7..7e318d9 100644 --- a/src/libcamera/thread.cpp +++ b/src/libcamera/thread.cpp @@ -552,25 +552,37 @@ void Thread::removeMessages(Object *receiver) } /** - * \brief Dispatch all posted messages for this thread + * \brief Dispatch posted messages for this thread + * \param[in] type The message type + * + * This function immediately dispatches all the messages previously posted for + * this thread with postMessage() that match the message \a type. If the \a type + * is Message::Type::None, all messages are dispatched. */ -void Thread::dispatchMessages() +void Thread::dispatchMessages(Message::Type type) { MutexLocker locker(data_->messages_.mutex_); - while (!data_->messages_.list_.empty()) { - std::unique_ptr msg = std::move(data_->messages_.list_.front()); - data_->messages_.list_.pop_front(); - if (!msg) + std::list> &messages = data_->messages_.list_; + + for (auto iter = messages.begin(); iter != messages.end(); ) { + std::unique_ptr &msg = *iter; + + if (!msg || (type != Message::Type::None && msg->type() != type)) { + ++iter; continue; + } - Object *receiver = msg->receiver_; - ASSERT(data_ == receiver->thread()->data_); + std::unique_ptr message = std::move(msg); + iter = data_->messages_.list_.erase(iter); + Object *receiver = message->receiver_; + ASSERT(data_ == receiver->thread()->data_); receiver->pendingMessages_--; locker.unlock(); - receiver->message(msg.get()); + receiver->message(message.get()); + message.reset(); locker.lock(); } } From patchwork Fri Jul 31 13:39:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 9089 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 61267BD878 for ; Fri, 31 Jul 2020 13:39:59 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2F85C61ED2; Fri, 31 Jul 2020 15:39:59 +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="bzez5/Wr"; 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 D133F616FF for ; Fri, 31 Jul 2020 15:39:56 +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=MGQPbAd0XKQDUt1FWMgX/SrB2ThHIz3/DdC4y9z43Y0=; b=bzez5/Wr5Y24p/0Tq4D6HXf1JOip9Orb9OAhMZTFUZh/mHVp7DGz+wBuAlhSSbZ4gD9Y nC2VXEH1nkq2sMov9m5KHCs20j1j7NFJBGc7ty+dsspwRHSdVlvBaDQHLMh16/Cw8rAEX3 TBRVWPy7+RpRRNogXPNm7WRzFzppxzq70= Received: by filterdrecv-p3mdw1-7ff865655c-hpdqc with SMTP id filterdrecv-p3mdw1-7ff865655c-hpdqc-19-5F241F2B-25 2020-07-31 13:39:55.564770342 +0000 UTC m=+156224.759476860 Received: from mail.uajain.com (unknown) by ismtpd0005p1maa1.sendgrid.net (SG) with ESMTP id mQXGQXJBQDqYv704DpU31Q Fri, 31 Jul 2020 13:39:54.679 +0000 (UTC) From: Umang Jain Date: Fri, 31 Jul 2020 13:39:55 +0000 (UTC) Message-Id: <20200731133947.84258-3-email@uajain.com> In-Reply-To: <20200731133947.84258-1-email@uajain.com> References: <20200731133947.84258-1-email@uajain.com> Mime-Version: 1.0 X-SG-EID: 1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcu2AmDZxpR9j2S9uUCg9Ys7krDPmWy2OqGnsRcgal8sKxTv5UymibaUbsW/VyAEP3afoHjebEFcdoq9MaUEQ8Vjg2pT6uEzAwz8VC3pVmHhnIHavlLtoAFaZIIKh/LrmDc3CL7P59uC2Ww54f0zJ/t9hbwnFxIQJ9DIVzssOmx71A7rSaY3zYv8X+oOPFhaNRIKOMc95GpJJHUc+5nnBy8w== To: libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [PATCH v3 2/3] 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 libcamera 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/message.cpp | 2 + src/libcamera/object.cpp | 48 ++++++++++ test/meson.build | 1 + test/object-delete.cpp | 125 +++++++++++++++++++++++++++ 6 files changed, 179 insertions(+) create mode 100644 test/object-delete.cpp diff --git a/include/libcamera/internal/message.h b/include/libcamera/internal/message.h index 92ea64a..98e5a28 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 Schedule deletion of the instance in the thread it belongs to + * + * This function schedules deletion of the Object when control returns to the + * event loop that the object belongs to. This ensures the object is destroyed + * from the right context, as required by the libcamera threading model. + * + * If this function is called before the thread's event loop is started, the + * object will be deleted when the event loop starts. + * + * Deferred deletion can be used to control the destruction context with shared + * pointers. An object managed with shared pointers is deleted when the last + * reference is destroyed, which makes difficult to ensure through software + * design which context the deletion will take place in. With a custom deleter + * for the shared pointer using deleteLater(), the deletion can be guaranteed to + * happen in the thread the object is bound to. + * + * \code{.cpp} + * std::shared_ptr createObject() + * { + * struct Deleter : std::default_delete { + * void operator()(MyObject *obj) + * { + * delete obj; + * } + * }; + * + * MyObject *obj = new MyObject(); + * + * return std::shared_ptr(obj, Deleter()); + * } + * \endcode + * + * \context This function is \threadsafe. + */ +void Object::deleteLater() +{ + postMessage(std::make_unique(Message::DeferredDelete)); +} + /** * \brief Post a message to the object's thread * \param[in] msg The message @@ -144,6 +188,10 @@ void Object::message(Message *msg) break; } + case Message::DeferredDelete: + delete this; + break; + default: break; } diff --git a/test/meson.build b/test/meson.build index f41d6e7..f5aa144 100644 --- a/test/meson.build +++ b/test/meson.build @@ -34,6 +34,7 @@ internal_tests = [ ['hotplug-cameras', 'hotplug-cameras.cpp'], ['message', 'message.cpp'], ['object', 'object.cpp'], + ['object-delete', 'object-delete.cpp'], ['object-invoke', 'object-invoke.cpp'], ['pixel-format', 'pixel-format.cpp'], ['signal-threads', 'signal-threads.cpp'], diff --git a/test/object-delete.cpp b/test/object-delete.cpp new file mode 100644 index 0000000..6cd1a1e --- /dev/null +++ b/test/object-delete.cpp @@ -0,0 +1,125 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Umang Jain + * + * object.cpp - Object deletion tests + */ + +#include + +#include +#include + +#include "libcamera/internal/thread.h" + +#include "test.h" + +using namespace std; +using namespace libcamera; + +/* + * Global flag to check if object is on its way to deletion from it's + * own thread only. + */ +bool ObjectDeleted = false; + +class NewThread : public Thread +{ +public: + NewThread(Object *obj = nullptr) + : object_(obj), deleteScheduled_(false) + { + } + + bool objectScheduledForDeletion() { return deleteScheduled_; } + +protected: + void run() + { + object_->deleteLater(); + deleteScheduled_ = true; + } + +private: + Object *object_; + bool deleteScheduled_; +}; + + +class TestObject : public Object +{ +public: + TestObject() + { + } + + ~TestObject() + { + if (thread() == Thread::current()) + ObjectDeleted = true; + } +}; + +class ObjectDeleteTest : public Test +{ +protected: + int init() + { + isDeleteScheduled_ = false; + + return 0; + } + + int run() + { + TestObject *obj; + + obj = new TestObject(); + NewThread thread(obj); + thread.start(); + Timer timer; + timer.start(100); + + while (timer.isRunning() && !isDeleteScheduled_) + isDeleteScheduled_ = thread.objectScheduledForDeletion(); + + thread.exit(); + thread.wait(); + + if (!isDeleteScheduled_) { + cout << "Failed to queue object for deletion from another thread" << endl; + return TestFail; + } + + Thread::current()->dispatchMessages(Message::Type::DeferredDelete); + + if (!ObjectDeleted) { + cout << "Failed to delete object queued from another thread" << endl; + return TestFail; + } + + ObjectDeleted = false; + obj = new TestObject(); + obj->deleteLater(); + /* + * \todo Devise a check for multiple deleteLater() calls to a object. + * It should be checked that the object is deleted only once. + */ + Thread::current()->dispatchMessages(Message::Type::DeferredDelete); + if(!ObjectDeleted) { + cout << "Failed to delete object"; + return TestFail; + } + + return TestPass; + } + + void cleanup() + { + } + +private: + bool isDeleteScheduled_; +}; + +TEST_REGISTER(ObjectDeleteTest) From patchwork Fri Jul 31 13:39:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 9090 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 0E1CDBD878 for ; Fri, 31 Jul 2020 13:40:00 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8EB8761EEF; Fri, 31 Jul 2020 15:39:59 +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="E8HMt7yo"; 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 EAA9D61EA4 for ; Fri, 31 Jul 2020 15:39:56 +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=Q13Y/KIA9jXepafD4MdInIVxJgHdpkJml9AgdG7ctRo=; b=E8HMt7yosv/O5SF43DjkQoJGKN62gtxVzVP1o1zXsDka6fvrK+gLf4WUdmYaxFmhAmO1 8JJuuHGysh31NZZvQm1jwXeT/F8AGDi/k0N7TAvzgSdqJL9ptYF4ZL/Gp/FLBHgrvIGh1l YgBDhFrrpmA/R0XX8a9wkkztquqm0MXNI= Received: by filterdrecv-p3iad2-d8cc6d457-tlvzw with SMTP id filterdrecv-p3iad2-d8cc6d457-tlvzw-19-5F241F2B-2A 2020-07-31 13:39:55.67984757 +0000 UTC m=+156690.394921146 Received: from mail.uajain.com (unknown) by ismtpd0001p1maa1.sendgrid.net (SG) with ESMTP id APo5bF33SdSYpXkNlB5ZcQ Fri, 31 Jul 2020 13:39:55.235 +0000 (UTC) From: Umang Jain Date: Fri, 31 Jul 2020 13:39:55 +0000 (UTC) Message-Id: <20200731133947.84258-4-email@uajain.com> In-Reply-To: <20200731133947.84258-1-email@uajain.com> References: <20200731133947.84258-1-email@uajain.com> Mime-Version: 1.0 X-SG-EID: 1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcREc2X1LpIgLW8oEasGbeg9uPR3HyoPdWA+c8k6xqhnH2yVbMfbMS9MDZuibMw2RmskFzVjMwpfjjA/Ah5AKcr1cMsEQlRCK6Hj8SG1mFRxAHATDOM1J51AiekMHBcBKn6ixU7B4tO1QcPQByENNmqq2CNN0E1GKA4oYfagZAufxjlObs93wSJLD9Ej+mRWZqndUvlHrTBeJdB24nqjqXZA== To: libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [PATCH v3 3/3] 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 --- include/libcamera/camera.h | 3 ++- src/libcamera/camera.cpp | 2 +- src/libcamera/camera_manager.cpp | 6 +++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/include/libcamera/camera.h b/include/libcamera/camera.h index 4d1a4a9..7dd23d7 100644 --- a/include/libcamera/camera.h +++ b/include/libcamera/camera.h @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -66,7 +67,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(); } }; diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp index f60491d..c45bf33 100644 --- a/src/libcamera/camera_manager.cpp +++ b/src/libcamera/camera_manager.cpp @@ -164,9 +164,13 @@ void CameraManager::Private::cleanup() /* * Release all references to cameras to ensure they all get destroyed - * before the device enumerator deletes the media devices. + * before the device enumerator deletes the media devices. Cameras are + * destroyed via Object::deleteLater() API, hence we need to explicitly + * process deletion requests from the thread's message queue as the event + * loop is not in action here. */ cameras_.clear(); + dispatchMessages(Message::Type::DeferredDelete); enumerator_.reset(nullptr); } From patchwork Fri Jul 31 15:14:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 9097 X-Patchwork-Delegate: laurent.pinchart@ideasonboard.com 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 1EAE7BD878 for ; Fri, 31 Jul 2020 15:15:42 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A11E561DE1; Fri, 31 Jul 2020 17:15:41 +0200 (CEST) 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="ADvE19ji"; 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 84AC160398 for ; Fri, 31 Jul 2020 17:15:40 +0200 (CEST) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id AEFB353C; Fri, 31 Jul 2020 17:15:39 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596208539; bh=jsaOI15SXIosTpJEs0q1SE8kaVTm+N7BCr3ISUpBs6o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ADvE19jiDI835kuYkOx4SZ8xV9mpCN/bZlR6Ajg2CunFQ1W1T4fXsXUm2ZPr6F+Qk 2GmGfs6dldZd8I1uXN099bqZl6SzI8KDFuGiC4xW1iwfKRF+virhGSX0sOICi8RwNb uA/LWq/kUpz47rMh5TD2NKUHfWARNv7pGfdSnAgQ= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Fri, 31 Jul 2020 18:14:24 +0300 Message-Id: <20200731151424.28003-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200731133947.84258-1-email@uajain.com> References: <20200731133947.84258-1-email@uajain.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3.1 4/3] test: Add Object::deleteLater() test 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" From: Umang Jain Add a test case for the Object::deleteLater() API, to verify that - the object is deleted from the correct thread - multiple deleteLater() calls delete the object once only Signed-off-by: Umang Jain Reviewed-by: Laurent Pinchart Signed-off-by: Laurent Pinchart --- test/meson.build | 1 + test/object-delete.cpp | 97 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 test/object-delete.cpp diff --git a/test/meson.build b/test/meson.build index c0fb9bdfac17..775187159dec 100644 --- a/test/meson.build +++ b/test/meson.build @@ -33,6 +33,7 @@ internal_tests = [ ['hotplug-cameras', 'hotplug-cameras.cpp'], ['message', 'message.cpp'], ['object', 'object.cpp'], + ['object-delete', 'object-delete.cpp'], ['object-invoke', 'object-invoke.cpp'], ['pixel-format', 'pixel-format.cpp'], ['signal-threads', 'signal-threads.cpp'], diff --git a/test/object-delete.cpp b/test/object-delete.cpp new file mode 100644 index 000000000000..a1a6f5ccefd5 --- /dev/null +++ b/test/object-delete.cpp @@ -0,0 +1,97 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * object.cpp - Object deletion tests + */ + +#include + +#include + +#include "libcamera/internal/thread.h" + +#include "test.h" + +using namespace std; +using namespace libcamera; + +class TestObject : public Object +{ +public: + TestObject(unsigned int *count) + : deleteCount_(count) + { + } + + ~TestObject() + { + /* Count the deletions from the correct thread. */ + if (thread() == Thread::current()) + (*deleteCount_)++; + } + + unsigned int *deleteCount_; +}; + +class NewThread : public Thread +{ +public: + NewThread(Object *obj) + : object_(obj) + { + } + +protected: + void run() + { + object_->deleteLater(); + } + +private: + Object *object_; +}; + +class ObjectDeleteTest : public Test +{ +protected: + int run() + { + /* + * Test that deferred deletion is executed from the object's + * thread, not the caller's thread. + */ + unsigned int count = 0; + TestObject *obj = new TestObject(&count); + + NewThread thread(obj); + thread.start(); + thread.wait(); + + Thread::current()->dispatchMessages(Message::Type::DeferredDelete); + + if (count != 1) { + cout << "Failed to dispatch DeferredDelete (" << count << ")" << endl; + return TestFail; + } + + /* + * Test that multiple calls to deleteLater() delete the object + * once only. + */ + count = 0; + obj = new TestObject(&count); + obj->deleteLater(); + obj->deleteLater(); + + Thread::current()->dispatchMessages(Message::Type::DeferredDelete); + if (count != 1) { + cout << "Multiple deleteLater() failed (" << count << ")" << endl; + return TestFail; + } + + return TestPass; + } +}; + +TEST_REGISTER(ObjectDeleteTest)