{"id":2697,"url":"https://patchwork.libcamera.org/api/1.1/patches/2697/?format=json","web_url":"https://patchwork.libcamera.org/patch/2697/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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":"<20200120002437.6633-11-laurent.pinchart@ideasonboard.com>","date":"2020-01-20T00:24:28","name":"[libcamera-devel,10/19] libcamera: Define the threading model","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"c6e895f36abf5ae03855fa86c616d441f09d004a","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/2697/mbox/","series":[{"id":641,"url":"https://patchwork.libcamera.org/api/1.1/series/641/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=641","date":"2020-01-20T00:24:19","name":"Initial libcamera threading model","version":1,"mbox":"https://patchwork.libcamera.org/series/641/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2697/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2697/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 4DC78607DF\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 20 Jan 2020 01:24:46 +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 E34A7529\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 20 Jan 2020 01:24:45 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1579479886;\n\tbh=B55uMI/61cz8qwfQFgZpeVW8M2AhxylTUV0IPlyUnkA=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=BZq9KRZO0Q9pr8FUPLOyNE5jpBZlR8TMRmIHi9B3ZjQtW5LFO6grQ6vCfxdvR413c\n\tjCvDRA8bvthhYHmkqRp0pyqn8vaM8wvkCpo/G0/WH7HMHH7mEjXZNd+BkK5+U+LBkE\n\tU8K5QunDPwSJC+JeRUHAJ+AhBgHwIl3nnrAk5BR4=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 20 Jan 2020 02:24:28 +0200","Message-Id":"<20200120002437.6633-11-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","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 10/19] libcamera: Define the threading\n\tmodel","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":"Mon, 20 Jan 2020 00:24:47 -0000"},"content":"Document the design of libcamera's threading support, and prepare to\ndocument thread-safety of classes and functions with a doxygen alias\ncommand.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n Documentation/Doxyfile.in |  4 +-\n src/libcamera/thread.cpp  | 82 +++++++++++++++++++++++++++++++++++++++\n 2 files changed, 85 insertions(+), 1 deletion(-)","diff":"diff --git a/Documentation/Doxyfile.in b/Documentation/Doxyfile.in\nindex 8e6fbdbb92b6..734672ed15dc 100644\n--- a/Documentation/Doxyfile.in\n+++ b/Documentation/Doxyfile.in\n@@ -239,7 +239,9 @@ TAB_SIZE               = 4\n # newlines (in the resulting output). You can put ^^ in the value part of an\n # alias to insert a newline as if a physical newline was in the original file.\n \n-ALIASES                =\n+ALIASES                = \"context=\\xrefitem context \\\"Thread Safety\\\" \\\"Thread Safety\\\"\"\n+ALIASES               += \"threadbound=\\ref thread-bound \\\"thread-bound\\\"\"\n+ALIASES               += \"threadsafe=\\ref thread-safe \\\"thread-safe\\\"\"\n \n # This tag can be used to specify a number of word-keyword mappings (TCL only).\n # A mapping has the form \"name=value\". For example adding \"class=itcl::class\"\ndiff --git a/src/libcamera/thread.cpp b/src/libcamera/thread.cpp\nindex fe32cd677596..c1698d469a6c 100644\n--- a/src/libcamera/thread.cpp\n+++ b/src/libcamera/thread.cpp\n@@ -19,6 +19,88 @@\n #include \"log.h\"\n #include \"message.h\"\n \n+/**\n+ * \\page thread Thread Support\n+ *\n+ * libcamera supports multi-threaded applications through a threading model that\n+ * sets precise rules to guarantee thread-safe usage of the API. Additionally,\n+ * libcamera makes internal use of threads, and offers APIs that simplify\n+ * interactions with application threads. Careful compliance with the threading\n+ * model will ensure avoidance of race conditions.\n+ *\n+ * \\section thread-reentrancy Reentrancy and Thread-Safety\n+ *\n+ * Through the documentation, several terms are used to define how classes and\n+ * their member functions can be used from multiple threads.\n+ *\n+ * - A **reentrant** function may be called simultaneously from multiple\n+ *   threads if and only if each invocation uses a different instance of the\n+ *   class. This is the default for all member functions not explictly marked\n+ *   otherwise.\n+ *\n+ * - \\anchor thread-safe A **thread-safe** function may be called\n+ *   simultaneously from multiple threads on the same instance of a class. A\n+ *   thread-safe function is thus reentrant.\n+ *\n+ * - \\anchor thread-bound A **thread-bound** function may be called only from\n+ *   the thread that the class instances lives in (see section \\ref\n+ *   thread-objects). For instances of classes that do not derive from the\n+ *   Object class, this is the thread in which the instance was created. A\n+ *   thread-bound function is not thread-safe, and may or may not be reentrant.\n+ *\n+ * Neither reentrancy nor thread-safety, in this context, mean that a function\n+ * may be called simultaneously from the same thread, for instance from a\n+ * callback invoked by the the function. This may deadlock and isn't allowed\n+ * unless separately documented.\n+ *\n+ * A class is defined as reentrant, thread-safe or thread-bound if all its\n+ * member functions are reentrant, thread-safe or thread-bound respectively.\n+ * Some member functions may additionally be documented as having additional\n+ * thread-related attributes.\n+ *\n+ * Most classes are reentrant but not thread-safe, as making them fully\n+ * thread-safe would incur locking costs considered prohibitive for the\n+ * expected use cases.\n+ *\n+ * \\section thread-objects Threads and Objects\n+ *\n+ * Instances of the Object class and all its derived classes are thread-aware\n+ * and are bound to the thread they are created in. They are said to *live* in\n+ * a thread, and they interact with the event loop of their thread for the\n+ * purpose of message passing and signal delivery. Messages posted to the\n+ * object with Object::postMessage() will be delivered from the event loop of\n+ * the thread that the object lives in. Signals delivered to the object, unless\n+ * explicitly connected with ConnectionTypeDirect, will also be delivered from\n+ * the object thread's event loop.\n+ *\n+ * All Object instances created by libcamera are bound to an internal thread,\n+ * an applications don't need to provide an event loop to support them. Object\n+ * instances created by applications require an event loop. It is the\n+ * responsibility of applications to provide that event loop, either explicitly\n+ * through CameraManager::setEventDispatcher(), or by running the default event\n+ * loop provided by CameraManager::eventDispatcher() in their main thread. The\n+ * main thread of an application is the one that calls CameraManager::start().\n+ *\n+ * \\section thread-signals Threads and Signals\n+ *\n+ * When sent to a receiver that does not inherit from the Object class, signals\n+ * are delivered synchronously in the thread of the sender. When the receiver\n+ * inherits from the Object class, delivery is by default asynchronous if the\n+ * sender and receiver live in different threads. In that case, the signal is\n+ * posted to the receiver's message queue and will be delivered from the\n+ * receiver's event loop, running in the receiver's thread. This mechanism can\n+ * be overridden by selecting a different connection type when calling\n+ * Signal::connect().\n+ *\n+ * Asynchronous signal delivery is used internally in libcamera, but is also\n+ * available to applications if desired. To use this feature, applications\n+ * shall create receiver classes that inherit from the Object class, and\n+ * provide an event loop to the CameraManager as explained above. Note that\n+ * Object instance created by the application are limited to living in the\n+ * application's main thread. Creating Object instances from another thread of\n+ * an application causes undefined behaviour.\n+ */\n+\n /**\n  * \\file thread.h\n  * \\brief Thread support\n","prefixes":["libcamera-devel","10/19"]}