Show a patch.

GET /api/1.1/patches/1833/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 1833,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/1833/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/1833/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/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": "<20190817152104.10834-16-laurent.pinchart@ideasonboard.com>",
    "date": "2019-08-17T15:21:01",
    "name": "[libcamera-devel,v2,15/18] libcamera: object: Create parent-child relationships",
    "commit_ref": null,
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "dccca933511f7de1ead08a212b62234cfbceab01",
    "submitter": {
        "id": 2,
        "url": "https://patchwork.libcamera.org/api/1.1/people/2/?format=api",
        "name": "Laurent Pinchart",
        "email": "laurent.pinchart@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/1833/mbox/",
    "series": [
        {
            "id": 463,
            "url": "https://patchwork.libcamera.org/api/1.1/series/463/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=463",
            "date": "2019-08-17T15:20:46",
            "name": "Object & Thread enhancements",
            "version": 2,
            "mbox": "https://patchwork.libcamera.org/series/463/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/1833/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/1833/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<laurent.pinchart@ideasonboard.com>",
        "Received": [
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4FC4E61934\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 17 Aug 2019 17:21:18 +0200 (CEST)",
            "from pendragon.bb.dnainternet.fi\n\t(dfj612yhrgyx302h3jwwy-3.rev.dnainternet.fi\n\t[IPv6:2001:14ba:21f5:5b00:ce28:277f:58d7:3ca4])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id EA4BB556\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 17 Aug 2019 17:21:17 +0200 (CEST)"
        ],
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1566055278;\n\tbh=rajxxP0JYFZz6vqzW1m/fMCMoczU566n4DKH/Hk9Nc0=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=KmkY/wjRJWZnzuiNJSI0uS3xOfIYQJQez5XsnzE5+DJARafxbO2y8UTQ5Rq9OW43e\n\tEqCnkfLYDiq2R/EzFz95Mw8c65e1pui5zUl0mGqTQWRa0M7gvUkZMrSTB9IVFCxEUg\n\tybNZo58EKtNFGPBwx+XB76+S+OT4WG9cPdcxdy/A=",
        "From": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Sat, 17 Aug 2019 18:21:01 +0300",
        "Message-Id": "<20190817152104.10834-16-laurent.pinchart@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.21.0",
        "In-Reply-To": "<20190817152104.10834-1-laurent.pinchart@ideasonboard.com>",
        "References": "<20190817152104.10834-1-laurent.pinchart@ideasonboard.com>",
        "MIME-Version": "1.0",
        "Content-Type": "text/plain; charset=UTF-8",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v2 15/18] libcamera: object: Create\n\tparent-child relationships",
        "X-BeenThere": "libcamera-devel@lists.libcamera.org",
        "X-Mailman-Version": "2.1.23",
        "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, 17 Aug 2019 15:21:19 -0000"
    },
    "content": "Add a parent Object to Object instances, and track the parent-children\nrelationships. Children are bound to the same thread as their parent,\nand moving an Object to a thread automatically moves all its children.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Jacopo Mondi <jacopo@jmondi.org>\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n---\nChanges since v1:\n\n- Document Object::~Object()\n---\n include/libcamera/object.h     |  8 +++-\n src/libcamera/include/thread.h |  2 +\n src/libcamera/object.cpp       | 72 ++++++++++++++++++++++++++++------\n src/libcamera/thread.cpp       | 12 +++++-\n 4 files changed, 81 insertions(+), 13 deletions(-)",
    "diff": "diff --git a/include/libcamera/object.h b/include/libcamera/object.h\nindex e128c75e7db8..3308330af857 100644\n--- a/include/libcamera/object.h\n+++ b/include/libcamera/object.h\n@@ -9,6 +9,7 @@\n \n #include <list>\n #include <memory>\n+#include <vector>\n \n #include <libcamera/bound_method.h>\n \n@@ -23,7 +24,7 @@ class Thread;\n class Object\n {\n public:\n-\tObject();\n+\tObject(Object *parent = nullptr);\n \tvirtual ~Object();\n \n \tvoid postMessage(std::unique_ptr<Message> msg);\n@@ -41,6 +42,8 @@ public:\n \tThread *thread() const { return thread_; }\n \tvoid moveToThread(Thread *thread);\n \n+\tObject *parent() const { return parent_; }\n+\n protected:\n \tvirtual void message(Message *msg);\n \n@@ -57,6 +60,9 @@ private:\n \tvoid connect(SignalBase *signal);\n \tvoid disconnect(SignalBase *signal);\n \n+\tObject *parent_;\n+\tstd::vector<Object *> children_;\n+\n \tThread *thread_;\n \tstd::list<SignalBase *> signals_;\n \tunsigned int pendingMessages_;\ndiff --git a/src/libcamera/include/thread.h b/src/libcamera/include/thread.h\nindex 630abb49534f..37edd4f5138b 100644\n--- a/src/libcamera/include/thread.h\n+++ b/src/libcamera/include/thread.h\n@@ -61,6 +61,8 @@ private:\n \tfriend class ThreadMain;\n \n \tvoid moveObject(Object *object);\n+\tvoid moveObject(Object *object, ThreadData *currentData,\n+\t\t\tThreadData *targetData);\n \n \tstd::thread thread_;\n \tThreadData *data_;\ndiff --git a/src/libcamera/object.cpp b/src/libcamera/object.cpp\nindex bbb28f261405..98aa0af2f9b9 100644\n--- a/src/libcamera/object.cpp\n+++ b/src/libcamera/object.cpp\n@@ -7,6 +7,8 @@\n \n #include <libcamera/object.h>\n \n+#include <algorithm>\n+\n #include <libcamera/signal.h>\n \n #include \"log.h\"\n@@ -21,6 +23,8 @@\n \n namespace libcamera {\n \n+LOG_DEFINE_CATEGORY(Object)\n+\n /**\n  * \\class Object\n  * \\brief Base object to support automatic signal disconnection\n@@ -29,10 +33,11 @@ namespace libcamera {\n  * slots. By inheriting from Object, an object is automatically disconnected\n  * from all connected signals when it gets destroyed.\n  *\n- * Object instances are bound to the thread in which they're created. When a\n- * message is posted to an object, its handler will run in the object's thread.\n- * This allows implementing easy message passing between threads by inheriting\n- * from the Object class.\n+ * Object instances are bound to the thread of their parent, or the thread in\n+ * which they're created when they have no parent. When a message is posted to\n+ * an object, its handler will run in the object's thread. This allows\n+ * implementing easy message passing between threads by inheriting from the\n+ * Object class.\n  *\n  * Object slots connected to signals will also run in the context of the\n  * object's thread, regardless of whether the signal is emitted in the same or\n@@ -41,12 +46,29 @@ namespace libcamera {\n  * \\sa Message, Signal, Thread\n  */\n \n-Object::Object()\n-\t: pendingMessages_(0)\n+/**\n+ * \\brief Construct an Object instance\n+ * \\param[in] parent The object parent\n+ *\n+ * The new Object instance is bound to the thread of its \\a parent, or to the\n+ * current thread if the \\a parent is nullptr.\n+ */\n+Object::Object(Object *parent)\n+\t: parent_(parent), pendingMessages_(0)\n {\n-\tthread_ = Thread::current();\n+\tthread_ = parent ? parent->thread() : Thread::current();\n+\n+\tif (parent)\n+\t\tparent->children_.push_back(this);\n }\n \n+/**\n+ * \\brief Destroy an Object instance\n+ *\n+ * Deleting an Object automatically disconnects all signals from the Object's\n+ * slots. All the Object's children are made orphan, but stay bound to their\n+ * current thread.\n+ */\n Object::~Object()\n {\n \tfor (SignalBase *signal : signals_)\n@@ -54,6 +76,16 @@ Object::~Object()\n \n \tif (pendingMessages_)\n \t\tthread()->removeMessages(this);\n+\n+\tif (parent_) {\n+\t\tauto it = std::find(parent_->children_.begin(),\n+\t\t\t\t    parent_->children_.end(), this);\n+\t\tASSERT(it != parent_->children_.end());\n+\t\tparent_->children_.erase(it);\n+\t}\n+\n+\tfor (auto child : children_)\n+\t\tchild->parent_ = nullptr;\n }\n \n /**\n@@ -129,16 +161,19 @@ void Object::invokeMethod(BoundMethodBase *method, void *args)\n  */\n \n /**\n- * \\brief Move the object to a different thread\n+ * \\brief Move the object and all its children to a different thread\n  * \\param[in] thread The target thread\n  *\n- * This method moves the object from the current thread to the new \\a thread.\n- * It shall be called from the thread in which the object currently lives,\n- * otherwise the behaviour is undefined.\n+ * This method moves the object and all its children from the current thread to\n+ * the new \\a thread. It shall be called from the thread in which the object\n+ * currently lives, otherwise the behaviour is undefined.\n  *\n  * Before the object is moved, a Message::ThreadMoveMessage message is sent to\n  * it. The message() method can be reimplement in derived classes to be notified\n  * of the upcoming thread move and perform any required processing.\n+ *\n+ * Moving an object that has a parent is not allowed, and causes undefined\n+ * behaviour.\n  */\n void Object::moveToThread(Thread *thread)\n {\n@@ -147,6 +182,12 @@ void Object::moveToThread(Thread *thread)\n \tif (thread_ == thread)\n \t\treturn;\n \n+\tif (parent_) {\n+\t\tLOG(Object, Error)\n+\t\t\t<< \"Moving object to thread with a parent is not permitted\";\n+\t\treturn;\n+\t}\n+\n \tnotifyThreadMove();\n \n \tthread->moveObject(this);\n@@ -156,8 +197,17 @@ void Object::notifyThreadMove()\n {\n \tMessage msg(Message::ThreadMoveMessage);\n \tmessage(&msg);\n+\n+\tfor (auto child : children_)\n+\t\tchild->notifyThreadMove();\n }\n \n+/**\n+ * \\fn Object::parent()\n+ * \\brief Retrieve the object's parent\n+ * \\return The object's parent\n+ */\n+\n void Object::connect(SignalBase *signal)\n {\n \tsignals_.push_back(signal);\ndiff --git a/src/libcamera/thread.cpp b/src/libcamera/thread.cpp\nindex 24422f7b7bd0..872ad1bd9d69 100644\n--- a/src/libcamera/thread.cpp\n+++ b/src/libcamera/thread.cpp\n@@ -448,7 +448,7 @@ void Thread::dispatchMessages()\n }\n \n /**\n- * \\brief Move an \\a object to the thread\n+ * \\brief Move an \\a object and all its children to the thread\n  * \\param[in] object The object\n  */\n void Thread::moveObject(Object *object)\n@@ -460,6 +460,12 @@ void Thread::moveObject(Object *object)\n \tMutexLocker lockerTo(targetData->mutex_, std::defer_lock);\n \tstd::lock(lockerFrom, lockerTo);\n \n+\tmoveObject(object, currentData, targetData);\n+}\n+\n+void Thread::moveObject(Object *object, ThreadData *currentData,\n+\t\t\tThreadData *targetData)\n+{\n \t/* Move pending messages to the message queue of the new thread. */\n \tif (object->pendingMessages_) {\n \t\tunsigned int movedMessages = 0;\n@@ -483,6 +489,10 @@ void Thread::moveObject(Object *object)\n \t}\n \n \tobject->thread_ = this;\n+\n+\t/* Move all children. */\n+\tfor (auto child : object->children_)\n+\t\tmoveObject(child, currentData, targetData);\n }\n \n }; /* namespace libcamera */\n",
    "prefixes": [
        "libcamera-devel",
        "v2",
        "15/18"
    ]
}