{"id":2686,"url":"https://patchwork.libcamera.org/api/patches/2686/?format=json","web_url":"https://patchwork.libcamera.org/patch/2686/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200118213603.16888-1-laurent.pinchart@ideasonboard.com>","date":"2020-01-18T21:36:03","name":"[libcamera-devel] libcamera: bound_method: Avoid deadlock with ConnectionTypeBlocking","commit_ref":"8034af7423e0b9f00c84eaa00cd095dbcf44f4a5","pull_url":null,"state":"accepted","archived":false,"hash":"c08c2153dbf7e3e06374c5191efaad22f1a1cef7","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/2686/mbox/","series":[{"id":640,"url":"https://patchwork.libcamera.org/api/series/640/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=640","date":"2020-01-18T21:36:03","name":"[libcamera-devel] libcamera: bound_method: Avoid deadlock with ConnectionTypeBlocking","version":1,"mbox":"https://patchwork.libcamera.org/series/640/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2686/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2686/checks/","tags":{},"headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 45E62607BA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 18 Jan 2020 22:36:21 +0100 (CET)","from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id E291297A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 18 Jan 2020 22:36:20 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1579383381;\n\tbh=UQLUtb90QWp3LVf1jGG+eoDgW5+bMc13qqgR+HgRMvE=;\n\th=From:To:Subject:Date:From;\n\tb=JHsv2fIHMhdausm7XvXXVOYVthasxdF5lYrhLRuhWXbuPXESsRlEORzxjB2QKk2d7\n\tMhnnsnEmsru4sEFDjXTHVXo5ZU/XR7vkv1N/8qklcTzZB5zbWZBRDcXWRd4dmqBuvP\n\ttSNvq397Pab2MTq3QoRHipjkAElwT00hLS89BFqE=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Sat, 18 Jan 2020 23:36:03 +0200","Message-Id":"<20200118213603.16888-1-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.24.1","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH] libcamera: bound_method: Avoid deadlock\n\twith ConnectionTypeBlocking","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Sat, 18 Jan 2020 21:36:21 -0000"},"content":"ConnectionTypeBlocking always invokes the method through inter-thread\nmessage passing, which results in deadlocks if the sender and receiver\nlive in the same thread. The deadlock can easily be avoided by turning\nthe invocation into a direct call in this case. Do so to make\nConnectionTypeBlocking easier to use when some of the senders live in\nthe same thread as the receiver while the other senders don't.\n\nExtend the object-invoke test to cover this usage.\n\nWhile at it reformat the documentation to avoid long \\brief lines.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/libcamera/bound_method.cpp | 22 ++++++++++++++--------\n test/object-invoke.cpp         | 20 ++++++++++++++++++++\n 2 files changed, 34 insertions(+), 8 deletions(-)","diff":"diff --git a/src/libcamera/bound_method.cpp b/src/libcamera/bound_method.cpp\nindex e18c2eb4c68e..9aa59dc3678f 100644\n--- a/src/libcamera/bound_method.cpp\n+++ b/src/libcamera/bound_method.cpp\n@@ -35,16 +35,19 @@ namespace libcamera {\n  * thread.\n  *\n  * \\var ConnectionType::ConnectionTypeQueued\n- * \\brief The receiver is invoked asynchronously in its thread when control\n- * returns to the thread's event loop. The sender proceeds without waiting for\n- * the invocation to complete.\n+ * \\brief The receiver is invoked asynchronously\n+ *\n+ * Invoke the receiver asynchronously in its thread when control returns to the\n+ * thread's event loop. The sender proceeds without waiting for the invocation\n+ * to complete.\n  *\n  * \\var ConnectionType::ConnectionTypeBlocking\n- * \\brief The receiver is invoked asynchronously in its thread when control\n- * returns to the thread's event loop. The sender blocks until the receiver\n- * signals the completion of the invocation. This connection type shall not be\n- * used when the sender and receiver live in the same thread, otherwise\n- * deadlock will occur.\n+ * \\brief The receiver is invoked synchronously\n+ *\n+ * If the sender and the receiver live in the same thread, this is equivalent to\n+ * ConnectionTypeDirect. Otherwise, the receiver is invoked asynchronously in\n+ * its thread when control returns to the thread's event loop. The sender\n+ * blocks until the receiver signals the completion of the invocation.\n  */\n \n /**\n@@ -71,6 +74,9 @@ bool BoundMethodBase::activatePack(std::shared_ptr<BoundMethodPackBase> pack,\n \t\t\ttype = ConnectionTypeDirect;\n \t\telse\n \t\t\ttype = ConnectionTypeQueued;\n+\t} else if (type == ConnectionTypeBlocking) {\n+\t\tif (Thread::current() == object_->thread())\n+\t\t\ttype = ConnectionTypeDirect;\n \t}\n \n \tswitch (type) {\ndiff --git a/test/object-invoke.cpp b/test/object-invoke.cpp\nindex 8e2055ca620f..fa162c838c78 100644\n--- a/test/object-invoke.cpp\n+++ b/test/object-invoke.cpp\n@@ -100,6 +100,26 @@ protected:\n \t\t\treturn TestFail;\n \t\t}\n \n+\t\t/*\n+\t\t * Test that blocking invocation is delivered directly when the\n+\t\t * caller and callee live in the same thread.\n+\t\t */\n+\t\tobject_.reset();\n+\n+\t\tobject_.invokeMethod(&InvokedObject::method,\n+\t\t\t\t     ConnectionTypeBlocking, 42);\n+\n+\t\tswitch (object_.status()) {\n+\t\tcase InvokedObject::NoCall:\n+\t\t\tcout << \"Method not invoked for main thread (blocking)\" << endl;\n+\t\t\treturn TestFail;\n+\t\tcase InvokedObject::InvalidThread:\n+\t\t\tcout << \"Method invoked in incorrect thread for main thread (blocking)\" << endl;\n+\t\t\treturn TestFail;\n+\t\tdefault:\n+\t\t\tbreak;\n+\t\t}\n+\n \t\t/*\n \t\t * Move the object to a thread and verify that auto method\n \t\t * invocation is delivered in the correct thread.\n","prefixes":["libcamera-devel"]}