Patch Detail
Show a patch.
GET /api/patches/19460/?format=api
{ "id": 19460, "url": "https://patchwork.libcamera.org/api/patches/19460/?format=api", "web_url": "https://patchwork.libcamera.org/patch/19460/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20240123011249.22716-13-laurent.pinchart@ideasonboard.com>", "date": "2024-01-23T01:12:49", "name": "[v2,12/12] libcamera: object: Add and use thread-bound assertion", "commit_ref": null, "pull_url": null, "state": "accepted", "archived": false, "hash": "6dfb60f9d8a124e7677a90aa7c0ed2b9b1f4a6bf", "submitter": { "id": 2, "url": "https://patchwork.libcamera.org/api/people/2/?format=api", "name": "Laurent Pinchart", "email": "laurent.pinchart@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/19460/mbox/", "series": [ { "id": 4157, "url": "https://patchwork.libcamera.org/api/series/4157/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4157", "date": "2024-01-23T01:12:37", "name": "libcamera: Hardening against thread race conditions", "version": 2, "mbox": "https://patchwork.libcamera.org/series/4157/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/19460/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/19460/checks/", "tags": {}, "headers": { "Return-Path": "<libcamera-devel-bounces@lists.libcamera.org>", "X-Original-To": "parsemail@patchwork.libcamera.org", "Delivered-To": "parsemail@patchwork.libcamera.org", "Received": [ "from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 7612FC324D\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 23 Jan 2024 01:13:06 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 0A92562950;\n\tTue, 23 Jan 2024 02:13:06 +0100 (CET)", "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 0D65A62973\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 23 Jan 2024 02:13:02 +0100 (CET)", "from pendragon.ideasonboard.com (89-27-53-110.bb.dnainternet.fi\n\t[89.27.53.110])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id B6143188E;\n\tTue, 23 Jan 2024 02:11:48 +0100 (CET)" ], "Authentication-Results": "lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"K5SE5yQB\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1705972308;\n\tbh=vWwEaa5AmxWhUWEB93AXIdNgpOby95jk/FitnDlkli8=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=K5SE5yQBa/kX2VQviFIaUSEoaH3ZczUHzWDzF4bJYwx3xRCjHe5QW3ZbdK/Xj4FzM\n\ty8EJJ/wztUWK7HjR4Yr4I8p9SDPESUscpYLhhFVYYhToH89m+6AwdW7X4xODn6/440\n\t2z+eqDOWrm50ttuaL0swETP0chQLjHJ3/vc2QT7I=", "From": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>", "To": "libcamera-devel@lists.libcamera.org", "Subject": "[PATCH v2 12/12] libcamera: object: Add and use thread-bound\n\tassertion", "Date": "Tue, 23 Jan 2024 03:12:49 +0200", "Message-ID": "<20240123011249.22716-13-laurent.pinchart@ideasonboard.com>", "X-Mailer": "git-send-email 2.43.0", "In-Reply-To": "<20240123011249.22716-1-laurent.pinchart@ideasonboard.com>", "References": "<20240123011249.22716-1-laurent.pinchart@ideasonboard.com>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "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>", "Errors-To": "libcamera-devel-bounces@lists.libcamera.org", "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>" }, "content": "Several functions in libcamera classes are marked as thread-bound,\nrestricting the contexts in which those functions can be called. There\nis no infrastructure to enforce these restrictions, causing difficult to\ndebug race conditions when they are not met by callers.\n\nAs a first step to solve this, add an assertThreadBound() protected\nfunction to the Object class to test if the calling thread context is\nvalid, and use it in member functions of Object subclasses marked as\nthread-bound. This replaces manual tests in a few locations.\n\nThe thread-bound member functions of classes that do not inherit from\nObject are not checked, and neither are the functions of classes marked\nas thread-bound at the class level. These issue should be addressed in\nthe future.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\nChanges since v1:\n\n- Fix typo in commit message\n---\n include/libcamera/base/object.h | 2 ++\n src/libcamera/base/event_notifier.cpp | 6 +++++\n src/libcamera/base/object.cpp | 32 ++++++++++++++++++++++++++-\n src/libcamera/base/timer.cpp | 10 +++------\n 4 files changed, 42 insertions(+), 8 deletions(-)", "diff": "diff --git a/include/libcamera/base/object.h b/include/libcamera/base/object.h\nindex 933336361155..cb7e0a132be2 100644\n--- a/include/libcamera/base/object.h\n+++ b/include/libcamera/base/object.h\n@@ -49,6 +49,8 @@ public:\n protected:\n \tvirtual void message(Message *msg);\n \n+\tbool assertThreadBound(const char *message);\n+\n private:\n \tfriend class SignalBase;\n \tfriend class Thread;\ndiff --git a/src/libcamera/base/event_notifier.cpp b/src/libcamera/base/event_notifier.cpp\nindex fd93c0878c6f..a519aec38efb 100644\n--- a/src/libcamera/base/event_notifier.cpp\n+++ b/src/libcamera/base/event_notifier.cpp\n@@ -8,6 +8,7 @@\n #include <libcamera/base/event_notifier.h>\n \n #include <libcamera/base/event_dispatcher.h>\n+#include <libcamera/base/log.h>\n #include <libcamera/base/message.h>\n #include <libcamera/base/thread.h>\n \n@@ -20,6 +21,8 @@\n \n namespace libcamera {\n \n+LOG_DECLARE_CATEGORY(Event)\n+\n /**\n * \\class EventNotifier\n * \\brief Notify of activity on a file descriptor\n@@ -104,6 +107,9 @@ EventNotifier::~EventNotifier()\n */\n void EventNotifier::setEnabled(bool enable)\n {\n+\tif (!assertThreadBound(\"EventNotifier can't be enabled from another thread\"))\n+\t\treturn;\n+\n \tif (enabled_ == enable)\n \t\treturn;\n \ndiff --git a/src/libcamera/base/object.cpp b/src/libcamera/base/object.cpp\nindex c6040fc6a78b..81054b5833a3 100644\n--- a/src/libcamera/base/object.cpp\n+++ b/src/libcamera/base/object.cpp\n@@ -225,6 +225,35 @@ void Object::message(Message *msg)\n \t}\n }\n \n+/**\n+ * \\fn Object::assertThreadBound()\n+ * \\brief Check if the caller complies with thread-bound constraints\n+ * \\param[in] message The message to be printed on error\n+ *\n+ * This function verifies the calling constraints required by the \\threadbound\n+ * definition. It shall be called at the beginning of member functions of an\n+ * Object subclass that are explicitly marked as thread-bound in their\n+ * documentation.\n+ *\n+ * If the thread-bound constraints are not met, the function prints \\a message\n+ * as an error message. For debug builds, it additionally causes an assertion\n+ * error.\n+ *\n+ * \\todo Verify the thread-bound requirements for functions marked as\n+ * thread-bound at the class level.\n+ *\n+ * \\return True if the call is thread-bound compliant, false otherwise\n+ */\n+bool Object::assertThreadBound(const char *message)\n+{\n+\tif (Thread::current() == thread_)\n+\t\treturn true;\n+\n+\tLOG(Object, Error) << message;\n+\tASSERT(false);\n+\treturn false;\n+}\n+\n /**\n * \\fn R Object::invokeMethod()\n * \\brief Invoke a method asynchronously on an Object instance\n@@ -276,7 +305,8 @@ void Object::message(Message *msg)\n */\n void Object::moveToThread(Thread *thread)\n {\n-\tASSERT(Thread::current() == thread_);\n+\tif (!assertThreadBound(\"Object can't be moved from another thread\"))\n+\t\treturn;\n \n \tif (thread_ == thread)\n \t\treturn;\ndiff --git a/src/libcamera/base/timer.cpp b/src/libcamera/base/timer.cpp\nindex 74b060af32ff..24dbf1e892c3 100644\n--- a/src/libcamera/base/timer.cpp\n+++ b/src/libcamera/base/timer.cpp\n@@ -85,10 +85,8 @@ void Timer::start(std::chrono::milliseconds duration)\n */\n void Timer::start(std::chrono::steady_clock::time_point deadline)\n {\n-\tif (Thread::current() != thread()) {\n-\t\tLOG(Timer, Error) << \"Timer \" << this << \" << can't be started from another thread\";\n+\tif (!assertThreadBound(\"Timer can't be started from another thread\"))\n \t\treturn;\n-\t}\n \n \tdeadline_ = deadline;\n \n@@ -114,13 +112,11 @@ void Timer::start(std::chrono::steady_clock::time_point deadline)\n */\n void Timer::stop()\n {\n-\tif (!isRunning())\n+\tif (!assertThreadBound(\"Timer can't be stopped from another thread\"))\n \t\treturn;\n \n-\tif (Thread::current() != thread()) {\n-\t\tLOG(Timer, Error) << \"Timer \" << this << \" can't be stopped from another thread\";\n+\tif (!isRunning())\n \t\treturn;\n-\t}\n \n \tunregisterTimer();\n }\n", "prefixes": [ "v2", "12/12" ] }