From patchwork Mon Jan 20 00:24:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2689 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2260A607C4 for ; Mon, 20 Jan 2020 01:24:43 +0100 (CET) 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 BD9A6563 for ; Mon, 20 Jan 2020 01:24:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1579479882; bh=NQ6JOWDLOr67iSP/Erm1ZtqOU7E7ppsS0wuyFcdBQBA=; h=From:To:Subject:Date:In-Reply-To:References:From; b=dAxbqBKFDHJiZUpdVhU+E5qMnALCINBcU2lwstC5pyHhVpeyXywWvf5c8GrK9zJP3 KjerVz7ae8vESyyfZpf/ZHUhZnBZ7izfHlrsRQNh4c2t37ILzcesn978SuCuHYFIro NOYOALD6AB/YL6DzVAFVjk776eP9rX4HNiRa6NfA= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Mon, 20 Jan 2020 02:24:20 +0200 Message-Id: <20200120002437.6633-3-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200120002437.6633-1-laurent.pinchart@ideasonboard.com> References: <20200120002437.6633-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 02/19] libcamera: bound_method: Avoid deadlock with ConnectionTypeBlocking 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: , X-List-Received-Date: Mon, 20 Jan 2020 00:24:43 -0000 ConnectionTypeBlocking always invokes the method through inter-thread message passing, which results in deadlocks if the sender and receiver live in the same thread. The deadlock can easily be avoided by turning the invocation into a direct call in this case. Do so to make ConnectionTypeBlocking easier to use when some of the senders live in the same thread as the receiver while the other senders don't. Extend the object-invoke test to cover this usage. While at it reformat the documentation to avoid long \brief lines. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund Reviewed-by: Jacopo Mondi --- src/libcamera/bound_method.cpp | 22 ++++++++++++++-------- test/object-invoke.cpp | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/libcamera/bound_method.cpp b/src/libcamera/bound_method.cpp index e18c2eb4c68e..9aa59dc3678f 100644 --- a/src/libcamera/bound_method.cpp +++ b/src/libcamera/bound_method.cpp @@ -35,16 +35,19 @@ namespace libcamera { * thread. * * \var ConnectionType::ConnectionTypeQueued - * \brief The receiver is invoked asynchronously in its thread when control - * returns to the thread's event loop. The sender proceeds without waiting for - * the invocation to complete. + * \brief The receiver is invoked asynchronously + * + * Invoke the receiver asynchronously in its thread when control returns to the + * thread's event loop. The sender proceeds without waiting for the invocation + * to complete. * * \var ConnectionType::ConnectionTypeBlocking - * \brief The receiver is invoked asynchronously in its thread when control - * returns to the thread's event loop. The sender blocks until the receiver - * signals the completion of the invocation. This connection type shall not be - * used when the sender and receiver live in the same thread, otherwise - * deadlock will occur. + * \brief The receiver is invoked synchronously + * + * If the sender and the receiver live in the same thread, this is equivalent to + * ConnectionTypeDirect. Otherwise, the receiver is invoked asynchronously in + * its thread when control returns to the thread's event loop. The sender + * blocks until the receiver signals the completion of the invocation. */ /** @@ -71,6 +74,9 @@ bool BoundMethodBase::activatePack(std::shared_ptr pack, type = ConnectionTypeDirect; else type = ConnectionTypeQueued; + } else if (type == ConnectionTypeBlocking) { + if (Thread::current() == object_->thread()) + type = ConnectionTypeDirect; } switch (type) { diff --git a/test/object-invoke.cpp b/test/object-invoke.cpp index 8e2055ca620f..fa162c838c78 100644 --- a/test/object-invoke.cpp +++ b/test/object-invoke.cpp @@ -100,6 +100,26 @@ protected: return TestFail; } + /* + * Test that blocking invocation is delivered directly when the + * caller and callee live in the same thread. + */ + object_.reset(); + + object_.invokeMethod(&InvokedObject::method, + ConnectionTypeBlocking, 42); + + switch (object_.status()) { + case InvokedObject::NoCall: + cout << "Method not invoked for main thread (blocking)" << endl; + return TestFail; + case InvokedObject::InvalidThread: + cout << "Method invoked in incorrect thread for main thread (blocking)" << endl; + return TestFail; + default: + break; + } + /* * Move the object to a thread and verify that auto method * invocation is delivered in the correct thread.