{"id":2699,"url":"https://patchwork.libcamera.org/api/patches/2699/?format=json","web_url":"https://patchwork.libcamera.org/patch/2699/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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-13-laurent.pinchart@ideasonboard.com>","date":"2020-01-20T00:24:30","name":"[libcamera-devel,12/19] libcamera: signal: Make connection and disconnection thread-safe","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"401b92151879ac0724a85fb53c847df4f6ad833e","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/2699/mbox/","series":[{"id":641,"url":"https://patchwork.libcamera.org/api/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/2699/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2699/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 E66CE607C4\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 8AF34504\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 20 Jan 2020 01:24:46 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1579479886;\n\tbh=51/YLrAB8DXWNr/g/YSoeq08tZX7TNIoH3L7qTNvrCc=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=pptF5bsc/7BBVl5QEWBemumLzV38G+XXssLMHGdSP97zArUmguqP0XYAVL0c2tk8a\n\tu4n8d0/+4K0ubNSdk+nPARoR8/ICzR5ZPMKEO7JcAg5iBscuy0ks/qkIqvtnnp8TMT\n\ty8SJ0UCFKgd8U4WyHxXK45Nwb3juoXpdKPsAT2bk=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 20 Jan 2020 02:24:30 +0200","Message-Id":"<20200120002437.6633-13-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 12/19] libcamera: signal: Make connection\n\tand 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":"Mon, 20 Jan 2020 00:24:47 -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>\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","12/19"]}