From patchwork Wed Jan 22 20:57:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2724 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6D4AC60881 for ; Wed, 22 Jan 2020 21:57:45 +0100 (CET) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 181E82E5 for ; Wed, 22 Jan 2020 21:57:45 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1579726665; bh=vebNeGtWI0w6k+eCpMICX8kMcmih7ByGwkSV+rMLlPw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=tDaL/PdQ9r64ykxIFMh5oDKcALJmz0N8xEGgbOPmh0sQMlIrzZmOKz6In20Bvjhb/ 3+E16tU2ZskyJfD7wl2aUJDqv7Il7o52fsoas6azpRfAj0HwLdsqnkeDUMYmbWjyh1 ynvuCOBY428erkSTkQ8Y/uxgeRPktoJD1AY8Jjxo= From: Laurent Pinchart 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 Subject: [libcamera-devel] [PATCH v2 08/13] libcamera: signal: Make connection and disconnection thread-safe X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 22 Jan 2020 20:57:48 -0000 Make the signal connection and disconnection thread-safe, and document them as such. This is required to make objects connectable from different threads. The connect(), disconnect() and emit() methods are now all protected by a global mutex, which may generate a high lock contention. This could be improved with finer-grained locks or with a pool of mutexes. Signed-off-by: Laurent Pinchart Reviewed-by: Jacopo Mondi Reviewed-by: Niklas Söderlund --- src/libcamera/signal.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/libcamera/signal.cpp b/src/libcamera/signal.cpp index 9bb7eca8ed6e..6eab1fa74d42 100644 --- a/src/libcamera/signal.cpp +++ b/src/libcamera/signal.cpp @@ -7,6 +7,8 @@ #include +#include "thread.h" + /** * \file signal.h * \brief Signal & slot implementation @@ -14,8 +16,21 @@ namespace libcamera { +namespace { + +/* + * Mutex to protect the SignalBase::slots_ and Object::signals_ lists. If lock + * contention needs to be decreased, this could be replaced with locks in + * Object and SignalBase, or with a mutex pool. + */ +Mutex signalsLock; + +} /* namespace */ + void SignalBase::connect(BoundMethodBase *slot) { + MutexLocker locker(signalsLock); + Object *object = slot->object(); if (object) object->connect(this); @@ -31,6 +46,8 @@ void SignalBase::disconnect(Object *object) void SignalBase::disconnect(std::function match) { + MutexLocker locker(signalsLock); + for (auto iter = slots_.begin(); iter != slots_.end(); ) { if (match(iter)) { Object *object = (*iter)->object(); @@ -47,6 +64,7 @@ void SignalBase::disconnect(std::function match) SignalBase::SlotList SignalBase::slots() { + MutexLocker locker(signalsLock); return slots_; } @@ -99,23 +117,31 @@ SignalBase::SlotList SignalBase::slots() * disconnected from the \a func slot of \a object when \a object is destroyed. * Otherwise the caller shall disconnect signals manually before destroying \a * object. + * + * \context This function is \threadsafe. */ /** * \fn Signal::connect(R (*func)(Args...)) * \brief Connect the signal to a static function slot * \param[in] func The slot static function + * + * \context This function is \threadsafe. */ /** * \fn Signal::disconnect() * \brief Disconnect the signal from all slots + * + * \context This function is \threadsafe. */ /** * \fn Signal::disconnect(T *object) * \brief Disconnect the signal from all slots of the \a object * \param[in] object The object pointer whose slots to disconnect + * + * \context This function is \threadsafe. */ /** @@ -123,12 +149,16 @@ SignalBase::SlotList SignalBase::slots() * \brief Disconnect the signal from the \a object slot member function \a func * \param[in] object The object pointer whose slots to disconnect * \param[in] func The slot member function to disconnect + * + * \context This function is \threadsafe. */ /** * \fn Signal::disconnect(R (*func)(Args...)) * \brief Disconnect the signal from the slot static function \a func * \param[in] func The slot static function to disconnect + * + * \context This function is \threadsafe. */ /** @@ -141,6 +171,9 @@ SignalBase::SlotList SignalBase::slots() * function are passed to the slot functions unchanged. If a slot modifies one * of the arguments (when passed by pointer or reference), the modification is * thus visible to all subsequently called slots. + * + * This function is not \threadsafe, but thread-safety is guaranteed against + * concurrent connect() and disconnect() calls. */ } /* namespace libcamera */