From patchwork Sat Aug 17 15:20:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 1830 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 4E90761920 for ; Sat, 17 Aug 2019 17:21:17 +0200 (CEST) Received: from pendragon.bb.dnainternet.fi (dfj612yhrgyx302h3jwwy-3.rev.dnainternet.fi [IPv6:2001:14ba:21f5:5b00:ce28:277f:58d7:3ca4]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id DB9B5E70 for ; Sat, 17 Aug 2019 17:21:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1566055277; bh=k3lsHINhRA1bC8Q8vyGfm6YEox6cCIeNKhXPKEZincc=; h=From:To:Subject:Date:In-Reply-To:References:From; b=UWDwJfmQcP91gByQYZ+XmbjMzIuLyCugoVWFfXoJs9GL9l9gi1Ne4pvvxD4x1jTEx +RCPHsxUz8cEvuB46bKjveXjgRVoDnrtFds2DhZOSiU0/AQvoeiS1gFlV9b3dyslav v2dRJpb3q2jh9E5iQ+FyXHqan0elg4Xe5cwYTovo= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Sat, 17 Aug 2019 18:20:58 +0300 Message-Id: <20190817152104.10834-13-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190817152104.10834-1-laurent.pinchart@ideasonboard.com> References: <20190817152104.10834-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 12/18] libcamera: timer: Bind timers to threads X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 17 Aug 2019 15:21:18 -0000 The Timer instances are registered with the event dispatcher instance of the CameraManager. This makes it impossible to use timers in other threads. Fix this by inheriting from Object, which allows binding instances to a thread, and register them with the event dispatcher for the thread they are bound to. Signed-off-by: Laurent Pinchart Reviewed-by: Jacopo Mondi Reviewed-by: Niklas Söderlund --- Changes since v1: - Fix invokeMethod() call --- include/libcamera/timer.h | 11 ++++++++++- src/libcamera/timer.cpp | 28 ++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/include/libcamera/timer.h b/include/libcamera/timer.h index f082339b1fed..853808e07da8 100644 --- a/include/libcamera/timer.h +++ b/include/libcamera/timer.h @@ -9,11 +9,14 @@ #include +#include #include namespace libcamera { -class Timer +class Message; + +class Timer : public Object { public: Timer(); @@ -28,7 +31,13 @@ public: Signal timeout; +protected: + void message(Message *msg) override; + private: + void registerTimer(); + void unregisterTimer(); + unsigned int interval_; uint64_t deadline_; }; diff --git a/src/libcamera/timer.cpp b/src/libcamera/timer.cpp index 0dcb4e767be3..f45061d4c6f6 100644 --- a/src/libcamera/timer.cpp +++ b/src/libcamera/timer.cpp @@ -13,6 +13,8 @@ #include #include "log.h" +#include "message.h" +#include "thread.h" /** * \file timer.h @@ -66,7 +68,7 @@ void Timer::start(unsigned int msec) << "Starting timer " << this << " with interval " << msec << ": deadline " << deadline_; - CameraManager::instance()->eventDispatcher()->registerTimer(this); + registerTimer(); } /** @@ -79,11 +81,21 @@ void Timer::start(unsigned int msec) */ void Timer::stop() { - CameraManager::instance()->eventDispatcher()->unregisterTimer(this); + unregisterTimer(); deadline_ = 0; } +void Timer::registerTimer() +{ + thread()->eventDispatcher()->registerTimer(this); +} + +void Timer::unregisterTimer() +{ + thread()->eventDispatcher()->unregisterTimer(this); +} + /** * \brief Check if the timer is running * \return True if the timer is running, false otherwise @@ -112,4 +124,16 @@ bool Timer::isRunning() const * The timer pointer is passed as a parameter. */ +void Timer::message(Message *msg) +{ + if (msg->type() == Message::ThreadMoveMessage) { + if (deadline_) { + unregisterTimer(); + invokeMethod(&Timer::registerTimer); + } + } + + Object::message(msg); +} + } /* namespace libcamera */