From 6ecf99a708936c4b7a33ad6296baa04e9975abb2 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Sun, 11 Aug 2019 15:42:18 +0300 Subject: libcamera: timer: Bind timers to threads MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/libcamera/timer.cpp | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'src/libcamera/timer.cpp') diff --git a/src/libcamera/timer.cpp b/src/libcamera/timer.cpp index 0dcb4e76..f45061d4 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 */ -- cgit v1.2.1