From patchwork Mon Jan 20 00:24:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2696 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 073AE607DD for ; Mon, 20 Jan 2020 01:24:46 +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 931C6B57 for ; Mon, 20 Jan 2020 01:24:45 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1579479885; bh=5/f77IEbrZNeopV8uxm1wG8GJJMXo/vUPIp2sDTZG6c=; h=From:To:Subject:Date:In-Reply-To:References:From; b=wW0kFYMyDSt5tYKQDyd6FYE5WrwO55o6l9y/J0Y5JebkUgxB13gstDPa3x3xhVOIA Q4HLELsWEXr+dOqMiIfe66QHhE1hOnBHVh15ZTTYuh5WC17Z6hS6yhOp/oUz1uRiYN b/1zpyP2BngSq04xgRzfm13zYLgu/Rpw61J+2wOg= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Mon, 20 Jan 2020 02:24:27 +0200 Message-Id: <20200120002437.6633-10-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 Subject: [libcamera-devel] [PATCH 09/19] libcamera: signal: Make slots list private 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: Mon, 20 Jan 2020 00:24:47 -0000 The slots list is touched from most of the Signal template functions. In order to prepare for thread-safety, move handling of the lists list to a small number of non-template functions in the SignalBase class. This incidently fixes a bug in signal disconnection handling where the signal wasn't removed from the object's signals list, as pointed out by the signals unit test. Signed-off-by: Laurent Pinchart Reviewed-by: Jacopo Mondi Reviewed-by: Niklas Söderlund --- include/libcamera/object.h | 4 +- include/libcamera/signal.h | 88 ++++++++++++++++---------------------- src/libcamera/object.cpp | 7 ++- src/libcamera/signal.cpp | 36 ++++++++++++++++ 4 files changed, 81 insertions(+), 54 deletions(-) diff --git a/include/libcamera/object.h b/include/libcamera/object.h index 9344af30bc79..4d16f3f2b610 100644 --- a/include/libcamera/object.h +++ b/include/libcamera/object.h @@ -48,9 +48,7 @@ protected: virtual void message(Message *msg); private: - template - friend class Signal; - friend class BoundMethodBase; + friend class SignalBase; friend class Thread; void notifyThreadMove(); diff --git a/include/libcamera/signal.h b/include/libcamera/signal.h index 432d95d0ed1c..c13bb30f0636 100644 --- a/include/libcamera/signal.h +++ b/include/libcamera/signal.h @@ -7,6 +7,7 @@ #ifndef __LIBCAMERA_SIGNAL_H__ #define __LIBCAMERA_SIGNAL_H__ +#include #include #include #include @@ -19,23 +20,18 @@ namespace libcamera { class SignalBase { public: - template - void disconnect(T *obj) - { - for (auto iter = slots_.begin(); iter != slots_.end(); ) { - BoundMethodBase *slot = *iter; - if (slot->match(obj)) { - iter = slots_.erase(iter); - delete slot; - } else { - ++iter; - } - } - } + void disconnect(Object *object); protected: - friend class Object; - std::list slots_; + using SlotList = std::list; + + void connect(BoundMethodBase *slot); + void disconnect(std::function match); + + SlotList slots(); + +private: + SlotList slots_; }; template @@ -45,12 +41,7 @@ public: Signal() {} ~Signal() { - for (BoundMethodBase *slot : slots_) { - Object *object = slot->object(); - if (object) - object->disconnect(this); - delete slot; - } + disconnect(); } #ifndef __DOXYGEN__ @@ -59,8 +50,7 @@ public: ConnectionType type = ConnectionTypeAuto) { Object *object = static_cast(obj); - object->connect(this); - slots_.push_back(new BoundMethodMember(obj, object, func, type)); + SignalBase::connect(new BoundMethodMember(obj, object, func, type)); } template::value>::type * = nullptr> @@ -69,63 +59,62 @@ public: #endif void connect(T *obj, R (T::*func)(Args...)) { - slots_.push_back(new BoundMethodMember(obj, nullptr, func)); + SignalBase::connect(new BoundMethodMember(obj, nullptr, func)); } template void connect(R (*func)(Args...)) { - slots_.push_back(new BoundMethodStatic(func)); + SignalBase::connect(new BoundMethodStatic(func)); } void disconnect() { - for (BoundMethodBase *slot : slots_) - delete slot; - slots_.clear(); + SignalBase::disconnect([](SlotList::iterator &iter) { + return true; + }); } template void disconnect(T *obj) { - SignalBase::disconnect(obj); + SignalBase::disconnect([obj](SlotList::iterator &iter) { + return (*iter)->match(obj); + }); } template void disconnect(T *obj, R (T::*func)(Args...)) { - for (auto iter = slots_.begin(); iter != slots_.end(); ) { + SignalBase::disconnect([obj, func](SlotList::iterator &iter) { BoundMethodArgs *slot = static_cast *>(*iter); + + if (!slot->match(obj)) + return false; + /* * If the object matches the slot, the slot is * guaranteed to be a member slot, so we can safely * cast it to BoundMethodMember to match * func. */ - if (slot->match(obj) && - static_cast *>(slot)->match(func)) { - iter = slots_.erase(iter); - delete slot; - } else { - ++iter; - } - } + return static_cast *>(slot)->match(func); + }); } template void disconnect(R (*func)(Args...)) { - for (auto iter = slots_.begin(); iter != slots_.end(); ) { - BoundMethodArgs *slot = *iter; - if (slot->match(nullptr) && - static_cast *>(slot)->match(func)) { - iter = slots_.erase(iter); - delete slot; - } else { - ++iter; - } - } + SignalBase::disconnect([func](SlotList::iterator &iter) { + BoundMethodArgs *slot = + static_cast *>(*iter); + + if (!slot->match(nullptr)) + return false; + + return static_cast *>(slot)->match(func); + }); } void emit(Args... args) @@ -134,8 +123,7 @@ public: * Make a copy of the slots list as the slot could call the * disconnect operation, invalidating the iterator. */ - std::vector slots{ slots_.begin(), slots_.end() }; - for (BoundMethodBase *slot : slots) + for (BoundMethodBase *slot : slots()) static_cast *>(slot)->activate(args...); } }; diff --git a/src/libcamera/object.cpp b/src/libcamera/object.cpp index 21aad5652b38..f2a8be172547 100644 --- a/src/libcamera/object.cpp +++ b/src/libcamera/object.cpp @@ -76,7 +76,12 @@ Object::Object(Object *parent) */ Object::~Object() { - for (SignalBase *signal : signals_) + /* + * Move signals to a private list to avoid concurrent iteration and + * deletion of items from Signal::disconnect(). + */ + std::list signals(std::move(signals_)); + for (SignalBase *signal : signals) signal->disconnect(this); if (pendingMessages_) diff --git a/src/libcamera/signal.cpp b/src/libcamera/signal.cpp index 190033317c72..9bb7eca8ed6e 100644 --- a/src/libcamera/signal.cpp +++ b/src/libcamera/signal.cpp @@ -14,6 +14,42 @@ namespace libcamera { +void SignalBase::connect(BoundMethodBase *slot) +{ + Object *object = slot->object(); + if (object) + object->connect(this); + slots_.push_back(slot); +} + +void SignalBase::disconnect(Object *object) +{ + disconnect([object](SlotList::iterator &iter) { + return (*iter)->match(object); + }); +} + +void SignalBase::disconnect(std::function match) +{ + for (auto iter = slots_.begin(); iter != slots_.end(); ) { + if (match(iter)) { + Object *object = (*iter)->object(); + if (object) + object->disconnect(this); + + delete *iter; + iter = slots_.erase(iter); + } else { + ++iter; + } + } +} + +SignalBase::SlotList SignalBase::slots() +{ + return slots_; +} + /** * \class Signal * \brief Generic signal and slot communication mechanism