Show a patch.

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

{
    "id": 1793,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/1793/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/1793/",
    "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": "<20190812124642.24287-16-laurent.pinchart@ideasonboard.com>",
    "date": "2019-08-12T12:46:39",
    "name": "[libcamera-devel,15/18] libcamera: object: Create parent-child relationships",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "d87442674054c9a864b1f179ede25eca2be9bddc",
    "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/1793/mbox/",
    "series": [
        {
            "id": 455,
            "url": "https://patchwork.libcamera.org/api/1.1/series/455/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=455",
            "date": "2019-08-12T12:46:24",
            "name": "Object & Thread enhancements",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/455/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/1793/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/1793/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 C5B6461965\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 12 Aug 2019 14:46:54 +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 62720327\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 12 Aug 2019 14:46:54 +0200 (CEST)"
        ],
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1565614014;\n\tbh=Olctu71DA8JkmR8208+oSL5X2yoXETwUTiYkft+WM6Q=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=I2+z4YElnuWqOTA4iSKJJtwVclPcCY0sstp7BdlYNljCizBb5dQHzj6SF53Np/94C\n\tuq+C67YLEyM1PyuQOjtYE6fGUGA5NOawkBrYuTaPq9gXqezuQAuKVXWoS+LieTnya4\n\tJBcRSjESbUpUrYCttrn5RWxjb693UGPXSYxvGPTM=",
        "From": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Mon, 12 Aug 2019 15:46:39 +0300",
        "Message-Id": "<20190812124642.24287-16-laurent.pinchart@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.21.0",
        "In-Reply-To": "<20190812124642.24287-1-laurent.pinchart@ideasonboard.com>",
        "References": "<20190812124642.24287-1-laurent.pinchart@ideasonboard.com>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH 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": "Mon, 12 Aug 2019 12:46:56 -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>\n---\n include/libcamera/object.h     |  8 ++++-\n src/libcamera/include/thread.h |  2 ++\n src/libcamera/object.cpp       | 65 ++++++++++++++++++++++++++++------\n src/libcamera/thread.cpp       | 12 ++++++-\n 4 files changed, 74 insertions(+), 13 deletions(-)",
    "diff": "diff --git a/include/libcamera/object.h b/include/libcamera/object.h\nindex 14b939a9bd3d..4a0a272b875a 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@@ -42,6 +43,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@@ -58,6 +61,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 7c68ec01f78c..57cd6fefe20f 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,10 +46,20 @@ 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 Object::~Object()\n@@ -54,6 +69,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@@ -143,16 +168,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@@ -161,6 +189,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@@ -170,8 +204,17 @@ void Object::notifyThreadMove()\n {\n \tMessage msg(Message::ThreadMoveMessage);\n \tsendMessage(&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",
        "15/18"
    ]
}