{"id":2724,"url":"https://patchwork.libcamera.org/api/1.1/patches/2724/?format=json","web_url":"https://patchwork.libcamera.org/patch/2724/","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":"<20200122205723.8865-9-laurent.pinchart@ideasonboard.com>","date":"2020-01-22T20:57:18","name":"[libcamera-devel,v2,08/13] libcamera: signal: Make connection and disconnection thread-safe","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"401b92151879ac0724a85fb53c847df4f6ad833e","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/2724/mbox/","series":[{"id":645,"url":"https://patchwork.libcamera.org/api/1.1/series/645/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=645","date":"2020-01-22T20:57:10","name":"Initial libcamera threading model","version":2,"mbox":"https://patchwork.libcamera.org/series/645/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2724/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2724/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 6D4AC60881\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 22 Jan 2020 21:57:45 +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 181E82E5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 22 Jan 2020 21:57:45 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1579726665;\n\tbh=vebNeGtWI0w6k+eCpMICX8kMcmih7ByGwkSV+rMLlPw=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=tDaL/PdQ9r64ykxIFMh5oDKcALJmz0N8xEGgbOPmh0sQMlIrzZmOKz6In20Bvjhb/\n\t3+E16tU2ZskyJfD7wl2aUJDqv7Il7o52fsoas6azpRfAj0HwLdsqnkeDUMYmbWjyh1\n\tynvuCOBY428erkSTkQ8Y/uxgeRPktoJD1AY8Jjxo=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Wed, 22 Jan 2020 22:57:18 +0200","Message-Id":"<20200122205723.8865-9-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.24.1","In-Reply-To":"<20200122205723.8865-1-laurent.pinchart@ideasonboard.com>","References":"<20200122205723.8865-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 08/13] libcamera: signal: Make\n\tconnection and disconnection thread-safe","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":"Wed, 22 Jan 2020 20:57:48 -0000"},"content":"Make the signal connection and disconnection thread-safe, and document\nthem as such. This is required to make objects connectable from\ndifferent threads.\n\nThe connect(), disconnect() and emit() methods are now all protected by\na global mutex, which may generate a high lock contention. This could be\nimproved with finer-grained locks or with a pool of mutexes.\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---\n src/libcamera/signal.cpp | 33 +++++++++++++++++++++++++++++++++\n 1 file changed, 33 insertions(+)","diff":"diff --git a/src/libcamera/signal.cpp b/src/libcamera/signal.cpp\nindex 9bb7eca8ed6e..6eab1fa74d42 100644\n--- a/src/libcamera/signal.cpp\n+++ b/src/libcamera/signal.cpp\n@@ -7,6 +7,8 @@\n \n #include <libcamera/signal.h>\n \n+#include \"thread.h\"\n+\n /**\n  * \\file signal.h\n  * \\brief Signal & slot implementation\n@@ -14,8 +16,21 @@\n \n namespace libcamera {\n \n+namespace {\n+\n+/*\n+ * Mutex to protect the SignalBase::slots_ and Object::signals_ lists. If lock\n+ * contention needs to be decreased, this could be replaced with locks in\n+ * Object and SignalBase, or with a mutex pool.\n+ */\n+Mutex signalsLock;\n+\n+} /* namespace */\n+\n void SignalBase::connect(BoundMethodBase *slot)\n {\n+\tMutexLocker locker(signalsLock);\n+\n \tObject *object = slot->object();\n \tif (object)\n \t\tobject->connect(this);\n@@ -31,6 +46,8 @@ void SignalBase::disconnect(Object *object)\n \n void SignalBase::disconnect(std::function<bool(SlotList::iterator &)> match)\n {\n+\tMutexLocker locker(signalsLock);\n+\n \tfor (auto iter = slots_.begin(); iter != slots_.end(); ) {\n \t\tif (match(iter)) {\n \t\t\tObject *object = (*iter)->object();\n@@ -47,6 +64,7 @@ void SignalBase::disconnect(std::function<bool(SlotList::iterator &)> match)\n \n SignalBase::SlotList SignalBase::slots()\n {\n+\tMutexLocker locker(signalsLock);\n \treturn slots_;\n }\n \n@@ -99,23 +117,31 @@ SignalBase::SlotList SignalBase::slots()\n  * disconnected from the \\a func slot of \\a object when \\a object is destroyed.\n  * Otherwise the caller shall disconnect signals manually before destroying \\a\n  * object.\n+ *\n+ * \\context This function is \\threadsafe.\n  */\n \n /**\n  * \\fn Signal::connect(R (*func)(Args...))\n  * \\brief Connect the signal to a static function slot\n  * \\param[in] func The slot static function\n+ *\n+ * \\context This function is \\threadsafe.\n  */\n \n /**\n  * \\fn Signal::disconnect()\n  * \\brief Disconnect the signal from all slots\n+ *\n+ * \\context This function is \\threadsafe.\n  */\n \n /**\n  * \\fn Signal::disconnect(T *object)\n  * \\brief Disconnect the signal from all slots of the \\a object\n  * \\param[in] object The object pointer whose slots to disconnect\n+ *\n+ * \\context This function is \\threadsafe.\n  */\n \n /**\n@@ -123,12 +149,16 @@ SignalBase::SlotList SignalBase::slots()\n  * \\brief Disconnect the signal from the \\a object slot member function \\a func\n  * \\param[in] object The object pointer whose slots to disconnect\n  * \\param[in] func The slot member function to disconnect\n+ *\n+ * \\context This function is \\threadsafe.\n  */\n \n /**\n  * \\fn Signal::disconnect(R (*func)(Args...))\n  * \\brief Disconnect the signal from the slot static function \\a func\n  * \\param[in] func The slot static function to disconnect\n+ *\n+ * \\context This function is \\threadsafe.\n  */\n \n /**\n@@ -141,6 +171,9 @@ SignalBase::SlotList SignalBase::slots()\n  * function are passed to the slot functions unchanged. If a slot modifies one\n  * of the arguments (when passed by pointer or reference), the modification is\n  * thus visible to all subsequently called slots.\n+ *\n+ * This function is not \\threadsafe, but thread-safety is guaranteed against\n+ * concurrent connect() and disconnect() calls.\n  */\n \n } /* namespace libcamera */\n","prefixes":["libcamera-devel","v2","08/13"]}